Pattern Recognition

Concepts covered: Iteration

Most programming problems follow common patterns, and once you recognize one you can reuse a known approach. Experienced programmers solve problems faster mostly because they've seen the pattern before.

The accumulator pattern

This pattern builds up a result by processing items one at a time. You start with an initial value and update it in a loop.

# Sum all numbers
numbers = [4, 7, 2, 9]
total = 0
for num in numbers:
    total = total + num
print(f"Sum: {total}")

Output:

Sum: 22
# Count items matching a condition
words = ["apple", "banana", "cherry", "apricot"]
count = 0
for word in words:
    if word.startswith("a"):
        count = count + 1
print(f"Words starting with 'a': {count}")

Output:

Words starting with 'a': 2

Accumulator, search and filter cover the vast majority of beginner problems. Picking which one applies is often the hardest part, and the checklist below is a quick reference for that.

Pattern selection checklist

The search pattern

This pattern looks for something in a collection, returning early when found.

def contains_negative(numbers):
    for num in numbers:
        if num < 0:
            return True
    return False

print(contains_negative([5, 3, -2, 8]))
print(contains_negative([5, 3, 2, 8]))

Output:

True
False

The filter pattern

This pattern creates a new collection containing only items that match a condition.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

evens = []
for num in numbers:
    if num % 2 == 0:
        evens.append(num)

print(f"Even numbers: {evens}")

Output:

Even numbers: [2, 4, 6, 8, 10]

Once you can tell which pattern fits, you know the shape of the solution before you write any code.

More complex algorithms are usually combinations of these 3 patterns. A 2-pass algorithm might filter the data on the first pass and accumulate a result on the second, and once you can see those parts, even advanced code gets much easier to read.

About this lesson

This section is part of the Problem Solving: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.