recursion.py: Examples of recursive functions
print 78*'=','\n', 'Recursion by Example', '\n', 78*'-'
print
def printer(lst):
if lst:
print lst[0]
printer(lst[1:])
print 'printer():'
print 'The items in [1,2,3,4] are:'
printer([1,2,3,4])
print
def factorial(n):
product = 1
for value in range(1, n+1):
product = product * value
return product
print 'factorial() implemented using explicit iteration:'
print '4! = ', factorial(4)
print '5! = ', factorial(5)
print
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
print 'factorial() implemented using recursion:'
print '4! = ', factorial(4)
print '5! = ', factorial(5)
print
def bin_search(lst, target):
if not lst:
return False
elif lst[len(lst)/2] == target:
return True
elif lst[len(lst)/2] > target:
return bin_search(lst[0:len(lst)/2], target)
else:
return bin_search(lst[len(lst)/2+1:], target)
print 'bin_search():'
print '3 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 3)
print '5 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 5)
print '1 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 1)
print '15 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 15)
print '2 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 2)
print '6 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 6)
print '4 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 4)
print '0 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 0)
print '20 in [1,3,5,7,9,11,13,15]?', bin_search([1,3,5,7,9,11,13,15], 20)
print
def r_sum(lst):
''' returns the sum of all the numbers in lst and
its sublists.
N.B. This is quite difficult to do without recursion.'''
sum = 0
for item in lst:
if type(item) == type([]):
sum = sum + r_sum(item)
else:
sum = sum + item
return sum
print'r_sum():'
lst = [ 1, 2, 3, [6, 7], 9, [2, [8, 9], 4] ]
print 'The sum of [ 1, 2, 3, [6, 7], 9, [2, [8, 9], 4] ] is', r_sum(lst)
print 'The sum of [2, [[100, 7], 90], [1, 13], 8, 6] is', r_sum([2, [[100, 7], 90], [1, 13], 8, 6])
print 'The sum of [2, [[13, 7], 90], [1, 100], 8, 6] is', r_sum([2, [[13, 7], 90], [1, 100], 8, 6])
print 'The sum of [[[13, 7], 90], 2, [1, 100], 8, 6] is', r_sum([[[13, 7], 90], 2, [1, 100], 8, 6])
print
print 78*'-','\n', 'S U M M A R Y', '\n', 78*'-'
print '''
Recursion SYNTAX = a function calls itself.
Recursion FUNCTIONALITY = another way of iterating.
TWO PARTS of a recursive function definition:
BASE CASE or stopping condition (without this you have an infinite loop)
how to move one STEP CLOSER to solution
'''