Algorithmic Thinking & Problem Solving~20 min

Abstraction and Modeling

The same real-world thing can be represented many different ways — choosing deliberately which details matter is what makes a model useful.

By the end of this lesson, you can

  • Explain abstraction as deliberately choosing which details of a real-world thing to represent
  • Build a model of a scenario using only the details a specific problem actually needs
  • Explain why representing irrelevant detail can make a problem harder to solve, not easier

Why it matters

A real classroom has students with names, heights, favorite colors, seating positions, and countless other details. A program almost never needs all of that. Abstraction is deciding, on purpose, which details a problem actually requires — and a model is the concrete representation built from that decision. Picking the wrong level of detail doesn’t just waste effort; it makes the actual problem harder to solve, buried under details nobody asked for.

Mental model

The same real-world thing can have several valid models, because “valid” depends entirely on the question being asked:

  • “How many students are in the class?” only needs a count.
  • “What are the students’ names, in signup order?” needs an ordered list of names.

Neither model is more “correct” than the other in general — each is correct for its own problem, and wrong for the other’s.

PythonTwo different models of the same classroom
class_size = 24

student_names = ["Ada", "Grace", "Linus"]

class_size answers “how many?” directly, with no wasted detail. Given student_names instead, “how many?” is still answerable — len(student_names) — but only because that model happened to include more than the count question needed. If the problem only ever asks “how many?”, building the full list of names is unnecessary work for no benefit.

Choose the model

Matching a question to the model that actually answers it
QuestionMinimal model needed
How many students are enrolled?A single number (a count)
What are the students' names, in order?A list of names
Is a specific student, by name, enrolled?A list of names (or later, a set — see the data structures module)
What's the average of the students' test scores?A list of scores — names aren't needed here at all

Notice the last row: even though the scenario is still “a classroom,” a problem only about scores doesn’t need student names in its model at all.

Check your understanding

A program only needs to answer 'what was the highest temperature recorded today?' Which model fits best?
Or reveal the answer without checking

Answer:A list of every temperature reading
Finding the highest value only requires the values themselves — a list of temperature readings is exactly enough detail, no more, no less.

Why can representing more detail than a problem needs make it harder to solve?
Or reveal the answer without checking

Answer:Extra detail adds more state to track and more edge cases to consider, without helping answer the actual question
Every extra piece of tracked detail is something that has to stay consistent and correct, even though the problem never actually asked for it — cost with no corresponding benefit.

Practice: warm-up

For each problem, decide the minimal model needed: (1) “How many books are in the library?” (2) “Which specific books are currently overdue?” (3) “What’s the average length, in pages, of all the books?”

Stuck? Reveal one hint at a time.

  1. Hint 1

    Ask, for each one: does the answer require knowing about individual books at all, or just a summary number?

  2. Hint 2

    Problem 2 is the only one where you need to identify which specific books — the others can be answered from summary information alone.

Reveal one reasonable set of models

Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.

1. How many books total?        -> a single count
2. Which books are overdue?     -> a list of the specific overdue books
3. Average page length?         -> a list of page-length numbers (titles not needed)

Practice: apply it

This function only needs to report the average of a list of scores, but it tracks far more than that:

def summarize(names, scores):
    total = 0
    highest_name = names[0]
    highest_score = scores[0]
    for i in range(len(names)):
        total = total + scores[i]
        if scores[i] > highest_score:
            highest_score = scores[i]
            highest_name = names[i]
    print("Top scorer:", highest_name)
    return total / len(scores)
If the only requirement is 'return the average score,' what's the problem with this function?
Or reveal the answer without checking

Answer:It tracks names and the highest scorer even though the stated problem never asked for either — unnecessary detail and complexity for what should be a simple average
Everything involving names and highest_name is extra state the actual problem — 'return the average' — never asked for. It adds complexity (and an extra way to introduce a bug) with no benefit to the stated requirement.

Modification challenge: rewrite summarize to take only a list of scores and return their average — nothing about names or a highest scorer, since the problem never asked for either.

Summary

  • Abstraction is deliberately choosing which details of a real-world scenario matter for the current problem, and leaving the rest out.
  • A model is the concrete representation that results — and the “right” model depends entirely on the problem, not on the real-world thing being modeled.
  • Representing more detail than a problem needs isn’t safer — it’s extra state to keep correct, for no benefit.
  • Before building a model, ask what the problem actually needs to answer, and stop there.