You summed a list on Day 12. Today, count how many items match a condition — say, how many amounts are strictly greater than 40. How do you combine a loop with a filter?
Start at zero, loop through, add one every time the condition is true?
That's the conditional counter. Same three beats as the accumulator — initialize, loop, return — plus an if inside the loop that decides whether to bump the counter:
count = 0
for amount in amounts:
if amount > limit:
count += 1The counter only moves when the condition fires. Everything else passes through.
What if no items match — is the return value correct automatically?
Yes. If nothing matches, the if body never runs and count stays at 0. Same for an empty list — the loop body never runs. Both edge cases resolve to 0 without special-casing.
Is there a shorter way? This feels like four lines for a simple count.
There is — sum(1 for a in amounts if a > limit) does the same thing in one expression. But the explicit loop is worth writing a few times first. Once you see the shape — initialize → loop + filter → return — you'll recognize it in dozens of other problems.
def count_over_limit(amounts, limit):
count = 0
for amount in amounts:
if amount > limit:
count += 1
return countSo the difference between total_expenses and count_over_limit is two characters — += amount becomes += 1, and there's an if guarding the update.
Exactly. Same skeleton, different muscle. That's why the accumulator shape is worth learning once and reusing forever.
TL;DR: accumulator + filter — bump the counter only when the condition is True.
count = 0for item in items:if item > limit:count += 1| Goal | Inside the loop |
|---|---|
| Sum | total += amount |
| Count | count += 1 |
| Filtered count | if ...: count += 1 |
| Filtered sum | if ...: total += amount |
Four variations, one shape. Once you see it, you see it everywhere.
Write `count_over_limit(amounts, limit)` that returns how many items in the list are strictly greater than the limit.
Tap each step for scaffolded hints.
No blank-editor panic.
You summed a list on Day 12. Today, count how many items match a condition — say, how many amounts are strictly greater than 40. How do you combine a loop with a filter?
Start at zero, loop through, add one every time the condition is true?
That's the conditional counter. Same three beats as the accumulator — initialize, loop, return — plus an if inside the loop that decides whether to bump the counter:
count = 0
for amount in amounts:
if amount > limit:
count += 1The counter only moves when the condition fires. Everything else passes through.
What if no items match — is the return value correct automatically?
Yes. If nothing matches, the if body never runs and count stays at 0. Same for an empty list — the loop body never runs. Both edge cases resolve to 0 without special-casing.
Is there a shorter way? This feels like four lines for a simple count.
There is — sum(1 for a in amounts if a > limit) does the same thing in one expression. But the explicit loop is worth writing a few times first. Once you see the shape — initialize → loop + filter → return — you'll recognize it in dozens of other problems.
def count_over_limit(amounts, limit):
count = 0
for amount in amounts:
if amount > limit:
count += 1
return countSo the difference between total_expenses and count_over_limit is two characters — += amount becomes += 1, and there's an if guarding the update.
Exactly. Same skeleton, different muscle. That's why the accumulator shape is worth learning once and reusing forever.
TL;DR: accumulator + filter — bump the counter only when the condition is True.
count = 0for item in items:if item > limit:count += 1| Goal | Inside the loop |
|---|---|
| Sum | total += amount |
| Count | count += 1 |
| Filtered count | if ...: count += 1 |
| Filtered sum | if ...: total += amount |
Four variations, one shape. Once you see it, you see it everywhere.