Thinking like a programmer: break a problem into steps

An algorithm is a set of steps that turns an input into an output, in an order that can be followed and repeated. Thinking in algorithms does not start with code. It starts with a clear statement of the input, the output, and the steps in between, written in plain words. Once those steps are clear, turning them into code is mostly translation, not invention.
Beginners often skip the plain words step and start typing code, then get stuck halfway through. That stuck feeling usually means the problem was not yet broken down far enough. A step that says "find the best score" is not a step at all, because it hides the work inside a vague word. A step that says "compare each score with the largest score seen so far, and keep the larger one" is a step a program can follow, and it is the kind of step this guide teaches you to write.
Start with input, output, and one example
Take a simple problem: given a list of scores, find the largest score. The input is a list of numbers. The output is one number, the largest in the list. That pair of sentences already rules out many wrong starts, because any code that prints the whole list, or adds the numbers together, does not match the output you stated.
Now work an example by hand. Use the list [4, 9, 2, 9, 5] and walk through it slowly. Start with no largest score yet. Look at 4, and since it is the first number, it becomes the largest so far. Look at 9, which is larger than 4, so it becomes the largest so far. Look at 2, which is smaller, so the largest stays 9. Look at the second 9, which is not larger, so the largest stays 9. Look at 5, which is smaller, so the largest stays 9. At the end, the answer is 9.
def largest(scores):
best = scores[0]
for score in scores:
if score > best:
best = score
return best
print(largest([4, 9, 2, 9, 5]))
Break a bigger problem into smaller problems
Most real problems are too large for one set of steps. The way forward is to split the problem at a natural joint, solve each part on its own, and then join the parts. Suppose the task is to read scores from a file, find the largest score, and print a short report. That task splits into three smaller problems: read the file into a list, find the largest value in the list, and format the report. Each part can be worked out, coded, and tested on its own, and the main program only joins them in order.
| Question to ask | What it gives you | Example for the scores problem |
|---|---|---|
| What goes in? | The input, in plain words | A list of numbers from a file |
| What comes out? | The output, in plain words | One number, the largest score |
| What repeats? | The loop in the plan | Compare each score with the best so far |
| What is kept? | The variable in the plan | The best score seen so far |
| What could be empty or odd? | The edge cases to test | An empty list, one score, repeated scores |
Functions are the natural home for split problems in Python. Each smaller problem becomes a function with a clear input and output, and the main program calls those functions in order. The functions guide explains how to write a function that takes a value and returns a result, which is exactly the shape a split problem needs. If a function needs to know too much about other parts of the program, that is a sign the split is in the wrong place, and the boundary should move.
Edge cases are part of the plan, not an extra
An edge case is an input at the boundary of what the program should handle, such as an empty list, a list with one value, or a list where every value is the same. Beginners often test only with a normal list, and the program then fails on the first unusual input it meets. Planning for edge cases early is cheaper than fixing them later, because the fix often changes the starting value or the order of the steps.
Return to the largest score example. The code above starts with scores[0], the first score in the list. That works when the list has at least one score, and it fails when the list is empty, because there is no first score to read. That failure is useful information. It tells you the problem statement was incomplete: it never said what to do with an empty list. The honest fix is to decide that behaviour first, such as returning nothing, raising an error, or asking for a list with at least one score, and then to code that decision. The debugging guide shows how to use a failing case as evidence, rather than treating it as a surprise.
Common mistakes to watch for
Writing code before the steps are clear is the most common mistake, and it shows up as a program that is rewritten from scratch several times. If you find yourself deleting large blocks and starting again, stop and write the steps in plain words on paper. A plan that cannot be written in plain words is not ready for code, and a few minutes on paper usually saves a longer struggle in the editor.
Using a vague verb as a step is a related mistake. Steps such as "process the data" or "handle the scores" hide the real work, and code written from them tends to wander. Replace each vague verb with a concrete action: compare, add, count, keep, or return. If you cannot name the action, work another example by hand and watch what you actually do at that point.
Skipping the hand worked example is a third mistake. It feels slow, but it is the fastest way to find a missing step, because your own hands will pause at the point where the plan is unclear. That pause is the bug in the plan, found before any code is written. Code written from a complete hand worked example rarely needs a large rewrite, because the order and the kept values are already settled.
Limits of this guide
This guide covers breaking a problem into steps, working an example by hand, and testing edge cases. It does not cover sorting methods, searching methods, or measuring how fast a method runs on large inputs, because those topics need their own examples and their own trade offs. It also does not cover problems where the input itself is unclear, such as a task given in vague words by someone else. In that case, the first work is to ask what the input and output should be, and to agree on an example, before any steps are written.
Worked example: find the largest number, step by step
Finding the largest number in a list is a good algorithm example, because the steps are short and the result is easy to check. The plan starts by assuming the first item is the largest so far, then compares each later item with that current best, and replaces the best whenever a larger item appears. At the end, the best holds the largest value in the whole list.
numbers = [7, 19, 4, 12]
best = numbers[0]
for value in numbers:
if value > best:
best = value
print(best)
Trace the example by hand. Best starts at 7. The loop first compares 7 with 7, which is not larger, so best stays 7. It then compares 19 with 7, which is larger, so best becomes 19. It then compares 4 and 12 with 19, neither of which is larger, so best stays 19, and print shows 19. That trace confirms the algorithm, because each step follows a rule that can be checked without running the code.
Edge cases test the plan further. An empty list has no first item, so the starting step fails, and the program needs a rule for that case, such as reporting that no largest value exists. A list with one item works, because the loop compares that item with itself and best stays correct. Testing those cases before coding saves a later fix, because the plan already says what should happen when the input is empty or tiny.
From a problem statement to steps, in a table
Turning a vague problem into steps follows a repeatable pattern, and the table below shows that pattern with a counting example. The pattern starts with a concrete example, names the input and output, lists the steps in order, and then checks the steps against the example and its edge cases. Skipping the example stage is a common mistake, because a plan written in abstract words is harder to check than a plan tested on real values.
| Stage | What to write down | Counting example |
|---|---|---|
| Example | One small input and its answer | "red fish blue" gives red 1, fish 1, blue 1 |
| Input and output | What goes in, what comes out | Text in, word counts out |
| Steps | Actions in order | Split, then count each word |
| Edge cases | Empty, single, repeated inputs | Empty text, one word, repeated words |
Writing the steps in plain words before coding is called pseudocode, and it helps because plain words are easier to rearrange than code. When a step is wrong, changing a sentence is faster than changing a program, and the corrected sentence can then be turned into code with confidence. That habit also helps when asking for help, because a helper can read the plan without needing to run the code.
Once the steps are clear, translating them into code is usually straightforward, because each step maps to a loop, a condition, or a dictionary update. When a step does not map cleanly, that is a signal that the step is still vague, and it should be split into smaller steps before coding resumes. A step that says "process the data" is too vague, while a step that says "split the text into words" is ready to code.
Searching and sorting as thinking patterns
Many beginner problems reduce to searching for an item or sorting a set of items, and recognising those patterns saves time. Searching means scanning items until a match is found, or until the items run out, which is the pattern behind finding a name in a list or a code in a table. Sorting means arranging items in an order, which is the pattern behind ranking scores or listing names alphabetically.
A linear search scans from the start and checks each item in turn, which works on any list, sorted or not. A binary search is faster on a sorted list, because it checks the middle item and then discards half the list on each step, yet it fails on an unsorted list, because discarding half assumes the order is meaningful. Knowing which pattern fits the data shape is more useful than memorising the code for either one.
For sorting, the practical beginner pattern is to use the built-in sort for real work, and to study a simple sort, such as bubble sort, only to understand how sorting works step by step. That split keeps real programs reliable, while still building the understanding that helps when a sorting problem needs a custom order, such as sorting by a second field when the first field ties.
Estimate the work before coding a loop
A rough estimate of how much work an algorithm does helps when choosing between two plans. A single loop over a list does work proportional to the list size, because each item is visited once. A loop inside another loop does work proportional to the product of the two sizes, because the inner loop runs fully for each outer pass. That difference grows fast, which is why nested loops deserve a second look on larger inputs.
That estimate does not need precise timing. Counting the loops and the items they visit gives a good enough picture for beginner programs. A plan with one loop over a hundred items does about a hundred steps, while a plan with two nested loops over a hundred items each does about ten thousand steps. That comparison often decides which plan to code, before any code is written.
When two plans do similar work, clarity wins over a small speed difference. A plan that is easy to read and to test will be easier to fix when the requirements change, and requirements change often in real programs. A faster plan that is hard to follow may save a little time on a small input, while costing far more time when it needs to be changed or debugged later.
Frequently asked questions
Do I need strong maths to think in algorithms?
No, most beginner problems need clear steps, not advanced maths. Counting, comparing, and keeping track of a running value cover a large share of early problems. If a problem does need a formula, state it in plain words first, then look up the exact form in the docs, rather than letting the maths block the whole plan.
How should I practise algorithmic thinking each week?
Pick one small problem, write the input and output, work one example by hand, and write the steps before you code. Then code those steps, test with an empty input and a single value, and write one line about what went wrong. One problem done this way teaches more than five problems solved by guessing at code.
What if I understand the problem but cannot see the first step?
Work the smallest example you can, with real numbers, on paper. Write down what you did at each moment, in plain words, and look for a step that repeats. That repeated step is often the loop, and the value you kept updating is often the variable you need. If no step repeats, the problem may need to be split into two smaller problems.
How do I test a plan before coding it?
Test the plan by hand on a small example, with concrete input and the expected answer written down first. Walk each step in order, check the intermediate values against what the step should give, and try an edge case such as an empty input before writing any code.
What is pseudocode, and when does it help?
Pseudocode is a plan written in plain words, step by step, before code is written. It helps when a problem feels vague, because rearranging a sentence is faster than rearranging a program, and a helper can read the plan without running the code.
When should I use binary search instead of linear search?
Use binary search only on a sorted list, because it discards half the list on each step based on order. Use linear search on an unsorted list, because it checks each item in turn and works regardless of order.


