← Back to course
Code Crew G5 / Week 05 / Friday
5/6
Week 05 Β· Loop or recursion?

Friday

Which one should I pick?
// The same job, two ways
⏱ about 15 min

Friday: Which one should I pick?

Nova has collected 2,000 glow readings. Rocket runs the recursive total on them.

A red error fills the screen: RecursionError. "But my total function works!" Rocket says. "It worked on three numbers."

Raven counts on her fingers. "What do you notice about how many trays that would need?"

"One tray for every reading," Rocket says. "That is 2,000 trays!"

Nova dims her lights. "Here is a hint. Python only lets the stack get so tall."

A limit on the stack

Each recursive call adds a tray to the call stack. Python stops at about 1,000 trays and shows a RecursionError.

The recursive total makes one call per item, plus one for the empty list. So 2,000 readings is too deep.

A loop does not add trays. It handles a long list easily.

def total_loop(nums):
    total = 0
    for n in nums:
        total = total + n
    return total

readings = list(range(1, 2001))
print(len(readings))
print(total_loop(readings))
Output:
2000
2001000
How many calls does the recursive total make for a list of 10 items? Count the first call and the empty list.
WHY THIS EXERCISEEvery item needs its own call before the base case is reached.

How to choose

Pick a loop whenPick recursion when
you repeat the same step many timesthe problem has smaller copies of itself inside
the list could be very longyou split a problem into halves (coming in week 9)
a running total or count is all you needdata is nested, like a box in a box (coming in week 10)
LOOP OR RECURSION?
  • Read the question.
  • Tap your answer.
Add up 5,000 glow readings.
Open every box in a crate of nested boxes.
Print the numbers 1 to 10.
StatementTrue or false?
A loop and a recursion can give the same answer.?
Recursion is always the better choice.?
A very deep recursion can cause a RecursionError.?
WHY THIS EXERCISEEngineers pick the tool that fits the job, not the newest one.

Practice lab: how deep?

def depth(n):
    if n == 0:
        return 0
    return 1 + depth(n - 1)

print(depth(50))
print(depth(500))
What does print(depth(50)) show?
How many calls does depth(50) make, counting depth(0)?
What does print(depth(500)) show?
What does total_loop(list(range(1, 11))) return?
DEEP OR NOT?
  • Read the question.
  • Tap your answer.
What would depth(5000) most likely do?
Look inside folders that hold more folders.
Count 10,000 steps from a step counter list.
StatementTrue or false?
depth(500) needs more trays than depth(50).?
A loop adds a tray for every pass.?
total_loop can add 2,000 numbers with no error.?
The best tool depends on the job.?
WHY THIS EXERCISELoops reuse one spot. Recursion stacks a tray per call.
How many trays does the recursive total need for a list of 3 items? Count the empty list call.
WHY THIS EXERCISEEvery item needs a call, and the empty list needs one more.

Week 5 complete! You compared two ways to solve one problem, just like real engineers do. Next week we start searching the star catalog.

← Thursday