This tutorial will cover some Python Recursion Practice Problems With Solutions.
Python Recursion Problem 1
Write a Python Program to Find the Factorial of a Number using Recursion.
Solution
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
#Now you have to call the function
#you can choose any value of n, let say 5
fact = factorial(5)
print(fact)
Output
120
Python Recursion Problem 2
Write a Python Program to Compute the Fibonacci sequence with Recursion.
Solution
def reverse_string(s):
if len(s) == 0:
return s
else:
return reverse_string(s[1:]) + s[0]
#Pass any string to the function and print the output
str = reverse_string("AINAM NOHTYP")
print(str)
Output
PYTHON MANIA
Python Recursion Problem 3
The greatest common divisor (GCD) of two numbers is the largest number that divides both of them without leaving a remainder.
Here’s a recursive function that finds the GCD of two numbers using the Euclidean algorithm:
Solution
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
a = 32
b = 120
result = gcd(a, b)
print(result)
Output
8
Python Recursion Problem 4
Write a Python Program to Calculate the Sum of a list with Recursion
Solution
def sum_list(lst):
if len(lst) == 0:
return 0
else:
return lst[0] + sum_list(lst[1:])
#pass a list into the function
#it will return the sum of the list
#then you can use the sum in code or simply print it
sum = sum_list([2,3,4,5,9,2,4,5])
print(sum)
Output
34
Python Recursion Problem 5
Python program to check if the string is palindrome or not with Recursion
Solution
Here’s a recursive function that checks whether a given string is a palindrome (i.e., reads the same backward as forward):
def is_palindrome(s):
if len(s) <= 1:
return True
else:
return s[0] == s[-1] and is_palindrome(s[1:-1])
#This function will check if a string is palindrome or not
#Now you have to pass any string and check the output
#Lets pass Panama as a string
result = is_palindrome("543212345")
print(result)
Output
True
The output true represent that the string is a palindrome
Python Recursion Problem 6
Write a Python Program to Find the Minimum Value in a List Using Recursion.
Solution
Here’s a recursive function that takes a list of numbers and returns the smallest value:
def min_value(lst):
if len(lst) == 1:
return lst[0]
else:
return min(lst[0], min_value(lst[1:]))
#Pass any string to this function
#It will return the minimum entry of the list
#Let's pass a string and check whether its working or not
result = min_value([99, 24, 89, 3, 987, 35])
print(result)
Output
3
You can see, 3 is the minimum number that is present in the list
Python Recursion Problem 7
Write a Python Program to Calculate the Power of a Number with Recursion
Solution
Here’s a recursive function that calculates the result of raising a number to a given power:
def power(base, exponent):
if exponent == 0:
return 1
elif exponent % 2 == 0:
return power(base, exponent/2)**2
else:
return base * power(base, exponent-1)
#Just pass base and exponent as an argument to this function
#And it will find the power
#Now let's pass 2 as base and 3 as an exponent
base = 2
exponent = 3
result = power(base, exponent)
print(result)
Output
8
You can verify that 2 raised to power 3 is equal to 8.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.