-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquareRoot.py
34 lines (31 loc) · 1.13 KB
/
SquareRoot.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
def binary_search(square):
if type(square) == str:
return "invalid input"
if square < 0:
return "error!: Invalid input"
mid = square / 2
if (mid * mid) == square:
result = mid
elif (mid * mid) > square:
result = helper(square, 0, mid)
elif (mid * mid) < square:
result = helper(square, mid, square)
return int(result)
def helper(square, low, high):
mid = (low + high) / 2
if (mid * mid) >= (square) and (mid * mid) < square + 1:
return mid
elif (mid * mid) > square:
# change range of low and high
# change the high
# the mid will become the new high, because mid * mid is too high to be the answer
return helper(square, low, mid)
elif (mid * mid) < square:
# mid is too low, so it will become the new low.
return helper(square, mid, high)
print(binary_search(-1)) # edge case: expect error message
print(binary_search("s")) # edge case: invalid input message
print(binary_search(3)) # expect 1
print(binary_search(4)) # expect 2
print(binary_search(25)) # expect 5
print(binary_search(27)) # expect 5