For Stepwise Refinement’s Sake

Jed Rembold

September 3, 2025

Announcements

  • Homework
    • Have you swung by my office to visit yet? Many of you have not! Let’s make that happen by the end of the week!
    • Problem Set 1 posted and due on Monday night!
  • You are meeting with your small section today or tomorrow!
    • You should have gotten an email with a time and place from me
    • I may be approaching a few of you in the coming days to see if you’d be willing to swap to another to help keep the numbers balanced
  • Polling: polling.jedrembold.prof

Inception: Loops in Loops

  • Whenever a loop ends, you just return to the same indentation level as when that loop began

  • For loops inside other loops then, this means that the “inner-most” loop runs from start to finish for every single step of the outer loop

  • What does the below chunk of code accomplish?

    while front_is_clear():
        move()
        while not_facing_north():
            turn_left()
        turn_left()
    put_beeper()

Loopy Example

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()

Now Your Turn

Karel starts as shown to the right. At which beeper do they end up when the below sequence of commands finishes?

while no_beepers_present():
    while front_is_clear():
        move()
        if beepers_present():
            turn_left()
        else:
            turn_right()
    turn_left()

Counting Loops

  • Sometimes we know the number of times we want to loop

    • It is not dependent on some condition
  • In these circumstances, the iterative statement called a for loop is best used

  • Syntax looks like:

    for i in range(|||desired count|||):
        |||commands to be repeated desired count times|||
    • |||desired count||| is an integer indicating the number of times you want the loop to repeat
    • The i is a name that we will later make more general, but for now you can always leave it as an i

An Example for you

  • Suppose Karel starts in the bottom left corner of a “room”
  • We want Karel to create a 6x6 square outline of beepers in the center of the room
  • Need to repeat making each side 4 times
  • Need to repeat placing a beeper and moving 6 times for each side
    • Placing 6 beepers requires moving only 5 times. So not everything can be in the loop

A Potential Solution

import karel

def main():
    """Draw a 4x4 square in the world."""
    position()
    draw_box()

def position():
    """Move to starting corner of box."""
    move()
    move()
    turn_left()
    move()
    move()
    turn_right()

def turn_right():
    """Turns Karel 90 deg to the right."""
    turn_left()
    turn_left()
    turn_left()

def draw_box():
    """Draws a box with 4 equal sides in a CCW direction."""
    for i in range(4):
        draw_6_line()
        turn_left()

def draw_6_line():
    """Draws a straight line of 6 beepers, if space."""
    for i in range(5):
        if no_beepers_present():
            put_beeper()
        if front_is_clear():
            move()
    if no_beepers_present(): # Last beeper to make 6
        put_beeper()

Summary So Far

  • Karel can only:
    • move()
    • turn_left()
    • pick_beeper()
    • put_beeper()
  • Can get info about surroundings using predicate functions
    • Eg. front_is_clear()
  • Group code into bundles

    def |||function name|||():
        |||Code to be grouped|||
  • Conditional statements
    • Run certain code blocks only if a condition is true

      if |||condition test|||:
          |||Code if answer yes|||
      else:
          |||Code if answer no|||
  • Iterative statements
    • while loop: repeat code block as long as condition is true

      while |||condition test|||:
          |||Code to repeat|||
    • for loop: repeat set number of times

      for i in range(|||desired count|||):
          |||Code to repeat|||

Stepwise Refinement

  • The most successful way to solve a complex problem is to break it down into progressively simpler problems
  • Begin by breaking the whole problem into a few simpler parts
    • Some of these parts might then need further breaking down into even simpler parts
  • The process is commonly called stepwise refinement or decomposition

Excellent Decomposing

  • A good problem decomposition should mean:

    The proposed pieces should be easy to explain
    One indication that you have succeeded is if it is easy to give them simple names
    The steps are as general as possible
    Each piece of code you can reuse is one less piece of code you need to write! If your steps solve general tasks, they are much easier to reuse.
    The steps should make logical sense for the problem you are solving
    If you have a function that will work to solve a step but was designed (and named) with something else entirely in mind, adopt it for the currently needed situation

Enter the Winter

  • Suppose we want Karel to usher in the Fall/Winter by removing the “leaves” from the tops of all the trees

Understanding the Problem

  • What are we guaranteed by the problem?
  • How will we know when we are done?

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Top-Level Decomposition

  • We could break this problem into two main subproblems:
    1. Find the next tree
    2. Strip the leaves off that tree

Algorithms

  • The process of designing a solution strategy to a problem is called algorithmic design
  • An algorithm is just an approach or recipe for a method to solve a particular problem
    • Frequently language agnostic
  • Algorithms are not a new concept
    • Euclid’s algorithm to find the greatest common factor between two numbers, for instance
  • A large part of computer science is focused on the study or analysis of algorithms

Algorithm ⮕ Code

  • You need to have an algorithm in place before you can write the code to tell the computer what you want to do
    • Imagine an alien asking me how to bake a cake. I need to understand the steps to baking the cake before I can even worry about the translation or communication
  • Programming tools like conditional statements and loops will frequently play a role in your algorithm, but as general concepts
    • I can easily describe a loop to you without needing the exact syntax of Python
  • The implementation of the algorithm is the act of translating it into Python (or whatever language you are using)

An Amazing Algorithm

  • Consider a simple, loop-less, maze that we want to move through
    • Karel could start anywhere
    • The end of the maze is a beeper
  • A common algorithm to get through the maze is to essentially always follow or touch the wall to your right
    • How could we phrase this in language Karel would understand?
  • Let’s take the rest of class to work with a neighbor(s) to sketch out your code
    • Want to test it? A zip with some mazes is located here

// reveal.js plugins