In [ ]:
import zombit_recursion
In [ ]:
str_S1 = "7"
a1 = answer(str_S1)
print(a1)
if a1 == "4":
print("passed test 1")
else:
print("failed test 1")
str_S2 = "100"
if answer(str_S2) == "None":
print("passed test 2")
else:
print("failed test 2")
In [ ]:
from timeit import timeit
In [ ]:
%timeit answer("10000000000")
In [ ]:
def rec(num):
def R(m, d):
if m in d:
return d[m]
else:
if m % 2 == 0:
n = long(m / 2)
Rm = R(n, d) + R(n+1, d) + n
else:
n = long((m - 1) / 2)
Rm = R(n-1, d) + R(n, d) + 1
d[m] = Rm
return Rm
R_dict = dict()
R_dict[0] = 1
R_dict[1] = 1
R_dict[2] = 2
return R(num, R_dict)
In [ ]:
def test_answer(n):
S_n = str(rec(n)) # num of bunnies at n
n_out = answer(S_n) #
print(n)
if n_out == str(n):
print("test passed")
else:
S_n_out = str(rec(int(n_out)))
print("test failed")
print(S_n)
print(n_out)
print(S_n_out)
In [ ]:
for i in range(50):
test_answer(i)
In [ ]:
def simple(m):
if m == 0:
return 1
elif m == 1:
return 1
elif m == 2:
return 2
elif m % 2 == 0:
n = int(m / 2)
return R(n) + R(n+1) + n
else:
n = int((m - 1) / 2)
return R(n-1) + R(n) + 1
In [ ]:
# non-function loop
diff = []
R = [1,] # 0
R.append(1) # 1
R.append(2) # 2
R.append(R[0]+R[1]+1)
n = 2
while n < 51:
R.append(R[n]+R[n+1]+n)
print("{0}\t{1}".format(2*n,R[-1]))
R.append(R[n-1]+R[n]+1)
diff.append(R[-1]-R[-3])
print("{0}\t{1}".format(2*n+1,R[-1]))
n += 1
In [ ]:
from matplotlib import pyplot as plt
%matplotlib inline
#print(R.filter(lambda x: x%2==0, input_list))
#plt.plot(range(0,2*n,2),R[::2])
#plt.plot(range(1,2*n,2),R[1::2])
#plt.plot(range(2*n),R)
print(len(diff))
plt.plot(range(len(diff)),diff)
print(R[-1]/n)
In [ ]:
In [ ]:
alist = list(range(0,100,2))
lookfor = 18
lower = 0
upper = len(alist)-1
found = False
index = 0
while lower <= upper - 2: # and not odd_found: # loop until match found then break
middle = long((lower+upper)//2)
if middle % 2 == 0:
middle += 1
if alist[middle] == lookfor:
index = middle
break
if lookfor < alist[middle]:
upper = middle - 1
else:
lower = middle + 1
print(index)