Home / Reasoning / Biggest So Far: Find the Largest Value in a List
Reasoning & Coding · Coding
Biggest So Far: Find the Largest Value in a List
Loop through a list and keep the biggest value seen so far. Trace a max variable, flip it to find the smallest, and fix four common bugs.
The idea
Imagine a line of classmates walking past you one at a time, and you need to find the tallest. You do not have to remember everyone. You only remember the tallest person so far, and each time someone taller walks past, that person becomes the new tallest so far. When the line ends, you have your answer.
A program can do the same thing with a list. One variable, often called best or max, holds the biggest value seen so far. Inside the loop, an If compares each item with best. If the item is bigger, best is replaced by it. After the loop, best is the largest value in the whole list.
The pattern has three places, like the counter pattern from the last lesson. Before the loop: Set best to the first item, once. Inside the loop: If n > best [Set best to n]. After the loop: say best, once.
Why start with the first item instead of 0? Starting at 0 works only if some item is bigger than 0. For temperatures like [-4, -9, -2, -6], best would stay 0 and the program would report a temperature that never happened. The first item is always a real value from the list.
This is a Set, not a Change. A counter changes by 1 and a total changes by the item, but best is replaced: the old value is forgotten. To find the smallest instead, flip the test to n < best (and call the variable least). Everything else stays the same.
Ties need a decision. With n > best, an equal value does not pass, so the first of the tied items is kept. With n ≥ best, a tie passes, so the last one is kept. This matters when a second variable remembers who or which day, not just the value.
Worked examples
Example 1 · Trace the biggest so far
Set scores to [6, 9, 4, 11, 8]. Set best to the first item. For each s in scores [If s > best [Set best to s]]. say best.
- Trace table: s | s > best? | best. Start: best = 6.
- 6 | no | 6. 9 | yes | 9. 4 | no | 9. 11 | yes | 11. 8 | no | 11.
- The loop ends, then say best runs once.
It says 11.
Example 2 · Why not start at 0?
Set temps to [-4, -9, -2, -6]. Compare starting best at 0 with starting at the first item.
- Start at 0: no temperature is more than 0, so best stays 0 and the program says 0.
- Start at the first item: best = -4. -9 no, -2 yes (best -2), -6 no.
- 0 is not in the list, so the first version is wrong even though the loop is fine.
Starting with the first item gives the right answer, -2.
Example 3 · Flip it to find the smallest
Lap times in seconds (invented data) are [52, 47, 49, 45, 50]. Set least to the first item. For each t in times [If t < least [Set least to t]]. say least.
- least starts at 52.
- 47 < 52 yes (least 47). 49 no. 45 < 47 yes (least 45). 50 no.
- For lap times, the smallest number is the fastest lap.
It says 45: the fastest lap.
Example 4 · Remember who, and handle a tie
Jumps in centimetres: Ali 120, Bea 135, Cy 135, Dev 128. Set best to Ali's jump and who to Ali. For each kid [If jump > best [Set best to jump; Set who to kid]]. say who.
- Ali is the start. Bea: 135 > 120 yes, so best 135 and who Bea.
- Cy: 135 > 135 is no, so Bea stays. Dev: 128 no.
- Both Set steps are inside the same If, so who always matches best.
It says Bea. With ≥ instead of >, it would say Cy, the last of the tied jumpers.
Example 5 · Fix the bug
For each n in [7, 12, 5, 9] [Set best to 0; If n > best [Set best to n]]. say best. It says 9. Why?
- Set best to 0 is inside the loop, so it runs before every item.
- Each item is more than 0, so best becomes 7, then 12, then 5, then 9.
- Move Set best before the loop, and start it with the first item.
After the fix it says 12.
Common mistakes
Why this fails: If every item is negative, best stays 0, which is not in the list. Start with the first item instead.
Why this fails: Change adds the item to best, like a total: 7 then 7 + 12 = 19. Use Set best to n so the new value replaces the old one.
Why this fails: n < best keeps the smaller value each time, so the program finds the smallest. Use n > best for the largest.
Why this fails: best is wiped on every pass, so the program reports the last item. Set it once, before the loop.
Why this fails: Then who changes on every pass and ends as the last item in the list, even when that item is not the best.
Foundation
Foundation practice
Trace a best-so-far variable and choose the right test.
1. Set nums to [3, 8, 5, 10, 7]. Set best to the first item. For each n in nums [If n > best [Set best to n]]. say best. What is said?
- 7
- 8
- 10
- 33
2. In a biggest-so-far program, why is the step Set best to n and not Change best by n?
- The new biggest value replaces the old one; it is not added to it.
- Change only works with counters, so Set is the only choice.
- Set best to n makes the loop run faster.
- Change best by n would leave best at the first item.
3. Set nums to [6, 9, 4, 11, 8]. Set best to the first item. For each n in nums [If n > best [Set best to n]]. After the first Set, how many times does best change?
- 5
- 1
- 3
- 2
4. Set nums to [14, 9, 12, 7, 10]. Set least to the first item. For each n in nums [If n < least [Set least to n]]. say least. What is said?
- 14
- 7
- 10
- 9
5. You want best to end up as the largest number in the list. Which test goes in the If?
- n > best
- n < best
- n = best
- n + best
Practice
Practice practice
Pick a safe starting value and decide what happens with ties.
6. Set temps to [-5, -2, -8, -3]. Set best to 0. For each t in temps [If t > best [Set best to t]]. say best. What is said?
- -2
- -8
- -18
- 0
7. Change the program above so that best starts as the first item (-5) instead of 0. What does it say now?
- -5
- -2
- -8
- 0
8. Set nums to [4, 4, 2, 6, 6, 1]. Set best to the first item. For each n in nums [If n > best [Set best to n]]. How many times does the If test pass?
- 2
- 3
- 1
- 0
9. Jumps: Mo 31, Lu 34, Ty 34, Jo 29 (cm). Set best to Mo's jump and who to Mo. For each kid [If jump > best [Set best to jump; Set who to kid]]. say who. Who is said?
- Lu
- Ty
- Mo
- Jo
10. Same jumps, but the test is jump ≥ best. Who is said now?
- Lu
- Ty
- Mo
- Jo
Think
Think practice
Find the bug in max and min programs and explain the pattern.
11. For each n in [7, 12, 5, 9] [Set best to 0; If n > best [Set best to n]]. say best. It says 9, not 12. What is the bug?
- The test should be n < best.
- The list is in the wrong order.
- Set best to 0 is inside the loop, so best restarts on every pass.
- say best should be inside the loop.
12. Set best to the first item. For each n in [7, 12, 5, 9] [If n < best [Set best to n]]. say best. The program should find the largest number, but it says 5. What is the fix?
- Start best at 0.
- Use Change best by n.
- Move say best before the loop.
- Change the test to n > best.
13. Set best to the first item. For each n in [7, 12, 5, 9] [If n > best [Change best by n]]. say best. It says 19, not 12. What is the bug?
- Change best by n adds; it should be Set best to n.
- The test should be n ≥ best.
- best should start at 0.
- The loop should skip the first item.
14. Set nums to [15, 22, 9, 18]. One loop finds the biggest and the smallest. Then say biggest − smallest. What is said?
- 7
- 13
- 31
- 64
15. Explain why Set best to the first item is safer than Set best to 0.
Challenge
Challenge practice
Count records, add a second test and remember where the best was.
16. Set d to [3, 7, 2, 9, 9, 4]. Set best to the first item. Set records to 0. For each n in d [If n > best [Set best to n; Change records by 1]]. say records. What is said?
- 3
- 6
- 1
- 2
17. Set nums to [7, 12, 5, 18, 9, 14]. Set best to 0. For each n in nums [If n is even and n > best [Set best to n]]. say best. What is said?
- 14
- 12
- 18
- 9
18. Temperatures for days 1 to 5 are [3, -1, 4, -6, 2]. Set least to the first item and day to 1. For each day d [If temp < least [Set least to temp; Set day to d]]. say day. What is said?
- 4
- 2
- 5
- -6
19. Snowfall in centimetres for five days (invented data) is [12, 0, 25, 7, 18]. Write a program that finds the most snow in one day. What does it say?
20. Daily high temperatures (invented data) are [4, 9, 9, 6, 2, 7] for days 1 to 6. Design one loop that finds the warmest temperature and the first day it happened. Then say how you would change it to report the last day instead.
Answer key
- 1. 10
best goes 3, then 8, then 10, and 5 and 7 are not bigger, so it says 10. 7 is just the last item, 8 stops at the first new best, and 33 adds the items like a total. - 2. The new biggest value replaces the old one; it is not added to it.
best should hold one value from the list, the biggest seen so far, so the new value replaces the old one. Change by n would add them together, like a total. Change works on any variable, speed is not the reason, and Change would make best grow, not stay at the first item. - 3. 2
Only 9 (bigger than 6) and 11 (bigger than 9) beat the best so far: 2 changes. 5 is the number of items, 1 misses the 11, and 3 wrongly counts 8, which is less than 11. - 4. 7
least goes 14, then 9, then 7; 12 and 10 are not smaller. 14 is where it started, 10 is the last item, and 9 is the first new smallest, not the final one. - 5. n > best
Replace best only when the new item is bigger: n > best. n < best finds the smallest, n = best only matches ties, and n + best is a number, not a yes-or-no test. - 6. 0
No temperature is more than 0, so best never changes and the program says 0, a number that is not even in the list. -2 is the right warmest day, -8 is the coldest, and -18 is the total. - 7. -2
-2 > -5, so best becomes -2; -8 and -3 are not bigger than -2. -5 is only the start, -8 is the smallest, and 0 is the old bug. - 8. 1
Only the first 6 is more than 4. The first item is compared with itself (4 > 4 is no), and the second 6 is not more than 6. 2 counts both 6s, 3 also counts a 4, and 0 forgets the first 6. - 9. Lu
Lu's 34 beats 31. Ty also jumped 34, but 34 > 34 is no, so the first 34 is kept. Mo was only the starting best, and Jo's 29 never beats anything. - 10. Ty
With ≥, a tie also passes, so Ty's 34 replaces Lu's 34 and the last of the tied jumpers is kept. Lu is the answer for >, Mo is the starting value and Jo is the shortest jump. - 11. Set best to 0 is inside the loop, so best restarts on every pass.
Because best is wiped before every item, each item beats 0 and the last one, 9, wins. Set best once, before the loop. n < best would look for the smallest, the order of the list does not matter in a correct program, and saying inside the loop only shows the same mistake many times. - 12. Change the test to n > best.
With <, the program keeps the smaller number each time, so it finds the smallest, 5. Starting at 0 with < would say 0, Change by n adds values together, and saying before the loop reports only the first item. - 13. Change best by n adds; it should be Set best to n.
12 is bigger than 7, but Change best by 12 makes best 7 + 12 = 19. Set best to n would make it 12. Using ≥ or starting at 0 would still add, and skipping the first item does not stop the adding. - 14. 13
The biggest is 22 and the smallest is 9, and 22 − 9 = 13. 7 is 22 − 15 (the first item, not the smallest), 31 adds the biggest and smallest, and 64 is the total of the list. - 15. The first item is a real value from the list, so the answer will always be one of the items. Starting at 0 only works when some item is bigger than 0; with a list like [-5, -2, -8], best stays 0, which is not in the list.
Strong answers give an example with negative numbers (or a smallest-value search, where 0 would be smaller than every positive item). - 16. 2
New records happen at 7 and at the first 9. The second 9 only ties, so it is not more than best. 3 counts the tie, 6 counts every item, and 1 misses one of the records. - 17. 18
The even numbers are 12, 18 and 14, and the biggest is 18. Starting at 0 is fine here because every item is positive. 14 is the last even number, 12 is the first, and 9 is the biggest odd number, which the even test leaves out. - 18. 4
The coldest is −6, on day 4. Day 2 (−1) was only the first new low, day 5 is the last day, and −6 is the temperature, not the day. - 19. Set snow to [12, 0, 25, 7, 18]. Set most to the first item. For each s in snow [If s > most [Set most to s]]. say most. It says 25.
most goes 12, then 25, and nothing beats 25. Set most once before the loop, use > in the If and Set (not Change) inside it. - 20. Set best to the first item and day to 1. For each day d [If temp > best [Set best to temp; Set day to d]]. It says 9 on day 2. Using ≥ instead of > reports the last day with 9, which is day 3.
> keeps the first of a tie and ≥ keeps the last. Both Set steps belong inside the same If so the day always matches the temperature.