Stepwise Refinement

Jed Rembold & Fred Agbo

January 26, 2024

Announcements

  • Remember, problem Set 1 is due on Monday Jan. 29th @ 10pm!
  • Small sections started this week. Keep attending
    • Remember! The channel for getting help is Section leaders, QUAD, Instructors
    • Did you not get a message from Prof. Jed about your section meeting time?
      • Come see me after class and we can see about getting you placed into one.
  • Polling: https://www.polleverywhere.com/agbofred203
    • Reminder: include enough of your name that I can uniquely identify you!

Review Question

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

Summary So Far

  • Karel can only:
    • move()
    • turn_left()
    • pick_beeper()
    • put_beeper()
  • Get info about surroundings using predicate functions
    • Eg. front_is_clear()
    • Inverse options exist as well
  • Group code into reusable bundles
def function_name():
    # Code to be grouped
  • Conditional statements
    • Run certain code blocks only if a condition is true (the else block is optional)
    if condition_test:
        # Code if answer yes
    else:
        # Code if answer no
  • Iterative statements
    • Repeat code block as long as condition is true
    while condition_test:
        # Code to repeat
    • 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?

Understanding the Problem

  • There are four trees in this problem
  • We need to find a tree at a time
  • We need to remove the leaves
    • there are four leaves per 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

Top-Level Decomposition

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

Remember Your Algorithms!

  • Algorithmic design: The process of designing a solution strategy to a problem
  • An algorithm is just an approach or recipe for a method to solve a particular problem
    • Frequently language agnostic
  • Can you design algorithm to address the Fall/Winter problem?

Algorithm ⮕ Code

  • for four iterations
    • find a tree
    • then remove leaves
  • while the front is clear
    • keep moving
  • move up the tree
  • remove each leaf
  • move down the tree

Algorithm ⮕ Code

import karel
def main():
    # Here is our general solution with higher level of decomposition
    for i in range(3):
        find_next_tree()
        remove_leaves()
def find_next_tree():
    # the codes to find next tree
    while front_is_clear():
        move()
def remove_leaves():# codes to remove leaves
    move_up()
    deleaf()
    move_down()
  • moving up
def move_up():
    turn_left()
    while right_is_blocked():
        move()
  • deleafing
def deleaf():
    pick_beeper()
    for i in range(3):
        move()
        pick_beeper()
        turn_right()
    turn_left()
  • moving down
def move_down():
    while front_is_clear():
        move()
    turn_left()

Data Types

  • Generally, the data processed by computers can take on many forms
  • A data type defines the common characteristics of some data values that have a certain form or purpose.
    • Ex: a whole number or integer has certain characteristics common to all integers
  • A data type has a domain, which is the set of all potential values that would belong to that type.
    • Ex: 0,1,2,3,4,5,6,7,…
  • A data type has a set of operations that define how those values can be manipulated
    • Ex: You can add two whole numbers (5+2)

Numeric Types

  • Initially, we’ll focus on the numeric types
  • Python has 3 data types for representing numbers:
    • int for integers and whole numbers

      1, 2, 3, 10, 1001010101, -231
    • float for numbers containing a decimal point

      1.23, 3.14, 10.001, 0.0, -8.23
    • complex for numbers with an imaginary component (which we won’t deal with)

Expressions

  • Python describes computation using arithmetic expressions, which consist of terms joined by operators
    • Very similar to how a logical English sentence has nouns connected by verbs
  • A term in an expression can be:
    • an explicit numeric value (called a literal) like 1 or 3.14
    • a variable name serving as a placeholder to a value (more on those in a moment!)
    • a value resulting from the output of a function call (more on those on Monday!)
    • another expression enclosed in parentheses


[term] [term] [operator]

// reveal.js plugins