Module 7 of 13

Solving problems step by step

Turn a vague task into clear instructions and test them on paper.

Before writing a program, try explaining the task so that someone else could carry it out without guessing. This is harder than it sounds. The difficulty is useful: it reveals decisions that software would otherwise make accidentally.

A reading-log algorithm checks each entry, adds valid minutes, and stops on invalid input

7.1 Define the problem before choosing a tool

Suppose you want to summarize a reading log. “Show my progress” is too broad. Do you want total minutes, number of sessions, or days on which you read? What happens when the log is empty? What counts as an invalid entry? A useful problem statement names the inputs, the desired output, the rules, and what counts as success.

Decomposition breaks the work into smaller parts: read entries, check them, total valid minutes, show a result. Abstraction keeps the details that matter and temporarily sets aside those that do not. If the goal is total reading time, the color of a book cover does not matter. The book’s title may not matter either—unless you also want totals by book.

Worked example. Input: a list of reading sessions, each with a date and nonnegative minutes. Output: total minutes and session count. Rule: a negative time is rejected rather than silently added. Empty list: total and count are zero. Success: the result matches a hand calculation for supplied examples.

7.2 Algorithms, pseudocode, and flowcharts

An algorithm is a finite method for solving a defined problem. It is more than a vague intention but does not have to be written in a programming language. Pseudocode describes the steps in structured ordinary language. A flowchart draws decisions and routes. Both can help you see whether a step is missing.

Here is a method for the reading total:

set total to 0
for each session in the list:
    if minutes is not a nonnegative number:
        report an invalid entry and stop
    add minutes to total
report total

The loop repeats a check and addition for each session. The decision makes invalid data visible. “Add the times” would leave several questions unanswered. Does a blank cell count as zero? Should 30 minutes be accepted as text? Clear rules let two people get the same result.

Check. Is the pseudocode itself a Python program? Answer: No. It communicates the method. A program must use the precise syntax and behavior of a language.

7.3 Testing an idea on paper

Use a trace table to follow values as an algorithm runs. For the input [20, 15, 25], total starts at 0, then becomes 20, 35, and 60. Try an empty list: the loop runs zero times and the result is 0. Try [20, -5]: the method reports an invalid entry rather than producing 15.

Entry examinedTotal beforeActionTotal after
200add 2020
1520add 1535
2535add 2560

These cases check normal, empty, and invalid input. They do not prove the algorithm is correct for every possible input. A test finds a defect when the result is wrong; passing tests gives evidence within the tested cases. A written argument about why the steps work can give more confidence, but its assumptions must also be right.

Try it. Trace [0, 10]. Then trace [10, "unknown"]. State exactly where the second trace stops under the rule above.

7.4 Comparing approaches as a problem grows

If you search an unsorted list for a title, you may need to inspect every item. A sorted list allows binary search: inspect the middle, then discard the half that cannot contain the target. This can greatly reduce comparisons for a large list. It works only if the list is sorted according to the same rule you use to compare items. Sorting itself costs work, so it may not pay off for a tiny list searched once.

An algorithm must also terminate. A loop that asks for “the next item” needs a way to know when there are no items left. A search that keeps halving a range needs a clear stopping condition for both finding and not finding the target. Efficiency matters, but a fast wrong answer is still wrong.

Module activity. Write an algorithm for the reading-log summary. Include input rules, the empty case, and a final result. Ask someone to trace it, or trace it yourself after a break. If two interpretations are possible, rewrite the ambiguous step.

Check. “Look up the title quickly” is an instruction. Is it a complete algorithm? Answer: No. It does not say what collection to use, how to search, or what to report if the title is absent.

Now we can translate a small method into code.

Image preview

Fit