From bc289302285232da6ecdd9bdd1886c1b352e1591 Mon Sep 17 00:00:00 2001 From: Dale Dai Date: Sat, 14 Oct 2023 19:35:41 -0700 Subject: [PATCH 1/5] Add perfect cube binary search algorithm --- maths/perfect_cube.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/maths/perfect_cube.py b/maths/perfect_cube.py index 9ad287e41e75..22983e4288cc 100644 --- a/maths/perfect_cube.py +++ b/maths/perfect_cube.py @@ -10,7 +10,36 @@ def perfect_cube(n: int) -> bool: val = n ** (1 / 3) return (val * val * val) == n +def perfect_cube_binary_search(n: int) -> bool: + """ + Check if a number is a perfect cube or not using binary search. + Time complexity : O(Log(n)) + Space complexity: O(1) + + >>> perfect_cube_binary_search(27) + True + >>> perfect_cube_binary_search(64) + True + >>> perfect_cube_binary_search(4) + False + >>> perfect_cube_binary_search("a") + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'str' + """ + left = 0 + right = n + while left <= right: + mid = left + (right - left) // 2 + if mid*mid*mid == n: + return True + elif mid*mid*mid < n: + left = mid + 1 + else: + right = mid - 1 + return False if __name__ == "__main__": - print(perfect_cube(27)) - print(perfect_cube(4)) + import doctest + + doctest.testmod() From 8b417b35b32e34b67c2bb2c236b6b8e338c6769c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 02:44:08 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/perfect_cube.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/perfect_cube.py b/maths/perfect_cube.py index 22983e4288cc..d647b6c44966 100644 --- a/maths/perfect_cube.py +++ b/maths/perfect_cube.py @@ -10,6 +10,7 @@ def perfect_cube(n: int) -> bool: val = n ** (1 / 3) return (val * val * val) == n + def perfect_cube_binary_search(n: int) -> bool: """ Check if a number is a perfect cube or not using binary search. @@ -31,14 +32,15 @@ def perfect_cube_binary_search(n: int) -> bool: right = n while left <= right: mid = left + (right - left) // 2 - if mid*mid*mid == n: + if mid * mid * mid == n: return True - elif mid*mid*mid < n: + elif mid * mid * mid < n: left = mid + 1 else: right = mid - 1 return False + if __name__ == "__main__": import doctest From 36dc424a8e3726f80c8b1a9e2261a56772283460 Mon Sep 17 00:00:00 2001 From: Dale Dai Date: Mon, 16 Oct 2023 20:23:48 -0700 Subject: [PATCH 3/5] Add support for testing negative perfect cubes --- maths/perfect_cube.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/perfect_cube.py b/maths/perfect_cube.py index d647b6c44966..ea84d7d06313 100644 --- a/maths/perfect_cube.py +++ b/maths/perfect_cube.py @@ -28,6 +28,8 @@ def perfect_cube_binary_search(n: int) -> bool: ... TypeError: '<=' not supported between instances of 'int' and 'str' """ + if n < 0: + n = -n left = 0 right = n while left <= right: From 3a7a34df45f5c02fe02a0ec3748b4fe513abe87a Mon Sep 17 00:00:00 2001 From: Dale Dai Date: Tue, 17 Oct 2023 21:03:50 -0700 Subject: [PATCH 4/5] Add TypeError check for invalid inputs --- maths/perfect_cube.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/maths/perfect_cube.py b/maths/perfect_cube.py index ea84d7d06313..e8685caedfb0 100644 --- a/maths/perfect_cube.py +++ b/maths/perfect_cube.py @@ -26,8 +26,14 @@ def perfect_cube_binary_search(n: int) -> bool: >>> perfect_cube_binary_search("a") Traceback (most recent call last): ... - TypeError: '<=' not supported between instances of 'int' and 'str' + TypeError: perfect_cube_binary_search() only accepts integers + >>> perfect_cube_binary_search(0.1) + Traceback (most recent call last): + ... + TypeError: perfect_cube_binary_search() only accepts integers """ + if not isinstance(n, int): + raise TypeError("perfect_cube_binary_search() only accepts integers") if n < 0: n = -n left = 0 @@ -46,4 +52,4 @@ def perfect_cube_binary_search(n: int) -> bool: if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file From dd40d01b3ec55cc74b833355adc01e75ca243ca0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 04:05:31 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/perfect_cube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/perfect_cube.py b/maths/perfect_cube.py index e8685caedfb0..a732b7cce6c8 100644 --- a/maths/perfect_cube.py +++ b/maths/perfect_cube.py @@ -52,4 +52,4 @@ def perfect_cube_binary_search(n: int) -> bool: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod()