How to resolve the algorithm Stair-climbing puzzle step by step in the jq programming language
How to resolve the algorithm Stair-climbing puzzle step by step in the jq programming language
Table of Contents
Problem Statement
From Chung-Chieh Shan (LtU): Your stair-climbing robot has a very simple low-level API: the "step" function takes no argument and attempts to climb one step as a side effect. Unfortunately, sometimes the attempt fails and the robot clumsily falls one step instead. The "step" function detects what happens and returns a boolean flag: true on success, false on failure. Write a function "step_up" that climbs one step up [from the initial position] (by repeating "step" attempts if necessary). Assume that the robot is not already at the top of the stairs, and neither does it ever reach the bottom of the stairs. How small can you make "step_up"? Can you avoid using variables (even immutable ones) and numbers? Here's a pseudo-code of a simple recursive solution without using variables: Inductive proof that step_up() steps up one step, if it terminates:
The second (tail) recursion above can be turned into an iteration, as follows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Stair-climbing puzzle step by step in the jq programming language
Source code in the jq programming language
def tick: .+1;
def random: [0, 0, 0, 1, 0, 1, 1, 0];
def step:
random as $r
| if . >= ($r|length) then true else ($r[.] == 1) end ;
def step_up:
if step then tick
else tick | step_up | step_up
end;
0 | step_up
$ jq -n -f stair-climbing_puzzle.jq
11
def tco_step_up:
.[0] as $time | .[1] as $goal
| if $goal == 0 then $time
else
if $time|step then $goal - 1 else $goal + 1 end
| [ ($time|tick), .] | tco_step_up
end ;
[0,1] | tco_step_up
You may also check:How to resolve the algorithm Wireworld step by step in the Rust programming language
You may also check:How to resolve the algorithm Color wheel step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Zig programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the LFE programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the Go programming language