Stair-climbing puzzle: Difference between revisions

→‎Tcl: Added implementation
(added python)
(→‎Tcl: Added implementation)
Line 73:
step_up2() # undo the fall
step_up2() # try again</lang>
 
=={{header|Tcl}}==
The setup (note that <code>level</code> and <code>steps</code> are not used elsewhere, but are great for testing…)
<lang tcl>set level 41
set prob 0.5001
proc step {} {
global level prob steps
incr steps
if {rand() < $prob} {
incr level 1
return 1
} else {
incr level -1
return 0
}
}</lang>
===Iterative Solution===
All iterative solutions require a counter variable, but at least we can avoid any literal digits...
<lang tcl>proc step-up-iter {} {
for {incr d} {$d} {incr d} {
incr d [set s -[step]]; incr d $s
}
}</lang>
===Recursive Solution===
This is the simplest possible recursive solution:
<lang tcl>proc step-up-rec {} {
while {![step]} step-up-rec
}</lang>
Anonymous user