Breaking Problems into Steps
Concepts covered: Functions
A word problem arrives as a sentence, and Python runs lines. The work between them is a list of steps in plain words, each small enough that you already know the line of code it becomes. Writing that list first means the code is a translation, and a translation is checked line by line against its source.
From sentence to steps
Here is a problem as a teammate would phrase it: a store gives free shipping on orders over 50 dollars, and we have the list of order totals from today, so how many orders got free shipping? Before touching code, write what has to happen, in order:
Steps in plain words
- Start a counter at 0, because no orders have been checked yet.
- Look at each order total, one at a time.
- If the total is more than 50, add 1 to the counter.
- After the last order, the counter holds the answer.
Each step names 1 thing Python can do: an assignment, a for loop, an if with an assignment inside it, and a return. The translation is 1 line per step, with the loop body indented under its step.
| Step | Code |
|---|---|
| 1. Start a counter at 0 | count = 0 |
| 2. Look at each order | for total in orders: |
| 3. More than 50? Add 1 | if total > 50: count = count + 1 |
| 4. Report the counter | return count |
def count_free_shipping(orders: list) -> int:
count = 0
for total in orders:
if total > 50:
count = count + 1
return count
print(count_free_shipping([20, 65, 50, 120]))Output:
2
Of the 4 totals, 2 come out: 65 and 120 are over 50, 20 is under, and 50 sits on the line. The problem said over 50, and over means strictly more, so the 50 dollar order pays shipping. That decision was made at step 3, in words, before the > was typed, which is where boundary decisions belong.
Steps that need more than 1 variable
Some problems need 2 running values. A ratings problem: each product has a list of star ratings from 1 to 5, and a 0 means the customer skipped the rating, so what's the average of the real ratings? The word average hides 2 totals, the sum of ratings and the count of ratings, and both must skip the zeros.
Steps
- Start a running sum at 0 and a running count at 0.
- Look at each rating.
- If the rating is greater than 0, add it to the sum and add 1 to the count.
- After the loop, divide the sum by the count.
def average_rating(ratings: list) -> float:
total = 0
count = 0
for rating in ratings:
if rating > 0:
total = total + rating
count = count + 1
return total / count
print(average_rating([5, 0, 3, 4, 0]))Output:
4.0
Step 3 became 2 lines under 1 if, because both totals change under the same condition. Step 4 is the one that would go wrong if a product had only zeros: the count would be 0 and the division would fail. Catching that is the job of the last section of this lesson; the step list is what made it catchable, because 'divide the sum by the count' is a sentence you can question.
Practice
A teammate asks: given a list of daily temperatures, how many days were at least 30 degrees? Which step list translates the question?
- Start a counter at 0; look at each temperature; if it is 30 or more, add 1 to the counter; report the counter.
- Start a counter at 0; look at each temperature; if it is more than 30, add 1 to the counter; report the counter.
- Start a sum at 0; look at each temperature; if it is 30 or more, add it to the sum; report the sum.
- Look at each temperature; if it is 30 or more, add 1 to the counter; report the counter at the end.
Each blank in the next program comes from a word in its step list. 'Start a counter at 0' sets the starting value. 'More than 10' sets both the operator and the number it compares against. 'Add 1' sets how much the counter grows on each match. Before you submit, check the 10 kilogram package against your comparison: > 10 leaves it out, and >= 10 would count it.
About this lesson
This section is part of the Problem Solving: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.