Problem Solving Fundamentals · Approaching a Problem

Solve a simpler case first

simplifying the input

Solve a simpler case first

Some inputs arrive messy: long, irregular, full of detail you did not ask for. Trying to handle all of it at once is how a function grows knotted and wrong.

Take this task: pull the fields out of a CSV line. Real lines carry quoting, so this one hides a comma inside the first field:

"Ada, Lovelace",36,London

Parse that in one pass and you will get tangled. Do not start with the mess. Start with a cleaner version of the same input:

Ada,36,London → ["Ada", "36", "London"]

For that line the rule is a single sentence: split on every comma. You can write it, run it, and see it work. The clean case is solved.

Now add the mess back, one fact at a time. A quote can wrap a field, so a comma inside quotes is not a separator. Fields can carry spaces around them, so trim each one. Each new fact is a small change to a function that already works, and you can check it against a real line as you go.

That is the move: shrink the input until the rule is obvious, solve that, then widen the input and patch the rule. You never face the whole mess cold.

You are about to answer a short quiz on simplifying the input.

  1. Solving a smaller, cleaner version of an input first can make the general rule obvious.
  2. Reducing a messy input to a clean one before generalising is called ___ the input.
  3. After solving the simplified case, you add the messy details back one at a time.
  4. For a simple comma-separated line, the rule can be stated as a single ___.
  5. Solving a simplified input means you no longer have to handle the original messy input.
  6. You simplify the input until the ___ is obvious, then widen the input back out.