From a7d474926d7548af30f51004a4d12d2d8123086c Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 5 Aug 2025 19:02:40 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Miscellaneous:=20Time=20Complexity:=20Primality.=20Solved?= =?UTF-8?q?=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miscellaneous/ctci-big-o.md | 65 +++++++++++++++++++ .../miscellaneous/ctci_big_o.py | 33 ++++++++++ .../miscellaneous/ctci_big_o.testcases.json | 57 ++++++++++++++++ .../miscellaneous/ctci_big_o_test.py | 28 ++++++++ 4 files changed, 183 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md create mode 100644 src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.py create mode 100644 src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json create mode 100644 src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o_test.py diff --git a/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md b/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md new file mode 100644 index 00000000..6127c885 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md @@ -0,0 +1,65 @@ +# [Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` + +## Using bitwise operations + +A prime is a natural number greater than `1` that has no positive divisors other +than `1` and itself. +Given `p` integers, determine the primality of each integer and return `Prime` +or `Not prime` on a new line. + +**Note**: If possible, try to come up with an $ \mathcal{O}(\sqrt{n}) $ +primality algorithm, or see what sort of optimizations you can come up with for +san $ \mathcal{O}(\sqrt{n}) $ algorithm. Be sure to check out the Editorial +after submitting your code. + +## Function Description + +Complete the primality function in the editor below. +primality has the following parameter(s): + +- `int` n: an integer to test for primality + +## Returns + +- string: Prime if is prime, or Not prime + +## Input Format + +The first line contains an integer, , the number of integers to check for primality. +Each of the subsequent lines contains an integer, , the number to test. + +## Constraints + +- $ 1 \leq p \leq 30 $ +- $ 1 \leq n \leq 2 × 10^9 $ + +## Sample Input + +```text +STDIN Function +----- -------- +3 p = 3 (number of values to follow) +12 n = 12 (first number to check) +5 n = 5 +7 n = 7 +``` + +## Sample Output + +```text +Not prime +Prime +Prime +``` + +## Explanation + +We check the following $ p = 3 $ integers for primality: + +1. $ n = 12 $ is divisible by numbers other than $ 1 $ and itself + (i.e.: $ 2 $, $ 3 $, $ 4 $, $ 6 $). +1. $ n = 5 $ is only divisible and itself. +1. $ n = 7 $ is only divisible and itself. diff --git a/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.py b/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.py new file mode 100644 index 00000000..743430ea --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.py @@ -0,0 +1,33 @@ +# pylint: disable=line-too-long +# @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] # noqa +# pylint: enable=line-too-long + +import math + + +# pylint: disable=duplicate-code +def primeFactor(n: int) -> 'int | None': + + if n < 2: + return None + + divisor = n + max_prime_factor = None + + i = 2 + while i <= math.sqrt(divisor): + if 0 == divisor % i: + divisor = int(divisor / i) + max_prime_factor = divisor + else: + i += 1 + + if max_prime_factor is None: + return n + + return max_prime_factor +# pylint: enable=duplicate-code + + +def primality(n): + return "Prime" if primeFactor(n) == n else "Not prime" diff --git a/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json b/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json new file mode 100644 index 00000000..adfa3c30 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json @@ -0,0 +1,57 @@ +[ + { + "title": "Sample Test case 0", + "tests": [ + { + "input": 12, + "answer": "Not prime" + }, + { + "input": 5, + "answer": "Prime" + }, + { + "input": 7, + "answer": "Prime" + } + ] + }, + { + "title": "Sample Test case 1", + "tests": [ + { + "input": 31, + "answer": "Prime" + }, + { + "input": 33, + "answer": "Not prime" + } + ] + }, + { + "title": "Sample Test case 2", + "tests": [ + { + "input": 2, + "answer": "Prime" + }, + { + "input": 7, + "answer": "Prime" + }, + { + "input": 1982, + "answer": "Not prime" + }, + { + "input": 14582734, + "answer": "Not prime" + }, + { + "input": 9891, + "answer": "Not prime" + } + ] + } +] diff --git a/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o_test.py b/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o_test.py new file mode 100644 index 00000000..f0f44525 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o_test.py @@ -0,0 +1,28 @@ +import unittest +from pathlib import Path + +from ....lib.loader import loadTestCases +from .ctci_big_o import primality + + +FILE_PATH = str(Path(__file__).resolve().parent) + +TEST_CASES = loadTestCases( + FILE_PATH + '/ctci_big_o.testcases.json') + + +class TestCtciBigO(unittest.TestCase): + + def test_ctci_big_o(self): + + for _, testset in enumerate(TEST_CASES): + + for _, _tt in enumerate(testset['tests']): + + self.assertEqual( + primality(_tt['input']), _tt['answer'], + f"{_} | primality({_tt['input']}) must be " + f"=> {_tt['answer']}") + + def test_ctci_big_o_edge_cases(self): + self.assertEqual(primality(1), "Not prime")