How to resolve the algorithm Stair-climbing puzzle step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Stair-climbing puzzle step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

fcn step{  } // add code to return Bool
fcn stepUp{ while(not step()){ self.fcn() } }

var position=0;
fcn step(){  //-->0|1
   r:=(0).random(2);	// 0 or 1
   if(r) println("Climbed up to ",position+=1);
   else  println("Fell down to ", position-=1);
   r
}
stepUp();

  

You may also check:How to resolve the algorithm Combinations and permutations step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Plain English programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Rapira programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the VBA programming language