20 Python Recursion Practice Questions

  1. Write a Python function to calculate the factorial of a number using recursion.

     def factorial(n):
     if n == 0:
     return 1
     else:
     return n * factorial(n-1)
    
  2. Write a Python function to reverse a string using recursion.

     def reverse_string(string):
     if len(string) == 0:
     return string
     else:
     return reverse_string(string[1:]) + string[0]
    
  3. Write a Python function to calculate the sum of a list of numbers using recursion.

     def sum_list(numbers):
     if len(numbers) == 0:
     return 0
     else:
     return numbers[0] + sum_list(numbers[1:])
    
  4. Write a Python function to check if a number is a palindrome using recursion.

     def is_palindrome(n):
     if n < 0:
     n = -n
     if n < 10:
     return True
     else:
     return str(n)[0] == str(n)[-1] and is_palindrome(int(str(n)[1:-1]))
    
  5. Write a Python function to find the largest element in a list using recursion.

     def find_largest(numbers):
     if len(numbers) == 1:
     return numbers[0]
     else:
     largest = find_largest(numbers[1:])
     return numbers[0] if numbers[0] > largest else largest
    
  6. Write a Python function to find the length of a list using recursion.

     def find_length(lst):
     if lst == []:
     return 0
     else:
     return 1 + find_length(lst[1:])
    
  7. Write a Python function to check if a number is even using recursion.

     def is_even(n):
     if n == 0:
     return True
     elif n == 1:
     return False
     else:
     return is_even(n-2)
    
  8. Write a Python function to find the nth element in the Fibonacci sequence using recursion.

     def fibonacci(n):
     if n == 0:
     return 0
     elif n == 1:
     return 1
     else:
     return fibonacci(n-1) + fibonacci(n-2)
    
  9. Write a Python function to check if a word is a palindrome using recursion.

     def is_palindrome(word):
     if len(word) < 2:
     return True
     else:
     return word[0] == word[-1] and is_palindrome(word[1:-1])
    
  10. Write a Python function to find the gcd of two numbers using recursion.

    def gcd(a, b):
    if b == 0:
    return a
    else:
    return gcd(b, a % b)
    
  11. Write a Python function to find the lcm of two numbers using recursion.

    def lcm(a, b):
    return (a * b) // gcd(a, b)
    
  12. Write a Python function to check if a string is a palindrome using recursion.

    def is_palindrome(string):
    if len(string) < 2:
    return True
    else:
    return string[0] == string[-1] and is_palindrome(string[1:-1])
    
  13. Write a Python function to find the nth element of the Pascal's triangle using recursion.

    def pascal_triangle(n, row, col):
    if col == 0 or col == row:
    return 1
    else:
    return pascal_triangle(n-1, row-1, col-1) + pascal_triangle(n-1, row-1, col)
    
  14. Write a Python function to find the sum of the digits of a number using recursion.

    def sum_digits(n):
    if n < 10:
    return n
    else:
    return n % 10 + sum_digits(n // 10)
    
  15. Write a Python function to find the power of a number using recursion.

    def power(base, exponent):
    if exponent == 0:
    return 1
    else:
    return base * power(base, exponent-1)
    
  16. Write a Python function to find the nth term of the harmonic series using recursion.

    def harmonic_series(n):
    if n == 1:
    return 1
    else:
    return 1/n + harmonic_series(n-1)
    
  17. Write a Python function to find the sum of the elements in a matrix using recursion.

    def sum_matrix(matrix):
    if matrix == []:
    return 0
    else:
    return sum(matrix[0]) + sum_matrix(matrix[1:])
    
  18. Write a Python function to find the sum of the digits of a string using recursion.

    def sum_digits(string):
    if string == '':
    return 0
    else:
    return int(string[0]) + sum_digits(string[1:])
    
  19. Write a Python function to find the sum of the elements in a 2D list using recursion.

    def sum_2d_list(lst):
    if lst == []:
    return 0
    else:
    return sum(lst[0]) + sum_2d_list(lst[1:])
    
  20. Write a Python function to find the sum of the elements in a nested list using recursion.

    def sum_nested_list(lst):
    if lst == []:
    return 0
    else:
    return sum(lst[0]) + sum_nested_list(lst[1:])
    

    Conclusion

    Hopefully, this was challenging!