-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrecursion_exercises_python.py
More file actions
195 lines (161 loc) · 4.25 KB
/
Copy pathrecursion_exercises_python.py
File metadata and controls
195 lines (161 loc) · 4.25 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
1. Write a Python program to calculate the sum of a list of numbers.
"""
def sum_of_list(my_list):
if not my_list:
return 0
elif len(my_list) == 1:
return my_list[0]
else:
return my_list[0] + sum_of_list(my_list[1:])
print(sum_of_list([1,2,3]))
"""
3. Write a Python program of recursion list sum.
Test Data: [1, 2, [3,4], [5,6]]
Expected Result: 21
"""
def sum_of_list(my_list):
if not my_list:
return 0
elif isinstance(my_list[0], list):
return sum_of_list(my_list[0]) + sum_of_list(my_list[1:])
elif len(my_list) == 1:
return my_list[0]
else:
return my_list[0] + sum_of_list(my_list[1:])
print(sum_of_list([1, 2, [3,4], [5,6]]))
"""
4. Write a Python program to get the factorial of a non-negative integer.
"""
def factorial(number):
if number == 0 or number == 1:
return 1
else:
return number * factorial(number-1)
print(factorial(2))
"""
5. Write a Python program to solve the Fibonacci sequence using recursion.
"""
solution 1: bad function that will work for a low n but will slow the
computation drastically as the n increases
def fibonacci(n):
if n == 1 or n == 2:
b = 1
if n > 2:
b = fibonacci(n-1) + fibonacci(n-2)
return b
for i in range(1, 21):
print(fibonacci(i), end = ' ')
print('\n')
# solution 2: with memoization
# explanation to this solution on Socratica youtube channel at https://youtu.be/Qk0zUZW-U_M
fibonacci_cache = {}
def fibonacci(n):
if n in fibonacci_cache:
return fibonacci_cache[n]
if n == 1 or n == 2:
b = 1
else:
b = fibonacci(n-1) + fibonacci(n-2)
fibonacci_cache[n] = b
return b
for i in range(1, 21):
print(fibonacci(i), end = ' ')
print('\n')
# solution 3: with memoize function
# more on this at https://www.python-course.eu/python3_memoization.php
def memoize(func):
memo_cache = {}
def helper(n):
if n not in memo_cache:
memo_cache[n] = func(n)
return memo_cache[n]
return helper
@memoize
def fibonacci(n):
if n == 1 or n == 2:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
for i in range(1, 21):
print(fibonacci(i), end = ' ')
"""
6. Write a Python program to get the sum of all digits of a non-negative integer.
Test Data:
sumDigits(345) -> 12
sumDigits(45) -> 9
"""
def sum_digits(n):
if n//10 < 1:
return n
else:
return n%10 + sum_digits(n//10)
print(sum_digits(345))
"""
7. Write a Python program to calculate the sum of the positive integers
of n+(n-2)+(n-4)... (until n-x =< 0). Go to the editor
Test Data:
sum_series(6) -> 12
sum_series(10) -> 30
"""
def sum_series(n):
if (n-2) <= 0:
return n
else:
return n + sum_series(n-2)
print(sum_series(6))
"""
8. Write a Python program to calculate the harmonic sum of n-1.
Note: The harmonic sum is the sum of reciprocals of the positive integers.
"""
def harmonic_sum(n):
if n == 1:
return 1
else:
return 1/n + harmonic_sum(n-1)
print(harmonic_sum(5))
"""
9. Write a Python program to calculate the geometric sum of n-1.
Note: In mathematics, a geometric series is a series with a constant
ratio between successive terms.
"""
def geometric_sum(n, a, r):
# r is a common ratio, a is a start term
if n == 0:
return a
else:
return a*r**n + geometric_sum(n-1, a, r)
print(geometric_sum(5, 1, 2))
"""
10. Write a Python program to calculate the value of 'a' to the power 'b'.
Test Data :
(power(3,4) -> 81
"""
def power(a,b):
if b == 0:
return 1
else:
return a*power(a, b-1)
print(power(3,4))
"""
11. Write a Python program to find the greatest common divisor (gcd) of two
integers.
"""
# solution 1 (without recursion)
def find_gcd(num1, num2):
for i in range(min(num1,num2), 0, -1):
if num1%i==0 and num2%i==0:
return i
print(find_gcd(12, 500))
# solution 2
# from w3resource at https://www.w3resource.com/python-exercises/data-structures-and-algorithms/python-recursion-exercise-11.php
def gcd(num1, num2):
low = min(num1, num2)
high = max(num1, num2)
if low == 0:
return high
elif low == 1:
return 1
else:
return gcd(low, high%low)
print(gcd(12,500))