<titledata-rh="true">Recursion and backtracking with Robot Karel | mf</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://blog.mfocko.xyz/ib002/recursion/karel-1/"><metadata-rh="true"name="docusaurus_locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docusaurus_version"content="current"><metadata-rh="true"name="docusaurus_tag"content="docs-ib002-current"><metadata-rh="true"name="docsearch:version"content="current"><metadata-rh="true"name="docsearch:docusaurus_tag"content="docs-ib002-current"><metadata-rh="true"property="og:title"content="Recursion and backtracking with Robot Karel | mf"><metadata-rh="true"name="description"content="Aproblemwithtoomanyrestrictions.
resources. The point of this exercise is to show how powerful recursion and
backtracking can be even without anything else at your hand.</p><h2class="anchor anchorWithStickyNavbar_LWe7"id="your-environment-and-problem-description">Your environment and problem description<ahref="#your-environment-and-problem-description"class="hash-link"aria-label="Direct link to Your environment and problem description"title="Direct link to Your environment and problem description"></a></h2><h3class="anchor anchorWithStickyNavbar_LWe7"id="environment">Environment<ahref="#environment"class="hash-link"aria-label="Direct link to Environment"title="Direct link to Environment"></a></h3><p>You are given a robot that is present in a maze and is looking for an exit. Maze
consists of different walls and exit is marked with a single so-called “beeper”.</p><p>Walking into a wall results in a permanent damage of the robot.</p><h3class="anchor anchorWithStickyNavbar_LWe7"id="interface">Interface<ahref="#interface"class="hash-link"aria-label="Direct link to Interface"title="Direct link to Interface"></a></h3><p>You can control the robot using the following interface:</p><ul><li>actions — you can use them to change the current state of the robot and its
surroundings<ul><li><code>robot.step()</code> — moves robot one step further</li><li><code>robot.turn_left()</code> — turns robot 90-degrees counter-clockwise<ul><li>notice that you are not given <code>turn_right</code> or <code>turn_around</code>, but feel free
to implement them yourself</li></ul></li><li><code>robot.pick_beeper()</code> and opposite operation <code>robot.put_beeper()</code> — that
allows you to either pick or put “beeper” from or onto the current position</li></ul></li><li>queries — you can use them to check the current state of the robot and its
surroundings<ul><li><code>robot.beepers_present()</code> — to check if there are any beepers at the robot's
current location</li><li><code>robot.left_is_clear()</code> — to check if you can step to the left<ul><li>analogically for <code>front</code> and <code>right</code></li></ul></li></ul></li></ul><divclass="theme-admonition theme-admonition-caution alert alert--warning admonition_LlT9"><divclass="admonitionHeading_tbUL"><spanclass="admonitionIcon_kALy"><svgviewBox="0 0 16 16"><pathfill-rule="evenodd"d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>caution</div><divclass="admonitionContent_S0QG"><p>Helper functions / procedures are allowed. Return values are allowed.</p><p><strong>Variables are prohibited!</strong></p></div></div><h3class="anchor anchorWithStickyNavbar_LWe7"id="problem">Problem<ahref="#problem"class="hash-link"aria-label="Direct link to Problem"title="Direct link to Problem"></a></h3><p>Your task is to decide whether there is an exit from the maze or not. You can see
an example of a maze here:</p><p><imgloading="lazy"alt="Image of the maze"src="/assets/images/maze-a374d908bc9445061e15faeddc71641e.png"width="770"height="839"class="img_ev3q"></p><h2class="anchor anchorWithStickyNavbar_LWe7"id="simple-problem-to-get-familiar-with-the-robot">Simple problem to get familiar with the robot<ahref="#simple-problem-to-get-familiar-with-the-robot"class="hash-link"aria-label="Direct link to Simple problem to get familiar with the robot"title="Direct link to Simple problem to get familiar with the robot"></a></h2><p>If you feel completely lost after the previous description, let me start you off
with a simpler problem.</p><p>You are standing in front of the stairs, your task is to walk up the stairs.</p><p>You can see an example of such map here:</p><p><imgloading="lazy"alt="Image of the stairs"src="/assets/images/stairs-5ee5d03905645aeb13eeaa7774451a64.png"width="1058"height="1161"class="img_ev3q"></p><h2class="anchor anchorWithStickyNavbar_LWe7"id="brainstorm-the-idea">Brainstorm the idea<ahref="#brainstorm-the-idea"class="hash-link"aria-label="Direct link to Brainstorm the idea"title="Direct link to Brainstorm the idea"></a></h2><p>As a first step write down any ideas and things that you have noticed or came to
your mind. Ideally:</p><ul><li><p>Write down a nested list of the problems, e.g.</p><divclass="theme-admonition theme-admonition-info alert alert--info admonition_LlT9"><divclass="admonitionHeading_tbUL"><spanclass="admonitionIcon_kALy"><svgviewBox="0 0 14 16"><pathfill-rule="evenodd"d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>Example </div><divclass="admonitionContent_S0QG"><p>Problem: I want to find out whether the display on smartphone should rotate.</p><ul><li>Check if display has been rotated<ul><li>Read data from some sensor<ul><li>From what sensor</li></ul></li><li>In what format are the data I have read?</li><li>How do I communicate with the sensor?</li><li>What is the meaning of the data that I got?</li><li>How can I process it?</li></ul></li></ul></div></div></li><li><p>Write down list of problems that can happen, e.g.</p><divclass="theme-admonition theme-admonition-info alert alert--info admonition_LlT9"><divclass="admonitionHeading_tbUL"><spanclass="admonitionIcon_kALy"><svgviewBox="0 0 14 16"><pathfill-rule="evenodd"d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>Example continued</div><divclass="admonitionContent_S0QG"><p>Following the same problem.</p><ul><li>What if the sensor doesn't work?</li><li>What if the data doesn't conform to the specification?</li><li>What if my formulas are wrong?</li></ul></div></div></li><li><p>Write down <strong>anything</strong> you consider important to solving the problem, e.g.</p><divclass="theme-admonition theme-admonition-info alert alert--info admonition_LlT9"><divclass="admonitionHeading_tbUL"><spanclass="admonitionIcon_kALy"><svgviewBox="0 0 14 16"><pathfill-rule="evenodd"d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>Example continued once again</div><divclass="admonitionContent_S0QG"><ul><li>I could probably use gyroscope.</li><li>I should probably look up the datasheet for that module.</li><li>I could write some tests to verify that my computations are correct.</li></ul></div></div></li></ul><h2class="anchor anchorWithStickyNavbar_LWe7"id="rough-pseudocode">»Rough« pseudocode<ahref="#rough-pseudocode"class="hash-link"aria-label="Direct link to »Rough« pseudocode"title="Direct link to »Rough« pseudocode"></a></h2><p>As a next step write a <strong>mock up</strong> of a pseudocode solving the problem, you are
allowed to use comments as placeholders for bigger chunks of code.</p><p>Those comments are also a very good hints for decomposition and short, but
descriptive, commnets (if they are short enough and you decide not to factor them
out to separate functions).</p><divclass="theme-admonition theme-admonition-tip alert alert--success admonition_LlT9"><divclass="admonitionHeading_tbUL"><spanclass="admonitionIcon_kALy"><svgviewBox="0 0 12 16"><pathfill-rule="evenodd"d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><divclass="admonitionContent_S0QG"><p>The smaller the function is, the easier it is to test it and argue about its
correctness.</p></div></div><h2class="anchor anchorWithStickyNavbar_LWe7"id="proper-pseudocode">»Proper« pseudocode<ahref="#proper-pseudocode"class="hash-link"aria-label="Direct link to »Proper« pseudocode"title="Direct link to »Proper« pseudocode"></a></h2><p>If you are satisfied with the <em>»rough« pseudocode</em>, it's time to convert it into
a proper one. Get rid of the uncertain pieces of functionality and replace them
with proper pseudocode, i.e. list of the things that should happen in its place.</p><h2class="anchor anchorWithStickyNavbar_LWe7"id="library">Library<ahref="#library"class="hash-link"aria-label="Direct link to Library"title="Direct link to Library"></a></h2><p>If you got here, and you <strong>actually</strong> wrote down the pseudocode, you can try your
solution after downloading the sources linked at the beginning. If you download
the ZIP-file, you can there:</p><ul><li><p><code>generate_mazes.py</code> - that was used to generate the same maze with beepers in
different locations</p></li><li><p><code>karel_tk.py</code> - library which can run Karel given the his world</p><ul><li>documentation can be found <ahref="https://www.fi.muni.cz/~xfocko/ib111/10/docs/"target="_blank"rel="noopener noreferrer">here</a></li><li>also requires Tk Python library to be installed (it should be included in
majority of Python installations)</li></ul></li><li><p><code>*.kw</code> - which represent multiple worlds for Karel I have prepared</p></li><li><p><code>skeleton.py</code> - skeleton for your solution, needs to be put in the same directory
as <code>karel_tk.py</code> and takes path to the world as a first argument, example usage:</p><divclass="codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#000000;--prism-background-color:#ffffff"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-text codeBlock_bY9V thin-scrollbar"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ python3 skeleton.py stairs.kw</span><br></span></code></pre><divclass="buttonGroup__atx"><buttontype="button"aria-label="Copy code to clipboard"title="Copy"class="clean-btn"><spanclass="copyButtonIcons_eSgA"aria-hidden="true"><svgviewBox="0 0 24 24"class="copyButtonIcon_y97N"><pathfill="currentColor"d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svgviewBox="0 0 24 24"class="copyButtonSuccessIcon_LjdS"><pathfill="currentColor"d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><ul><li>of course, this file can be renamed ;)</li></ul></li></ul><h2class="anchor anchorWithStickyNavbar_LWe7"id="solution">Solution<ahref="#solution"class="hash-link"aria-label="Direct link to Solution"title="Direct link to Solution"></a></h2><p>Solution to this problem will be released as a second part, so that you can try
it out by yourself without any influence of “example solution”.</p><p>If you want to get any feedback, feel free to mail me your solution (including
all the steps that lead to your final solution, if you wish to get feedback on