W WolfCode · CSC 141

Find the Max

找最大值

≈ 15 min · python-loops · Open in WolfCode

The tracking-variable pattern. Initialize with the FIRST element — not zero — and your function survives the all-negatives case.

The pattern

Python · runnable

The trap: initializing to 0

What does this return for all-negative input?

Check yourself

What's the SAFEST initial value if the input could be ANY positive or negative numbers (but is non-empty)?

The pattern generalizes

The same three-step shape — initialize / update conditionally / return — drives find_min, find_longest_word, count_evens, and a dozen others. Once you can write find_max, you can write all of them.

Now you try

Open 03-find-max.py. 6 tests including the all-negatives edge case and a check that you didn't use the built-in max().