In [10]:
def factorial(n):
    if n < 0:
        return None
    elif n <= 1:
        return 1
    else:
        return n*factorial(n-1)
In [11]:
print(factorial(4))
24
In [12]:
print(factorial(0))
1
In [13]:
print(factorial(-3))
None
In [14]:
def sum_list(a):
    if len(a) == 0:
        return 0
    else:
        return a[0]+sum_list(a[1:])
In [15]:
print(sum_list([1,2,3,4,5]))
15
In [16]:
print(sum_list([]))
0
In [17]:
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:]))
In [18]:
print(max_list([1,2,3,4,5]))
5
In [22]:
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)
In [23]:
print(fibonacci(6))
8
In [24]:
for i in range(7):
    print(fibonacci(i))
None
1
1
2
3
5
8
In [28]:
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)
        
In [29]:
towers_of_hanoi(3,"Pole 1","Pole 2","Pole 3")
Move disc from  Pole 1  to  Pole 3
Move disc from  Pole 1  to  Pole 2
Move disc from  Pole 3  to  Pole 2
Move disc from  Pole 1  to  Pole 3
Move disc from  Pole 2  to  Pole 1
Move disc from  Pole 2  to  Pole 3
Move disc from  Pole 1  to  Pole 3
In [40]:
def palindrome(s):
    if len(s) <= 1:
        return True
    else:
        return s[0]==s[len(s)-1] and palindrome(s[1:len(s)-1])
In [41]:
print(palindrome("madam"))
True
In [42]:
print(palindrome("ma"))
False
In [43]:
print(palindrome("maddam"))
True
In [ ]: