September 20th, 2020
Based on handouts by Eric Roberts and Marty Stepp and converted to Python by Brahm Capoor
def main():
while front_is_clear():
if beepers_present():
build_hospital()
if front_is_clear():
move() # Karel needs to be facing East
def build_hospital():
"""
Precondition: Karel is on a beeper representing supplies for a hospital
Postcondition: Karel is facing East
"""
pick_beeper()
back_up()
turn_left()
place_three_beepers()
transition_to_next_column()
place_three_beepers()
turn_around()
transition_to_next_column()
place_three_beepers()
turn_left()
def place_three_beepers():
"""
Creates a line of three beepers
Precondition: Karel is in the first square in the line
Postcondition: Karel is in the last square in the line
"""
for i in range(2):
put_beeper()
move()
put_beeper()
def back_up():
"""
Backs up one corner, facing in the same direction
"""
turn_around()
move()
turn_around()
def transition_to_next_column():
move()
turn_right()
move()
turn_right()
Try downloading the PyCharm project for this section from the course website and running this program on your own computer. What would you do if you wanted the houses to be taller? How would you make them 5 beepers high? Is there any way you could improve the style of the solutions?
def main():
"""
To avoid the fencepost problem, we split the
logic into a loop to process columns, plus one
final call to check the last column.
"""
while front_is_clear():
process_column()
move()
process_column()
def process_column():
"""
Clears chad from the current column, if any.
Precondition: Karel is standing in the center of a column, facing East.
Postcondition: Karel is back in the same place/orientation and chad has been cleared
"""
if no_beepers_present():
remove_all_chad()
def remove_all_chad():
"""
Clears chad from the current column
Precondition: Karel is standing in the center of a column to be cleared
Postcondition: Karel is standing in the same place and the column has been cleared
"""
turn_left() # clean the upper corner
clean_chad()
turn_around() # clean the lower corner
clean_chad()
turn_left() # face East
def clean_chad():
"""
Clears chad from the corner Karel is facing
Precondition: Karel is facing a corner to be cleared of chad
Postcondition: Karel is in the same location/orientation, but
all chad has been cleared from the corner Karel is facing.
"""
move()
while beepers_present():
pick_beeper()
back_up()
def back_up():
"""
Backs up one corner, leaving Karel facing in the same
direction. If there is no space behind Karel, it will run
into a wall.
"""
turn_around()
move()
turn_around()
def turn_around():
turn_left()
turn_left()
Try downloading the Eclipse project for this section from the course website and running this program on your own computer. What would you do if you wanted Karel to repair ballots that were broken (that is, the center of the rectangle is not punched out, but some other square in the rectangle is)?
Comments: Make sure to comment every function you write, and describe what the function does, and what the assumptions are before and after it is called. Write your comments so that your program could easily be understood by another person.
Good function names: Part of good style is good naming. You want your function name to
succinctly describe what it does. Never call a function do_stuff, give it a good
specific name like back_up. Be consistent in how you name your methods. In our
solutions, we will use lower snake case naming conventions.
Short functions: We could have written our whole program in the run function, but it
is not good style and is difficult to follow. Try and break it down into function that are
small, understandable pieces of code, and that accomplish one main task. Establish
milestones as you work so that you can write some code for one simpler task, test it,
and fix it before moving on to the next task in solving a complex problem.