Sub topic 4
Code in Sequence: Adding Costs
This chapter assumes you can read Python loops and count the iterations of a single loop from its bounds and step size. Here you combine those counts. When blocks of code run one after another, their costs add.
The Rule
Suppose a function runs block A, then block B, then block C. Each block finishes before the next one starts, so the total step count is:
T = T(A) + T(B) + T(C)
Each block's cost comes from the rules you already know. A constant-time statement costs 1 step. A loop costs its iteration count times the work per iteration. The function's total is the sum of those costs.
A Worked Example
The function below counts how many values lie above the mean. It counts one step for each constant-time statement we track and one step for each loop iteration.
def above_mean(values):
steps = 0
n = len(values) # block A: 1 step
steps += 1
total = 0
for v in values: # block B: n steps
total += v
steps += 1
mean = total / n # block C: 1 step
steps += 1
above = 0
for v in values: # block D: n steps
if v > mean:
above += 1
steps += 1
return above, steps
print(above_mean([3, 1, 4, 1, 5, 9, 2, 6])) # (4, 18)With n = 8, the prediction is 1 + n + 1 + n = 2n + 2 = 18. The counter agrees.
| Block | What it does | Cost |
|---|---|---|
| A | Read the length | 1 |
| B | Sum the values | n |
| C | Divide | 1 |
| D | Compare each value to the mean | n |
| Total | 2n + 2 |
Different Inputs, Different Variables
If two sequential loops run over different inputs, give each input its own size variable.
def total_length(a, b):
steps = 0
for x in a: # len(a) steps
steps += 1
for y in b: # len(b) steps
steps += 1
return steps
print(total_length([1, 2, 3], [4, 5, 6, 7, 8])) # 8With n = len(a) and m = len(b), the cost is n + m. You cannot shrink this to a single variable unless you know how n and m relate. If b is much longer than a, the m term carries almost all the cost.
Branches Inside a Sequence
An if/else block runs only one of its branches, so its cost is the cost of the branch that runs. If one branch is a loop of n iterations and the other is a single statement, the block costs n steps on some inputs and 1 step on others. To get an upper bound, take the more expensive branch. A later chapter covers best and worst cases in detail.
When the Sum Gets Simplified
Exact sums such as 2n + 2 or n + m + 5 are worth writing down first, because they show where the cost comes from. A later chapter shows how to reduce them to an order of growth. For now, notice that as n grows, the constant parts (the 1s and 2s) become a smaller share of the total, and the loop terms account for most of it.
Exercise
A function runs for i in range(n) with a one-step body, then for j in range(0, n, 2) with a one-step body, then three constant-time statements. Counting one step per iteration and one per statement, what is the exact count for n = 10, and what is the general formula?
Show answer
The first loop runs n = 10 times. The second runs ceil(10 / 2) = 5 times. The statements add 3. The total is 10 + 5 + 3 = 18. In general, the count is n + ceil(n/2) + 3.
Exercise
A function loops over a list a of length n, then loops over a list b of length m, then loops over a again. Each loop body costs one step. What is the total, and why is it not n m n?
Show answer
The total is n + m + n = 2n + m. The three loops run one after another, and none is nested inside another, so their costs add instead of multiplying.