-
Notifications
You must be signed in to change notification settings - Fork 17
/
valid-perfect-square.py
53 lines (43 loc) · 1.53 KB
/
valid-perfect-square.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import unittest
# TODO: In this file I used the dumbest method after bruteforce. Have to
# learn to implement another methods
class Solution:
def find_sqrt(self, num, left=0, right=None):
if right is None:
right = num
mid = int((right + left) / 2)
if mid ** 2 == num:
return True
elif mid == left or mid < 2:
return False
else:
if mid ** 2 > num:
right = mid
else:
left = mid
return self.find_sqrt(num, left=left, right=right)
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 2:
return True
return self.find_sqrt(num, left=0, right=num)
class TestSolutions(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_sol(self):
self.assertTrue(self.sol.isPerfectSquare(0))
self.assertTrue(self.sol.isPerfectSquare(1))
self.assertFalse(self.sol.isPerfectSquare(2))
self.assertFalse(self.sol.isPerfectSquare(3))
self.assertTrue(self.sol.isPerfectSquare(4))
self.assertFalse(self.sol.isPerfectSquare(5))
self.assertFalse(self.sol.isPerfectSquare(14))
self.assertTrue(self.sol.isPerfectSquare(16))
self.assertFalse(self.sol.isPerfectSquare(17))
self.assertFalse(self.sol.isPerfectSquare(19))
self.assertFalse(self.sol.isPerfectSquare(21))
self.assertTrue(self.sol.isPerfectSquare(25))
unittest.main()