Stair-climbing puzzle: Difference between revisions

Content added Content deleted
(Add Chuhg-chieh Shan's stair-climbing problem)
 
(added python)
Line 50: Line 50:
true))
true))
</lang>
</lang>

=={{header|Python}}==

=== Iterative ===
<lang python>def step_up1()
"Straightforward implementation: keep track of how many level we
need to ascend, and stop when this count is zero."
deficit = 1
while deficit > 0:
if step():
deficit -= 1
else:
deficit += 1</lang>

=== Recursive ===
This satisfies Chung-chieh's challenge to avoid using numbers. Might blow the stack as
''p'' approaches 0.5.

<lang python>def step_up2():
"No numbers."
if not step():
step_up2() # undo the fall
step_up2() # try again</lang>