The Largest Value

The next pattern finds the largest value. Imagine Emma at the village fair, looking for the heaviest pumpkin. She lifts the first one. Then she goes from pumpkin to pumpkin, and whenever one is heavier than the heaviest one so far, she remembers that one instead. At the end, she knows the heaviest pumpkin. In code, “the heaviest one so far” is a variable:

local pumpkins = {8, 12, 7, 15, 10}
local heaviest = pumpkins[1]
for i = 2, #pumpkins do
    if pumpkins[i] > heaviest then
        heaviest = pumpkins[i]
    end
end
print("Heaviest: " .. heaviest .. " kg")

This prints:

Heaviest: 15 kg

heaviest starts with the first item, so the loop can start at index 2: there's no need to compare the first pumpkin with itself (starting at 1 would work too). 12 is more than 8, so heaviest becomes 12. 7 isn't more than 12, but 15 is, and 10 isn't more than 15, so 15 stays until the end.

Why not start with 0? Because 0 might not be one of the values. If all the values were negative, such as the winter temperatures {-5, -2, -8}, the starting 0 would stay the largest, and the program would print a temperature that isn't in the list at all. The first item is always a safe start.

You find the smallest value the same way, only with < instead of >.

You may remember math.max, which gives the largest of several numbers: math.max(8, 12, 7) is 12. But it wants the numbers themselves, not a list. math.max(pumpkins) doesn't look inside the list: with a single value, it simply gives that value back, so print(math.max(pumpkins)) prints table: 0x…. For a list, you need the loop.

Often you also need to know where the largest value is. Then keep its index in a second variable, and change both together. Here, ipairs gives you the index and the item at once:

local pumpkins = {8, 12, 7, 15, 10}
local heaviest = pumpkins[1]
local heaviestIndex = 1
for i, weight in ipairs(pumpkins) do
    if weight > heaviest then
        heaviest = weight
        heaviestIndex = i
    end
end
print("Pumpkin " .. heaviestIndex .. " weighs " .. heaviest .. " kg")

This prints:

Pumpkin 4 weighs 15 kg

Because Lua counts from 1, the index is directly the pumpkin's number, counted the way people count: the fourth pumpkin has index 4. With the index, you can also look into another list whose items belong to the same positions, such as the names of the farmers who grew the pumpkins: growers[heaviestIndex].

If two pumpkins weigh the same, > keeps the first of them, because a pumpkin that's only as heavy doesn't replace it. With >=, you would get the last one.

Instructions

Find the most coins Emma earned in one day. Create a local variable best with the first item of coins, go through the list, and whenever an item is greater than best, store it in best. After the loop, print Most coins: 52.

Find out which day it was. Next to best, create a local variable bestIndex with the value 1, and in the loop, store the index in it whenever you change best. After the loop, use it to get the day's name from days, and print Best day: Tuesday.

In a second loop, find the worst day the same way: the one with the fewest coins. Print its name and its coins: Worst day: Wednesday (18 coins).

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/11