Skip to content

Commit a532ed6

Browse files
authored
Create recursion_exercises_python.py
1 parent 84b444a commit a532ed6

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

recursion_exercises_python.py

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
"""
2+
1. Write a Python program to calculate the sum of a list of numbers.
3+
"""
4+
5+
def sum_of_list(my_list):
6+
if not my_list:
7+
return 0
8+
elif len(my_list) == 1:
9+
return my_list[0]
10+
else:
11+
return my_list[0] + sum_of_list(my_list[1:])
12+
13+
print(sum_of_list([1,2,3]))
14+
15+
"""
16+
3. Write a Python program of recursion list sum.
17+
Test Data: [1, 2, [3,4], [5,6]]
18+
Expected Result: 21
19+
"""
20+
21+
def sum_of_list(my_list):
22+
if not my_list:
23+
return 0
24+
elif isinstance(my_list[0], list):
25+
return sum_of_list(my_list[0]) + sum_of_list(my_list[1:])
26+
elif len(my_list) == 1:
27+
return my_list[0]
28+
else:
29+
return my_list[0] + sum_of_list(my_list[1:])
30+
31+
print(sum_of_list([1, 2, [3,4], [5,6]]))
32+
33+
"""
34+
4. Write a Python program to get the factorial of a non-negative integer.
35+
"""
36+
37+
def factorial(number):
38+
if number == 0 or number == 1:
39+
return 1
40+
else:
41+
return number * factorial(number-1)
42+
43+
print(factorial(2))
44+
45+
"""
46+
5. Write a Python program to solve the Fibonacci sequence using recursion.
47+
"""
48+
49+
solution 1: bad function that will work for a low n but will slow the
50+
computation drastically as the n increases
51+
def fibonacci(n):
52+
if n == 1 or n == 2:
53+
b = 1
54+
if n > 2:
55+
b = fibonacci(n-1) + fibonacci(n-2)
56+
return b
57+
58+
for i in range(1, 21):
59+
print(fibonacci(i), end = ' ')
60+
print('\n')
61+
62+
# solution 2: with memoization
63+
# explanation to this solution on Socratica youtube channel at https://youtu.be/Qk0zUZW-U_M
64+
fibonacci_cache = {}
65+
def fibonacci(n):
66+
if n in fibonacci_cache:
67+
return fibonacci_cache[n]
68+
if n == 1 or n == 2:
69+
b = 1
70+
else:
71+
b = fibonacci(n-1) + fibonacci(n-2)
72+
fibonacci_cache[n] = b
73+
return b
74+
75+
for i in range(1, 21):
76+
print(fibonacci(i), end = ' ')
77+
print('\n')
78+
79+
# solution 3: with memoize function
80+
# more on this at https://www.python-course.eu/python3_memoization.php
81+
def memoize(func):
82+
memo_cache = {}
83+
def helper(n):
84+
if n not in memo_cache:
85+
memo_cache[n] = func(n)
86+
return memo_cache[n]
87+
return helper
88+
89+
@memoize
90+
def fibonacci(n):
91+
if n == 1 or n == 2:
92+
return 1
93+
else:
94+
return fibonacci(n-1) + fibonacci(n-2)
95+
96+
for i in range(1, 21):
97+
print(fibonacci(i), end = ' ')
98+
99+
"""
100+
6. Write a Python program to get the sum of all digits of a non-negative integer.
101+
Test Data:
102+
sumDigits(345) -> 12
103+
sumDigits(45) -> 9
104+
"""
105+
106+
def sum_digits(n):
107+
if n//10 < 1:
108+
return n
109+
else:
110+
return n%10 + sum_digits(n//10)
111+
112+
print(sum_digits(345))
113+
114+
"""
115+
7. Write a Python program to calculate the sum of the positive integers
116+
of n+(n-2)+(n-4)... (until n-x =< 0). Go to the editor
117+
Test Data:
118+
sum_series(6) -> 12
119+
sum_series(10) -> 30
120+
"""
121+
122+
def sum_series(n):
123+
if (n-2) <= 0:
124+
return n
125+
else:
126+
return n + sum_series(n-2)
127+
128+
print(sum_series(6))
129+
130+
"""
131+
8. Write a Python program to calculate the harmonic sum of n-1.
132+
Note: The harmonic sum is the sum of reciprocals of the positive integers.
133+
"""
134+
135+
def harmonic_sum(n):
136+
if n == 1:
137+
return 1
138+
else:
139+
return 1/n + harmonic_sum(n-1)
140+
141+
print(harmonic_sum(5))
142+
143+
"""
144+
9. Write a Python program to calculate the geometric sum of n-1.
145+
Note: In mathematics, a geometric series is a series with a constant
146+
ratio between successive terms.
147+
"""
148+
149+
def geometric_sum(n, a, r):
150+
# r is a common ratio, a is a start term
151+
if n == 0:
152+
return a
153+
else:
154+
return a*r**n + geometric_sum(n-1, a, r)
155+
print(geometric_sum(5, 1, 2))
156+
157+
"""
158+
10. Write a Python program to calculate the value of 'a' to the power 'b'.
159+
Test Data :
160+
(power(3,4) -> 81
161+
"""
162+
163+
def power(a,b):
164+
if b == 0:
165+
return 1
166+
else:
167+
return a*power(a, b-1)
168+
169+
print(power(3,4))
170+
171+
"""
172+
11. Write a Python program to find the greatest common divisor (gcd) of two
173+
integers.
174+
"""
175+
176+
# solution 1 (without recursion)
177+
def find_gcd(num1, num2):
178+
for i in range(min(num1,num2), 0, -1):
179+
if num1%i==0 and num2%i==0:
180+
return i
181+
print(find_gcd(12, 500))
182+
183+
# solution 2
184+
# from w3resource at https://www.w3resource.com/python-exercises/data-structures-and-algorithms/python-recursion-exercise-11.php
185+
def gcd(num1, num2):
186+
low = min(num1, num2)
187+
high = max(num1, num2)
188+
189+
if low == 0:
190+
return high
191+
elif low == 1:
192+
return 1
193+
else:
194+
return gcd(low, high%low)
195+
print(gcd(12,500))

0 commit comments

Comments
 (0)