40 lines
946 B
Python
40 lines
946 B
Python
def recsum(n):
|
|
if not n:
|
|
return 0
|
|
else:
|
|
return recsum(n-1)+n
|
|
|
|
print(recsum(10))
|
|
|
|
def boardlength(n):
|
|
if n<=3:
|
|
return 0
|
|
else:
|
|
return boardlength(n-4)+1
|
|
|
|
print(boardlength(16))
|
|
|
|
def reversing(list_n):
|
|
if len(list_n) == 1:
|
|
return list_n
|
|
else:
|
|
first_half = list_n[:len(list_n)//2]
|
|
second_half = list_n[len(list_n)//2:]
|
|
return reversing(second_half) + reversing(first_half)
|
|
|
|
print(reversing([1,2,3,4]))
|
|
|
|
def binarySearch(list1,element):
|
|
print(list1,element,len(list1))
|
|
if list1[len(list1)//2]==element:
|
|
return len(list1)//2
|
|
|
|
elif list1[len(list1)//2] < element:
|
|
return binarySearch(list1[len(list1)//2:],element)
|
|
elif list1[len(list1)//2] > element:
|
|
return binarySearch(list1[:len(list1)//2],element)
|
|
|
|
elif len(list1) == 1 and list1[len(list1)//2] != element:
|
|
return None
|
|
|
|
print(binarySearch([1,2,3,4,5],5)) |