Advertisement
brilliant_moves

RecursiveMaxFinder.py

Apr 5th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. #RecursiveMaxFinder.py
  2.  
  3. #recursive function finds highest integer in list
  4. def findMax(li):
  5.  
  6.     #if only 1 item in list, return it
  7.     if len (li)==1: return li[0]
  8.  
  9.     #return the greater of (firstlistitem, maximumoftheotherlistitems)
  10.     return max (li[0], findMax (li[1:]))
  11.  
  12. def main():
  13.     a_list = [ 8, 3, 4, 5, 7, 1, 2, 6, 9 ]          # test values: try different values, e.g. remove 9
  14.     print ("List: %s" % a_list)                     # print list
  15.     print ("Highest number: %d" % findMax (a_list)) # print output of function findMax()
  16.  
  17. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement