Problem Solving

Introduction

How do you get from a set of requirements to a working program? There is no single approach. Your first instinct might be to start typing code right away. That can work for tiny problems, but for bigger ones it easily leads to a time-wasting spiral of errors you do not know how to fix. These general steps can guide you toward a solution:

  1. Understand the requirements
  2. Design tests that anticipate problems you need your code to solve
  3. Outline how you would approach the problem
  4. Translate your plan into code
    • Add and run tests for each piece of your code's functionality
    • Debug and revise your code accordingly
    • Iterate the steps in 4 until your solution meets your requirements
  5. Reflect on your approach and process

These steps may feel like overkill for very small programs, but practice them throughout this course.

1. Understand the requirements

In real life this is often the hardest part, because clients may not have a clear idea of exactly what they want. In this course, the assignments come with clear requirements. It still helps to go through the assignment and turn the requirements into a checklist. If you run into a case the assignment does not cover, do your best to reason it through, or ask the instructor.

Quick check on what "clear" means:

Check your understanding. A requirement is clear when you could write the code without guessing. Here are three requirements:

  • A. Print a nice greeting.
  • B. Ask for the user's name, then print Hello, followed by the name.
  • C. Make the program friendly.

Which one is clear enough to turn into code? Give answer a type hint and set it to its letter, in quotes, such as "A".

Hint 1

Look for the one that says exactly what to ask and exactly what to print.

2. Design tests

Come up with test cases that represent how your code could be used. They should be good enough that if your code passes all of them, you can be confident it meets the requirements. For each test, decide what result you expect before you run it. Otherwise it is easy to glance at the output and convince yourself it looks reasonable.

Try it on a small program that converts a temperature from Celsius to Fahrenheit. The best tests are the ones that would catch a mistake, so choosing the inputs is the real work.

Check your understanding. You only have time to run three tests on the converter. Which three Celsius inputs would you pick?

  • A. 0 (water freezes)
  • B. 100 (water boils)
  • C. 20 (a mild day)
  • D. -40
  • E. 25 (a warm day)
  • F. 1000

Give picks a type hint and set it to three different letters, in quotes, such as "ABC".

Hint 1

Which of these temperatures do you already know the Fahrenheit answer for without calculating? And which ones are so alike that testing both would teach you nothing new?

Here is why those three work well. Each one has an answer you can check by hand, so you do not have to trust the program to tell you it is right.

The others add little. 20 and 25 are ordinary values close together, so if one works the other almost certainly does. 1000 is an unusual case, but with only three tests it is better to spend them on values you can check.

Now write one expected result down before you run anything. The conversion is: multiply by 9, divide by 5, then add 32. Work out the Fahrenheit value for 100 degrees Celsius by hand, then run the cell and compare:

Check your understanding. Decide the Fahrenheit value you expect for 100 degrees Celsius, before you run the cell. Give expected a type hint and your value. Then run it and read the last line.

Hint 1

100 * 9 is 900, then divide by 5, then add 32. Write the number you get before running anything.

If the two numbers match, the test passes. If they do not, either the program or your expected value is wrong, and now you know there is something to look into.

How do you know what cases to test?

Later in the course, when your programs have branches and functions, you will also test each path through your logic and each part on its own, then all the parts together.

It is better for you to find cases that break your code than for a customer or employer to find them.

Try picking the cases for one boundary:

Check your understanding. A shop gives free shipping when you buy 4 or more items. Which two quantities test the boundary? Give the two variables below a type hint and a value:

  • below_boundary is the largest quantity that does not get free shipping
  • at_boundary is the smallest quantity that does
Hint 1

A boundary has two sides. Test the last value on one side and the first value on the other.

3. Outline your approach

Write an outline of the steps your code needs to take to meet the requirements. Two useful strategies are stepwise refinement and working through examples by hand.

Stepwise refinement

Stepwise refinement is a common way to outline an approach. You break the original problem into smaller sub-problems, then break each sub-problem into even smaller ones. Repeat until you have a list of steps detailed enough to translate into code. This list of steps is often called pseudocode.

As an example, suppose you want a program that works out what a customer owes at a coffee shop. It might seem like a lot at first, but you can break it into these sub-tasks:

Each step sounds clear until you ask what it means. What do we mean by "get the order"? The program needs to know how many coffees and how many muffins the customer wants. And "work out the total" from what? Let's break the steps down further:

Now ask again. What do we mean by "show the total"? A bare number like 7.3 is not much use to a customer, and a calculation can produce 7.299999999, which looks wrong. We want a label and exactly two decimal places. And what is "the cost of the coffees"? We need a price, and a tax rate, and nobody has said where those come from. One more layer:

Keep going until you know every small step that needs to happen. A step is detailed enough when you could write it as one or two lines of code without guessing.

Notice what the extra layers turned up: the prices and the tax rate, converting typed answers to numbers, rounding, and the label. None of them were in the first outline, and each is easy to forget until the program gives a wrong answer. Finding them on paper is much cheaper than finding them halfway through writing code. The habit to build is to look at each step and ask "what do I mean by this?" until no step is vague.

Check that you can order the steps of an outline:

Check your understanding. These five steps from the coffee shop program are out of order:

  • A. Print the total.
  • B. Ask how many coffees.
  • C. Add sales tax.
  • D. Multiply the coffees by the coffee price.
  • E. Round the total to two decimal places.

Put them in a sensible order. Give order a type hint and set it to the five letters in that order, in quotes, such as "ABCDE".

Hint 1

Start with what the program has to know first. Finish with the step that shows the answer.

Work through examples by hand

People do many tasks without being aware of the steps involved. In this method, you solve several examples of the problem yourself with pencil and paper, paying close attention to each tiny step. Doing this often helps you notice patterns in how you solve them. You can combine this with stepwise refinement to help figure out what the sub-tasks should be.

For example, how could you round 7.299999999 to two decimal places? Python has round() for this, but here we work it out ourselves to practice the method. It looks hard, so start with an easier problem: round to a whole number.

int() drops everything after the decimal point, but that chops instead of rounding: int(7.9) is 7, and 7.9 should round to 8. Work a few by hand:

The pattern: if the part after the decimal point is .5 or more, the number goes up. Adding 0.5 before chopping does exactly that: 7.3 + 0.5 is 7.8, which chops to 7, and 7.5 + 0.5 is 8.0, which chops to 8. So int(number + 0.5) rounds to a whole number.

Back to the real problem. Can we turn two decimal places into the problem we just solved? Multiplying by 100 moves the decimal point two places right, so 7.299999999 becomes 729.9999999. Rounding that to a whole number gives 730, and dividing by 100 moves the point back: 7.3.

Nobody has to remember this formula. Each step was a small problem on its own: solve an easier version first, then turn the harder problem into the easier one. This version only works for positive numbers, which is one reason real programs use round().

Now use the same steps on a different case:

  1. Round price and tip to one decimal place and print each one, without using round(). Use the steps above and change what needs to change.
Hint 1

Which number do you multiply by to move exactly one digit to the left of the decimal point? Use the same number to shift back at the end.

4a. Translate your outline into code

Translate each sub-task of your outline (or pseudocode) into code, but not all at once (unless it is a tiny program). Write a short section of code and run it to see if it does what you expect. If it does, add another short section and run again, until the whole outline is translated. Avoiding big changes makes it easier to track down new bugs.

Here is the first piece of the coffee shop program. Run it on its own, then follow the comments to add the next piece:

4b. Run your tests

How do you know if your code does what you expect? Run it on the cases you designed in step 2 and compare each result with the value you wrote down beforehand, as in the Celsius example. If they match, the test passes. If you think of more tests while coding, add them. The more you test, the more bugs you will catch.

Recall: click each blank to check yourself

Name the three kinds of test input.

  • cases, such as 0 for a factorial
  • values, on both sides of a limit, such as 9 and 10
  • cases, extreme or unusual inputs

4c. Debug your code

What if your code does not do what you expect? Then you need to find the problem and fix it. Since you only change a few lines at a time, a new problem is almost certainly among those lines. Error messages often tell you what the problem is and where it occurred. If you do not see the problem at first, narrow down where it could be, step by step. After fixing it, re-run your tests to make sure that part works now and that you have not broken anything else.

Practice finding a bug:

  1. This program should print the average of apples and oranges, which is 3.5. It prints something else. Run it, find the bug in the average line, and fix it. Keep using the variables, so the program would still work for other numbers.
Hint 1

Division happens before addition. Which part should be added first, before dividing by 2?

4d. Let me reiterate to iterate

As you build each piece of your code, test it and fix any bugs. Then repeat: build, test, and debug. Do not write the whole program in one go and hope it has no typos or logic errors. If each piece works as you build it, the finished program is much more likely to work too.

5. Reflect on your process

Reflecting on your problem-solving process helps you improve it for next time. Ask yourself: