1def rec(num):
2 if num <= 1:
3 return 1
4 else:
5 return num + rec(num - 1)
6
7print(rec(50))
1# Reccursion in python
2def recursive_method(n):
3 if n == 1:
4 return 1
5 else:
6 return n * recursive_method(n-1)
7 # 5 * factorial_recursive(4)
8 # 5 * 4 * factorial_recursive(3)
9 # 5 * 4 * 3 * factorial_recursive(2)
10 # 5 * 4 * 3 * 2 * factorial_recursive(1)
11 # 5 * 4 * 3 * 2 * 1 = 120
12num = int(input('enter num '))
13print(recursive_method(num))
14
15
1# Recursive function factorial_recursion()
2
3def factorial_recursion(n):
4 if n == 1:
5 return n
6 else:
7 return n*factorial_recursion(n-1)
8
1houses = ["Eric's house", "Kenny's house", "Kyle's house", "Stan's house"]
2
3# Each function call represents an elf doing his work
4def deliver_presents_recursively(houses):
5 # Worker elf doing his work
6 if len(houses) == 1:
7 house = houses[0]
8 print("Delivering presents to", house)
9
10 # Manager elf doing his work
11 else:
12 mid = len(houses) // 2
13 first_half = houses[:mid]
14 second_half = houses[mid:]
15
16 # Divides his work among two elves
17 deliver_presents_recursively(first_half)
18 deliver_presents_recursively(second_half)
19
1# Recursive Factorial Example
2# input: 5
3# output: 120 (5*4*3*2*1)
4def factorial(x):
5 if x == 1:
6 return 1
7 else:
8 return (x * factorial(x-1))