CodeNewbie Community 🌱

Vishnubhotla Bharadwaj
Vishnubhotla Bharadwaj

Posted on • Originally published at bharadwaj.hashnode.dev on

Midpoint with Karel in Python

Yesterday, while I was trying to find the midpoint of a row with Karel in python, I got stuck at a place and immediately I googled for help and came to know that there is not code, blog, or at least a video explaining at solving my problem in Python. There are hundreds of codes explaining the same concept in java. You can ask me, why can't you convert the code into python by seeing java code. I can explain where I got the problem. Normally for finding the number of steps we use a variable to keep track of it like this:

while front_is_clear():
    move()
    count += 1

Enter fullscreen mode Exit fullscreen mode

We can simply write these three lines code in java. But, the problem with python is that we cannot simply write count to keep track of the distance we move(we cant initialize it). So, I came up with the solution of finding a midpoint with Karel in python. Before diving into the code, let's first get the concept for this problem.

Karel should place the beeper at the center of the 1st row. For example, say Karel starts in the 5x5 world pictured in the figure:

Screenshot (104).pngKarel should end with Karel standing on a beeper in the following position:

Screenshot (105).pngNote that the final configuration of the world should have only a single beeper at the midpoint of 1st Street. Along the way, Karel is allowed to place additional beepers wherever it wants to, but must pick them all up again before it finishes. Similarly, if Karel paints/colors any of the corners in the world, they must all be uncolored before Karel finishes.

In solving this problem, you may count on the following facts about the world:

  • Karel starts at the bottom left corner, facing east, with an infinite number of beepers in its bag.
  • The initial state of the world includes no interior walls or beepers.
  • The world need not be square, but you may assume that it is at least as tall as it is wide.

Your program, moreover, can assume the following simplifications:

  • If the width of the world is odd, Karel must put the beeper in the center square. If the width is even, Karel may drop the beeper on either of the two center squares.
  • It does not matter which direction Karel is facing at the end of the run.

Before writing the code we should make sure that it can run in any world(not depending on the boundaries). Now, its time for our code:

from karel.stanfordkarel import *

def turn_around():
    turn_left()
    turn_left()
def main():
    put_beeper()
    if front_is_clear():
        move()
        if front_is_clear():
            findMid()
            remove_all_beepers()
            middle()
        else:
            turn_around()
            middle()

def findMid():
    placebeeperwall()
    while no_beepers_present():
        is_it_mid()
        find_beeper()

def remove_all_beepers():
    pick_beeper_alg()
    turn_around()
    middle()
    pick_beeper_alg()
    turn_around()

def pick_beeper_alg():
    while front_is_clear():
        move()
        pick_beeper()

def middle():
    while no_beepers_present():
        move()

def placebeeperwall():
    while front_is_clear():
        move()
    put_beeper()
    turn_and_move()

def is_it_mid():
    move()
    if beepers_present():
        turn_and_move()
        put_beeper()

def find_beeper():
    if no_beepers_present():
        while no_beepers_present():
            move()
        turn_and_move()
        put_beeper()
        move()

def turn_and_move():
    turn_around()
    move()

if __name__ == ' __main__':
    run_karel_program('Midpoint.w')

Enter fullscreen mode Exit fullscreen mode

To make sure this runs in any world, change the last line of code in this way: Midpoint1.w, Midpoint2.w, Midpoint8.w.

The above code runs in any given world, and the proof for it is:

Screenshot (106).png

Screenshot (109).png

Latest comments (0)