Four runners finish a race in Leeds. How can your code find the fastest time?
Four runners have just finished a race in Leeds. You need to record their times, correct one that was entered incorrectly, then find the fastest time. How could you do that without making a separate variable for each runner?
If one runner takes fourteen seconds and another takes sixteen, you can tell who was faster. Fourteen is less than sixteen, so this comparison is true. You can use that same comparison each time you meet a new race time.
Put sixteen, eighteen, fourteen and fifteen in order under one name, times. Each value is an element. Use its position number, called an index, to find it. Python starts at zero. A Python list works like a one-dimensional array here: you need one index to select a time.
Your list holds sixteen, eighteen, fourteen and fifteen in that order. You want the time for the third runner, which is fourteen seconds. Counting from zero, which index gets that time? Pick an answer.
First, store the four times in the list. Reading times at index one prints eighteen. That time was entered wrong, so assign thirteen to the same index. Print it again and you get thirteen. The other elements stay as they were.
Start with the first time as best. The range starts at one and stops before the list length, four, so it gives one, two and three. At each index, replace best only if that time is smaller. After the loop, print best.
Follow the loop with the corrected times. Best starts at sixteen. Thirteen is smaller, so best becomes thirteen. Fourteen is greater than thirteen, so best stays put. Fifteen is greater too. The loop prints the number thirteen, which represents thirteen seconds.
Now the times are twenty, seventeen, nineteen and sixteen seconds. Start with twenty as best. Compare each time in order and change best only when you find a smaller one. Which time does the loop print? Pick an answer.
In Paper One, check the given starting index. With four elements starting at zero, the indices run from zero to three, so index four is out of range. With four elements starting at one, the indices run from one to four. The length alone doesn't tell you the last index.
For several heats with the same number of runners, use a two-dimensional array. Give each heat a row and each runner position a column. Use two indices to select a time. To keep a runner's name with their time, use a record with two fields. Compare runners using their time fields.