basic recursion problems in python code example
Example 1: python recursion example
# Recursive Factorial Example
# input: 5
# output: 120 (5*4*3*2*1)
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
Example 2: recursion python problems
def productOfArray(arr): if len(arr) == 0: return 0 if len(arr) == 1: return arr[0] else: return arr[len(arr)-1] * productOfArray(arr[:len(arr)-1])
Example 3: recursion python problems
def isPalindrom(strng): if len(strng) == 0: return True if strng[0] != strng[len(strng)-1]: return False return isPalindrome(strng[1:-1])