A coding problem often feels difficult because it is still a cloud of words. Before choosing an algorithm, turn that cloud into a small, precise job.

Find the four important pieces

  • Input: what information are you given?
  • Output: what exactly must you return?
  • Rules: what is allowed or forbidden?
  • Examples: what should happen in small cases?

Suppose the task says: return the largest number in a list. The input is a list of numbers. The output is one number. A useful question is whether the list can be empty.

Say the solution in plain language

Try to explain your plan without code: keep the largest value seen so far, compare it with every new value, and update it when needed. If the sentence is unclear, the code will probably be unclear too.

Translate the plan into PythonPython
def largest(numbers):
    best = numbers[0]

    for number in numbers[1:]:
        if number > best:
            best = number

    return best

print(largest([4, 2, 9, 3]))  # 9

This habit prevents you from solving the wrong problem beautifully. It also reveals missing details early, when they are cheap to fix.

Your tiny challenge

Describe a function that counts red toys. Write its input, output, one rule, and two examples before writing any code.