Skip to content
CodeItRaw
Basics

Lesson 5/7

To find the largest in a list you do not need to see them all at once. Hold a "largest so far"; at each new element compare: if it is larger, it takes the place.

best = values[0]
for v in values:
if v > best:
best = v
return best
values[0] is the list's first element (counting starts at 0). Starting the search with a value from the list keeps the answer from being a number that is not in it.

The same idea finds the smallest, the last one seen, the longest. You can carry two things at once: two variables for the smallest and the largest, one loop.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.Go to the current lesson

  1. 01

    The Largest

    Function

  2. 02

    Spread

    Function

  3. 03

    Last Seen

    Code reading · Break the Code

If you would rather not wait for the order, every problem is open without locks: Problem list