CSC111 Lab 13 Solutions

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 15:14, 30 April 2010 (UTC)


Recursive Functions

# largest: Version 1
# 
def largest( A ):
   if len( A )==1:
      return A[0]
   return max( A[0], largest( A[1:] ) )

# largest: Version 2
def largest2( A ):
   if len(A)==1:
      return A[0]
   return max( largest( A[0:len(A)/2] ), largest( A[len(A)/2: ] ) )

def main():
    A = [ 1, 10, 2, 3, 5, 100, -1, -2, -4 ]
    print "largest Version 1 of ", A, " = ", largest( A )
    print "largest Version 2 of ", A, " = ", largest2( A )

main()