-
Notifications
You must be signed in to change notification settings - Fork 13
/
recursion.py
87 lines (62 loc) · 1.53 KB
/
recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#### Russian Doll recursive function ###
def openRussianDoll(doll):
if doll == 1:
print("All dolls are opened")
else:
openRussianDoll(doll-1)
openRussianDoll(4)
# def recursionMethod(parameters):
# if exit from condition satisfied:
# return some value
# else:
# recursionMethod(modified parameters)
def firstMethod():
secondMethod()
print("I am the first Method")
def secondMethod():
thirdMethod()
print("I am the second Method")
def thirdMethod():
fourthMethod()
print("I am the third Method")
def fourthMethod():
print("I am the fourth Method")
firstMethod()
def recursiveMethod(n):
if n<1:
print("n is less than 1")
else:
recursiveMethod(n-1)
print(n)
recursiveMethod(4)
## Recursion vs Iterarion###
def powerOfTwo(n):
if n == 0:
return 1
else:
power = powerOfTwo(n-1)
return power * 2
print(powerOfTwo(3))
def powerOfTwoIt(n):
i = 0
power = 1
while i < n:
power = power * 2
i = i + 1
return power
print(powerOfTwoIt(4))
## Factorial###
def factorial(n):
assert n >= 0 and int(n) == n, 'The number must be positive integer only!'
if n in [0,1]:
return 1
else:
return n * factorial(n-1)
## Fibonacci###
def fibonacci(n):
assert n >=0 and int(n) == n , 'Fibonacci number cannot be negative number or non integer.'
if n in [0,1]:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(7))