Clever Karel

Jed Rembold

January 16, 2026

Happy Friday!

Quick Announcements

  • Ensure you have the tech installed on your system!
    • Then swing by my office during office hours (or when my door is open) to check in with me before the end of next Tuesday
  • Sections start next week. If you have not yet filled out your availability, please do that now here! I’m still missing about 10 of you.
  • Discord channel is live! Invite code in Canvas announcement.
  • Problem Set 1 is live! Not due until a week from Monday

Group Problems

Preliminaries

  • Take a quick moment to introduce yourself to your group-mates for the day
    • Name
    • Class year
    • Major?
  • Fun question of the day: if you were to add one fun command to Karel, what would it do (and be named)?

Considerations

  • Don’t forget about decomposition!
  • For all code writing questions today, try to think about how you could decompose it into at least two parts
    • Frequently, you’ll be able to reuse one of those parts!
  • You can find a repository of worlds for today’s problems here
    • Not required, but there if you want to be able to test solutions

Problem 1: Flippy Floppy

  • Suppose we had the situation similar to the world below, and you want to exchange where beepers are
    • Replace empty space with a beeper, and a beeper with empty space
  • The world is always the same size, Karel has infinite beepers, and always starts facing East
  • Initial beepers could be anywhere though!

Problem 2: The Race

  • Karel starts at the bottom of a world facing north
  • There are exactly two stacks of beepers placed somewhere above them
  • To win the race Karel needs to pick up both stacks of beepers and then touch the North wall
    • Remember that Karel can only pick up one beeper at a time!

Problem 3: Nested Beeper Drops

Karel starts as shown to the right with 20 beepers in its bag. After executing the commands below, how many beepers are left in the bag upon the conclusion of the program?

while left_is_clear():
    while front_is_clear():
        move()
        if no_beepers_present():
            put_beeper()
    turn_left()

Live-Coding

A Narrow Path

  • There is a beeper path that extends from one side of the world to the other
  • Karel starts on one end, facing toward the first beeper
  • Goal is to follow the path to the other end

A Solution

import karel

def main():
    """Follows a path of beepers as best as possible from one end of the
    world to the other end.
    """
    while front_is_clear():
        follow_straight_beeper_path()
        determine_next_direction()

def follow_straight_beeper_path():
    """Follows a beeper path until no beeper
    is found, then doubles back to last beeper
    space"""
    while beepers_present():
        if front_is_clear():
            move()
    if front_is_clear():
        turn_around()
        move()

def determine_next_direction():
    """Determines the direction of the next path of beepers. Tries one direction
    and if it finds no beeper there it knows it must be the other direction.
    Leaves Karel facing the next path of beepers.
    """
    turn_left()
    if front_is_clear():
        move()
        if beepers_present(): # This side! I'm reseting here but probably don't need to
            turn_around()
            move()
            turn_around()
        else: # No beepers, so must be the other direction
            turn_around()
            move()
    

def turn_around():
    """Turns Karel 180"""
    turn_left()
    turn_left()
// reveal.js plugins