Exit
  1. Learn
  2. Check
  3. Solve
  4. Reflect

One walk, two facts

A weather app shows today's low and high. It doesn't scan the day's readings twice: it looks at each reading once and checks both things as it goes.

You can do the same. Keep two variables, the lowest so far and the highest so far, and update both at every box. One loop, two answers.

Two variables, one loop: check both at every box.
Track low and high
4
0
1
1
7
2
3
3
i

low = 4, high = 4.

Move 1 of 4

Both start at the first box

Before you've seen anything else, the first value is both the lowest and the highest so far. So start both variables there, for the same reason as last time: it's always a real value.

Use two separate checks, one for "lower?" and one for "higher?". A value can only change one of them, but you have to ask both questions.

Start both at the first box: it's the lowest and highest so far.
Two separate ifs: each variable needs its own check.
In code
low = high = nums[0]
for x in nums:
    if x < low:
        low = x
    if x > high:
        high = x
return [low, high]