From 022cc8eae6fc91ed4c67c04de41faeb4d91799ed Mon Sep 17 00:00:00 2001 From: Iheb Haboubi Date: Tue, 25 Aug 2020 11:18:18 +0100 Subject: [PATCH 1/3] Add perfect_square_binary_search --- maths/perfect_square.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/maths/perfect_square.py b/maths/perfect_square.py index 3e7a1c07a75f..3044b4de0006 100644 --- a/maths/perfect_square.py +++ b/maths/perfect_square.py @@ -21,6 +21,36 @@ def perfect_square(num: int) -> bool: return math.sqrt(num) * math.sqrt(num) == num +def perfect_square_binary_search(n: int) -> bool: + """ + Check if a number is perfect square using binary search. + Time complexity : O(Log(n)) + Space complexity: O(1) + + >>> perfect_square(9) + True + >>> perfect_square(16) + True + >>> perfect_square(1) + True + >>> perfect_square(0) + True + >>> perfect_square(10) + False + """ + left = 0 + right = n + while left <= right: + mid = (left + right) // 2 + if mid ** 2 == n: + return True + elif mid ** 2 > n: + right = mid - 1 + else: + left = mid + 1 + return False + + if __name__ == "__main__": import doctest From d95b2080ed31641dc52548c1b9bb85ef09f19b9a Mon Sep 17 00:00:00 2001 From: Iheb Haboubi Date: Tue, 25 Aug 2020 11:22:05 +0100 Subject: [PATCH 2/3] Update tests --- maths/perfect_square.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/perfect_square.py b/maths/perfect_square.py index 3044b4de0006..2f92e2194ad9 100644 --- a/maths/perfect_square.py +++ b/maths/perfect_square.py @@ -27,15 +27,15 @@ def perfect_square_binary_search(n: int) -> bool: Time complexity : O(Log(n)) Space complexity: O(1) - >>> perfect_square(9) + >>> perfect_square_binary_search(9) True - >>> perfect_square(16) + >>> perfect_square_binary_search(16) True - >>> perfect_square(1) + >>> perfect_square_binary_search(1) True - >>> perfect_square(0) + >>> perfect_square_binary_search(0) True - >>> perfect_square(10) + >>> perfect_square_binary_search(10) False """ left = 0 From f2ccef95746a9d28f983152e9c28ed568aa3266b Mon Sep 17 00:00:00 2001 From: Iheb Haboubi Date: Tue, 25 Aug 2020 19:11:51 +0100 Subject: [PATCH 3/3] Add tests --- maths/perfect_square.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/maths/perfect_square.py b/maths/perfect_square.py index 2f92e2194ad9..4393dcfbc774 100644 --- a/maths/perfect_square.py +++ b/maths/perfect_square.py @@ -37,6 +37,22 @@ def perfect_square_binary_search(n: int) -> bool: True >>> perfect_square_binary_search(10) False + >>> perfect_square_binary_search(-1) + False + >>> perfect_square_binary_search(1.1) + False + >>> perfect_square_binary_search("a") + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'str' + >>> perfect_square_binary_search(None) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'NoneType' + >>> perfect_square_binary_search([]) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'list' """ left = 0 right = n