Skip to content

Commit fd84720

Browse files
authored
Merge pull request #743 from sir-gon/feature/ctci-big-o
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Time Complexi…
2 parents c91f5be + a7d4749 commit fd84720

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingBasic`
5+
6+
## Using bitwise operations
7+
8+
A prime is a natural number greater than `1` that has no positive divisors other
9+
than `1` and itself.
10+
Given `p` integers, determine the primality of each integer and return `Prime`
11+
or `Not prime` on a new line.
12+
13+
**Note**: If possible, try to come up with an $ \mathcal{O}(\sqrt{n}) $
14+
primality algorithm, or see what sort of optimizations you can come up with for
15+
san $ \mathcal{O}(\sqrt{n}) $ algorithm. Be sure to check out the Editorial
16+
after submitting your code.
17+
18+
## Function Description
19+
20+
Complete the primality function in the editor below.
21+
primality has the following parameter(s):
22+
23+
- `int` n: an integer to test for primality
24+
25+
## Returns
26+
27+
- string: Prime if is prime, or Not prime
28+
29+
## Input Format
30+
31+
The first line contains an integer, , the number of integers to check for primality.
32+
Each of the subsequent lines contains an integer, , the number to test.
33+
34+
## Constraints
35+
36+
- $ 1 \leq p \leq 30 $
37+
- $ 1 \leq n \leq 2 × 10^9 $
38+
39+
## Sample Input
40+
41+
```text
42+
STDIN Function
43+
----- --------
44+
3 p = 3 (number of values to follow)
45+
12 n = 12 (first number to check)
46+
5 n = 5
47+
7 n = 7
48+
```
49+
50+
## Sample Output
51+
52+
```text
53+
Not prime
54+
Prime
55+
Prime
56+
```
57+
58+
## Explanation
59+
60+
We check the following $ p = 3 $ integers for primality:
61+
62+
1. $ n = 12 $ is divisible by numbers other than $ 1 $ and itself
63+
(i.e.: $ 2 $, $ 3 $, $ 4 $, $ 6 $).
64+
1. $ n = 5 $ is only divisible and itself.
65+
1. $ n = 7 $ is only divisible and itself.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# pylint: disable=line-too-long
2+
# @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] # noqa
3+
# pylint: enable=line-too-long
4+
5+
import math
6+
7+
8+
# pylint: disable=duplicate-code
9+
def primeFactor(n: int) -> 'int | None':
10+
11+
if n < 2:
12+
return None
13+
14+
divisor = n
15+
max_prime_factor = None
16+
17+
i = 2
18+
while i <= math.sqrt(divisor):
19+
if 0 == divisor % i:
20+
divisor = int(divisor / i)
21+
max_prime_factor = divisor
22+
else:
23+
i += 1
24+
25+
if max_prime_factor is None:
26+
return n
27+
28+
return max_prime_factor
29+
# pylint: enable=duplicate-code
30+
31+
32+
def primality(n):
33+
return "Prime" if primeFactor(n) == n else "Not prime"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"tests": [
5+
{
6+
"input": 12,
7+
"answer": "Not prime"
8+
},
9+
{
10+
"input": 5,
11+
"answer": "Prime"
12+
},
13+
{
14+
"input": 7,
15+
"answer": "Prime"
16+
}
17+
]
18+
},
19+
{
20+
"title": "Sample Test case 1",
21+
"tests": [
22+
{
23+
"input": 31,
24+
"answer": "Prime"
25+
},
26+
{
27+
"input": 33,
28+
"answer": "Not prime"
29+
}
30+
]
31+
},
32+
{
33+
"title": "Sample Test case 2",
34+
"tests": [
35+
{
36+
"input": 2,
37+
"answer": "Prime"
38+
},
39+
{
40+
"input": 7,
41+
"answer": "Prime"
42+
},
43+
{
44+
"input": 1982,
45+
"answer": "Not prime"
46+
},
47+
{
48+
"input": 14582734,
49+
"answer": "Not prime"
50+
},
51+
{
52+
"input": 9891,
53+
"answer": "Not prime"
54+
}
55+
]
56+
}
57+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from pathlib import Path
3+
4+
from ....lib.loader import loadTestCases
5+
from .ctci_big_o import primality
6+
7+
8+
FILE_PATH = str(Path(__file__).resolve().parent)
9+
10+
TEST_CASES = loadTestCases(
11+
FILE_PATH + '/ctci_big_o.testcases.json')
12+
13+
14+
class TestCtciBigO(unittest.TestCase):
15+
16+
def test_ctci_big_o(self):
17+
18+
for _, testset in enumerate(TEST_CASES):
19+
20+
for _, _tt in enumerate(testset['tests']):
21+
22+
self.assertEqual(
23+
primality(_tt['input']), _tt['answer'],
24+
f"{_} | primality({_tt['input']}) must be "
25+
f"=> {_tt['answer']}")
26+
27+
def test_ctci_big_o_edge_cases(self):
28+
self.assertEqual(primality(1), "Not prime")

0 commit comments

Comments
 (0)