Posted By

emptystacks on 10/22/11


Tagged


Versions (?)

Lab8 - Q0


 / Published in: Python
 

  1. # Question 0 - Iterative Version
  2. def contains_iterative(n, l):
  3. """Returns True if n is in rlist l and False otherwise.
  4.  
  5. >>> foo_lst = make_rlist(1, make_rlist(2, make_rlist(3, empty_rlist)))
  6. >>> contains_iterative(5, foo_lst)
  7. False
  8. >>> contains_iterative(1, foo_lst)
  9. True
  10. """
  11. found = False
  12. while (not rest(l) == None) and (not found):
  13. if first(l) == n:
  14. found = True
  15. else:
  16. l = rest(l)
  17. return found

Report this snippet  

You need to login to post a comment.