Skip to content
CodeItRaw
Arrays

Lesson 2/6

Most questions about a list are finished by walking it once from start to end. The trick is to carry what you need in a variable as you go: a sum, a counter, a best.

total = 0
for v in values:
if low <= v <= high:
total += v
return total
In Python low <= v <= high makes both comparisons at once: is v inside the range? Both ends are included.

On this site every read of an element is counted. Walking a list of n elements once is n reads; walking the list again for each element is n × n. When the list grows 10 times, the first gets 10 times slower and the second 100 times.

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

    Signal Sum

    Function

  2. 02

    Positive Sum

    Code reading · Review the AI

  3. 03

    How Many in Range?

    Code reading · Break the Code

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