You want the largest amount in a list. The accumulator pattern totals a list — what's the shape for finding the max instead?
Take the first element as the champion, then loop through and replace it whenever I find something bigger?
Exactly. It's the mirror of the accumulator: instead of adding everything up, you keep the best value you've seen so far:
highest = amounts[0]
for amount in amounts:
if amount > highest:
highest = amountIf you find something bigger, it becomes the new champion.
Why start at amounts[0] instead of 0?
Because zero might be bigger than every actual value — if all your amounts were negative, starting at zero would give the wrong answer. Starting at the first element guarantees you always return a value that's actually in the list.
What about an empty list? amounts[0] would crash.
That's the one edge case you guard. Empty list, return a safe default — 0.0 works for this track. Put the guard at the top, before you touch amounts[0]:
def find_highest_expense(amounts):
if not amounts:
return 0.0
highest = amounts[0]
for amount in amounts:
if amount > highest:
highest = amount
return highestnot amounts is True when the list is empty — empty collections are falsy in Python, a shortcut worth internalizing.
So the shape is: guard empty → champion from first element → loop and replace. Three clean steps.
Three steps, one pattern. Same structure applies to finding the min, the longest string, the smallest dict — anything where "best so far" is the answer.
TL;DR: champion from list[0], replace on each better value.
if not amounts: return 0.0amount > highest| Value | not value |
|---|---|
[] | True |
{} | True |
"" | True |
[0] | False (non-empty) |
not amounts is the idiomatic empty-check. Python has max() built in; you're writing the loop to learn the shape.
Write `find_highest_expense(amounts)` that returns the max value in a list of floats. Return 0.0 for an empty list.
Tap each step for scaffolded hints.
No blank-editor panic.
You want the largest amount in a list. The accumulator pattern totals a list — what's the shape for finding the max instead?
Take the first element as the champion, then loop through and replace it whenever I find something bigger?
Exactly. It's the mirror of the accumulator: instead of adding everything up, you keep the best value you've seen so far:
highest = amounts[0]
for amount in amounts:
if amount > highest:
highest = amountIf you find something bigger, it becomes the new champion.
Why start at amounts[0] instead of 0?
Because zero might be bigger than every actual value — if all your amounts were negative, starting at zero would give the wrong answer. Starting at the first element guarantees you always return a value that's actually in the list.
What about an empty list? amounts[0] would crash.
That's the one edge case you guard. Empty list, return a safe default — 0.0 works for this track. Put the guard at the top, before you touch amounts[0]:
def find_highest_expense(amounts):
if not amounts:
return 0.0
highest = amounts[0]
for amount in amounts:
if amount > highest:
highest = amount
return highestnot amounts is True when the list is empty — empty collections are falsy in Python, a shortcut worth internalizing.
So the shape is: guard empty → champion from first element → loop and replace. Three clean steps.
Three steps, one pattern. Same structure applies to finding the min, the longest string, the smallest dict — anything where "best so far" is the answer.
TL;DR: champion from list[0], replace on each better value.
if not amounts: return 0.0amount > highest| Value | not value |
|---|---|
[] | True |
{} | True |
"" | True |
[0] | False (non-empty) |
not amounts is the idiomatic empty-check. Python has max() built in; you're writing the loop to learn the shape.