Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. import sys
  2.  
  3. def jumps(elements):
  4.     current = elements[0]
  5.     remaining = len(elements)
  6.     if current == 0:        # base case: can't jump from 0
  7.         return sys.maxint
  8.     elif remaining == 1:    # base case: reached end
  9.         return 0
  10.     else:                   # recursive case
  11.         js = range(1, min(current+1, remaining))
  12.         candidates = map(lambda j: jumps(elements[j:]), js)
  13.         return 1 + min(candidates)
  14.  
  15. print jumps([0]) == sys.maxint
  16. print jumps([9]) == 0
  17. print jumps([1,3,5,8,9,2,6,7,6,8,9]) == 3
  18. print jumps([1,3,5,0,1,9,1,1,1,1,1,1,1]) == 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement