MAX_STEP = 3 STONES = [1, -1, 0, 2, 3, 5] def can_jump(pos=0, cur_time=0): for steps in xrange(1, MAX_STEP+ 1): next_pos = pos + steps # can we jump to the end? if next_pos >= len(STONES): return True if STONES[next_pos] <= cur_time or STONES[next_pos] == 0: # do we have a future successful path? has_next_step = can_jump(next_pos, cur_time) if has_next_step: return True return False for t in xrange(1, max(STONES)+1): if can_jump(0, t): print "Can jump on: %d" % (t,) else: print "Cant jump on: %d" % (t,)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question