Designing Programs#

In this worksheet, you will learn and practice how to design programs. You will learn how to use the design recipe to design code, and how to break down a problem into smaller subproblems.

The Design Recipe#

The design recipe is a systematic way of designing programs. It consists of the following steps:

  1. Problem analysis: Understand the problem and its requirements.

  2. Data analysis: Understand the data that the program will work with. You will decide what data you are working with and the types of that data.

  3. Examples: Find examples of the program’s input and output.

  4. Program design: Design the program. You will decide what functions you need, what the functions do, and how they work together. You might write down a series of steps for the program.

  5. Implementation: Implement the program in code. You have to translate everything you have decided in the previous steps into code that the computer understands.

  6. Testing: Test the program to make sure it works correctly. You will run the program with the examples you found in step 3 and make sure the program produces the correct output.

Example: Reading the Final Energy from ethanol.out#

Yesterday, we wrote code to get the Final Energy from ethanol.out. Let’s see how we can use the design recipe to understand this problem better.

1. Problem Analysis#

Looking at our task from yesterday, we wanted to find a specific number in the ethanol.out file. When we open this file, we see it contains many lines of text. Somewhere in this file is a line that has “Final Energy” followed by the number we want. Instead of searching through the file ourselves, we want Python to find this number for us.

2. Data Analysis#

Before we write code, let’s think about what we’re working with:

  • We have a file called “ethanol.out” in the data/outfiles directory

  • The file contains lines of text

  • One of these lines has “Final Energy” followed by a number

  • We want to end up with just that number, so we can use it for calculations

3. Examples#

Let’s be very specific about what we’re looking for. In our file, we might see a line like this:

Final Energy: -123.45

We want our program to find this line and give us just the -123.45 part.

4. Program Design#

If we were doing this by hand, we would:

  1. Open the file and read all the lines

  2. Look through each line until we see “Final Energy”

  3. Once we find that line, get just the number part

Now we need to turn each of these steps into Python code.

5. Implementation#

Let’s translate each of our design steps into Python code. This is where we figure out exactly how to tell Python what we want it to do.

Step 1: Open and read the file

filepath = "data/outfiles/ethanol.out"  # Tell Python where to find our file
with open(filepath,'r') as f:           # Open it for reading
    data = f.readlines()                # Get all the lines from the file

This puts all the lines from our file into data. Think of data like a list of all the lines from our file.

Step 2: Find the line containing “Final Energy”

for line in data:                    # Look at each line one at a time
    if 'Final Energy' in line:       # If this line has "Final Energy" in it
        energy_line = line           # Save this line - it's the one we want!

Now energy_line contains the whole line with our number in it, like “Final Energy: -123.45”

Step 3: Get just the number

words = energy_line.split()          # Break the line into pieces at each space
                                    # words now looks like: ["Final", "Energy:", "-123.45"]
energy = float(words[3])            # Convert the fourth piece to a number
print(energy)                       # Show us the answer

Let’s put it all together:

# Step 1: Read the file
filepath = "data/outfiles/ethanol.out"
with open(filepath,'r') as f:
    data = f.readlines()

# Step 2: Find the line that has "Final Energy"
for line in data:
    if 'Final Energy' in line:
        energy_line = line

# Step 3: Get the number from that line
words = energy_line.split()
energy = float(words[3])
print(energy)

6. Testing#

When we run this program, it should print out -123.45. We can check if this is right by:

  1. Opening ethanol.out ourselves

  2. Finding the “Final Energy” line

  3. Making sure the number we see matches what our program printed

Overall, our code yesterday looked like this:

# Read the file
filepath = "data/outfiles/ethanol.out"
with open(filepath,'r') as f:
    data = f.readlines()

# Find the line that has the phrase "Final Energy"
for line in data:
    if 'Final Energy' in line:
        energy_line = line

# Split the line into words
words = energy_line.split()
energy = float(words[3])
print(energy)
-154.09130176573018

Your Turn!#

Now that you’ve learned about the design recipe. Try applying it to the following problems.

For each problem, try applying the Design Recipe first. After you have made a plan, then try writing code.

  1. Write Python code that calculates the area of a rectangle. (Don’t forget to think of and check your answer with examples!)

  2. Calculate an average grade given a list of grades.

  3. Calculate the average of total energy for an mdout file (like 03_Prod.mdout). (Note, this one will be hard to predict, but you can check that pieces of your code work. For example, you might use similar logic to calculating the average grade.)

  4. Getting the Final Energy for several files and saving the results in a list.