Stair-climbing puzzle: Difference between revisions
Content deleted Content added
m Fixed lang tags. |
add Ruby |
||
Line 394: | Line 394: | ||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
|||
<lang ruby>def step_up |
|||
start_position = $position |
|||
step until ($position == start_position + 1) |
|||
end |
|||
# assumptions about the step function: |
|||
# - it maintains the current position of the robot "as a side effect" |
|||
# - the robot is equally likely to step back as to step up |
|||
def step |
|||
if rand < 0.5 |
|||
$position -= 1 |
|||
p "fall (#$position)" if $DEBUG |
|||
return false |
|||
else |
|||
$position += 1 |
|||
p "rise (#$position)" if $DEBUG |
|||
return true |
|||
end |
|||
end |
|||
$position = 0 |
|||
step_up</lang> |
|||
Sample run: |
|||
<pre>$ ruby -d stair.climbing.rb |
|||
"fall (-1)" |
|||
"rise (0)" |
|||
"fall (-1)" |
|||
"rise (0)" |
|||
"fall (-1)" |
|||
"fall (-2)" |
|||
"rise (-1)" |
|||
"rise (0)" |
|||
"rise (1)"</pre> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |