def factorial(n):
if n < 0:
return None
elif n <= 1:
return 1
else:
return n*factorial(n-1)
print(factorial(4))
print(factorial(0))
print(factorial(-3))
def sum_list(a):
if len(a) == 0:
return 0
else:
return a[0]+sum_list(a[1:])
print(sum_list([1,2,3,4,5]))
print(sum_list([]))
def max_list(a):
if len(a) == 0:
return None
elif len(a) == 1:
return a[0]
else:
return max(a[0],max_list(a[1:]))
print(max_list([1,2,3,4,5]))
def fibonacci(n):
if n <= 0:
return None
elif n == 1:
return 1
elif n == 2:
return 1
else:
return fibonacci(n-1)+fibonacci(n-2)
print(fibonacci(6))
for i in range(7):
print(fibonacci(i))
def towers_of_hanoi(n,source,temp,destination):
if n > 0:
towers_of_hanoi(n-1,source,destination,temp)
print("Move disc from ",source," to ",destination)
towers_of_hanoi(n-1,temp,source,destination)
towers_of_hanoi(3,"Pole 1","Pole 2","Pole 3")
def palindrome(s):
if len(s) <= 1:
return True
else:
return s[0]==s[len(s)-1] and palindrome(s[1:len(s)-1])
print(palindrome("madam"))
print(palindrome("ma"))
print(palindrome("maddam"))