Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
175 views

11+ Python Recursion Practice Problems With Solutions - Python Mania

This document provides solutions to 7 recursion problems in Python including calculating the factorial, Fibonacci sequence, greatest common divisor, sum of a list, checking for palindromes, finding the minimum value in a list, and calculating power.

Uploaded by

Samy Bouria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

11+ Python Recursion Practice Problems With Solutions - Python Mania

This document provides solutions to 7 recursion problems in Python including calculating the factorial, Fibonacci sequence, greatest common divisor, sum of a list, checking for palindromes, finding the minimum value in a list, and calculating power.

Uploaded by

Samy Bouria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

 Functions

11+ Python Recursion Practice Problems With


Solutions

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

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

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

You can verify that 2 raised to power 3 is equal to 8.

Was this helpful? Yes No

Related Articles:

Python Program To Reverse a Number (3 Ways)

While Loop in Python (With Examples)

Solved: Python Time Data Does Not Match Format

How to Use Celery in Django? (Ultimate Guide + Case Study)

How To Comment Out Multiple Lines In Python

Python Program For Linear Search (With Code & Explanation)

How to Use Scrapy in Python? (Ultimate Guide + Case Study)

Python Program for Matrix Addition with User Input (With Code)

Web Scraping Real Estate Data Python (With Example)

Python Program to Calculate the Number of Days Between Two Dates

Python Program To Calculate Body Mass Index

Previous Tutorial: Next Tutorial:


 
Understanding Recursion in Python in an Easy … Logical Operators in Python (With Examples):

Recent Articles:

Python Program to Convert Integer to Roman


Python Program to Convert Miles to Kilometers

Python Program to Convert Fahrenheit to Celsius

Python Program to Convert Binary to Decimal

Python Program to Convert Decimal to Binary

Python Program to Convert Kilometers to Miles

Python Program to Convert Celsius to Fahrenheit

Python Program to Print Hello World: A Beginner’s Guide

Python Program to Print Prime Numbers

Python Program to Add Two Numbers Without Addition Operator

Python Program to Add Two Numbers with User Input

0
Article Rating

Be the First to Comment!

0 COMMENTS
Related Tutorials:

Python Fundamentals: Learn the basics of python programming.

Control Flow: Learn about Conditional Statements and Loops

Functions: Learn Modular Programming from Basics!

OOP: The Art of Organizing Code for maximum reusability.

PYTHON MANIA

You might also like