forked from adisayhi27/Hackerrank-SI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinding-cube-root.py
47 lines (44 loc) · 1.16 KB
/
finding-cube-root.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
import math
def cubeRoot(n,lo,hi):
while(lo < hi):
mid = (lo+hi)//2
if mid * mid * mid == n :
return mid
elif mid * mid * mid < n :
return cubeRoot(n,mid,hi)
else:
return cubeRoot(n,lo,mid)
def cubeRootNegative(n, lo, hi):
while (lo < hi):
mid = (lo + hi) // 2
if mid * mid * mid == n:
return mid
elif mid * mid * mid < n:
return cubeRootNegative(n, mid, hi)
else:
return cubeRootNegative(n, lo, mid)
if __name__ == "__main__":
m=int(input())
for i in range(m):
n=int(input())
# if n>0:
# lo = 0
# hi = n
# print(cubeRoot(n,lo,hi))
# else:
# lo = n
# hi = 0
# print(cubeRootNegative(n, lo, hi))
# #print(-int(math.pow(abs(n), float(1) / 3))-1)
if n==1:
print("1")
elif n==-1:
print("-1")
elif n>1:
lo = 0
hi = n
print(cubeRoot(n,lo,hi))
elif n <-1:
lo = n
hi = 0
print(cubeRootNegative(n, lo, hi))