Week 14 — Laps That Add Up — Traditional alternative (no AI)
Instructors: publish this instead of the adaptive version if you prefer, and unpublish or delete the adaptive one.
Assignment 13 of the term — trace a recursive function call by call, count the calls it makes, catch a base case in the wrong place, and explain it all to someone who has never programmed.
This week you learned that a recursive function needs a base case that answers at once and a recursive case that moves toward it, that the calls pile up on the call stack and unwind in reverse order, and that factorial(5) makes six calls before it hands back 120. In this assignment you trace four short problems, run each one to check your prediction, run the extra programs that show where a recursion stops, and explain a real mix-up to someone who has never programmed. This builds on Week 7 and Week 9. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles.
The task
A cousin who helps coach a youth swim team wrote a short program to add up the laps four swimmers swam at practice:
def total_laps(laps):
if len(laps) == 1:
return 0
return laps[0] + total_laps(laps[1:])
print(total_laps([10, 8, 13, 6]))
It printed 31. Your cousin says, "Recursion must skip a swimmer sometimes." Your job is to check what Python really did, the way a programmer would: predict each output before you run it, run it in a free browser Python runner, and explain what happened. All four problems are illustrative; Problem 1 uses this week's reading log, and Problems 3 and 4 use the cousin's laps, repaired so that the base case is the empty list:
def total_laps(laps):
if laps == []:
return 0
return laps[0] + total_laps(laps[1:])
Work these four problems. For each one, write your prediction first, then run the code and write the actual output beside your prediction:
- The reading log's Week 14 function,
total_pages, is called on the first three days,total_pages([12, 40, 25]), and the program prints the value. What does it print? - The pinned
factorialfrom this week's chapter is called asprint(factorial(4)). What does it print? - The repaired
total_lapsis called on[10, 8, 13, 6]. Addprint("call with", laps)as the first line of its body and run it. How many calls does it make in all, counting the base-case call? - The program runs
print(total_laps([10, 8, 13, 6]))with the repaired function. What does it print?
Beside each answer, add one brief interpretation:
- Problem 1: name the base case of
total_pages, and report whatprint(total_pages([]))prints when you run it. - Problem 2: list the values the calls hand back as the frames unwind, and explain why each call keeps its own
n. - Problem 3: explain why there is one more call than there are swimmers, and explain why each slice leaves the original list alone.
- Problem 4: run your cousin's original version, report what it prints, name what is wrong with its base case, and explain why a base case on the empty list fixes it.
Explain it to a non-programmer
Write a 3–5 sentence message to your cousin that explains, in everyday words, why one swimmer went missing from the total, and how changing the stopping rule fixes it. Use the idea that each call hands the rest of the list to the next call and that the last call, with nothing left, should answer zero, and name one limit or caveat, such as what happens if a function's input never shrinks toward its stopping rule. Explain any technical term in everyday words. More than one good message is possible.
For a different, ungraded example, imagine a friend whose countdown for a game prints Go! before the numbers. Model message: "Your countdown asks the next, smaller countdown to run before it says its own number, so every number waits until the very last step has shouted Go Python did exactly what the instruction said, which is why there was no error message, only numbers in an odd order. If you move the line that says the number above the line that starts the smaller countdown, the numbers come first. One catch: the smaller countdown has to reach zero at some point, or the program never stops."
Use that pattern for your cousin's question, with your own wording and evidence.
Show your traces. A reader should be able to follow every prediction call by call and see the actual output of your run beside it, without guessing which list each call received or what each call handed back.
Deliverables
Submit a single report containing:
- the four answers, each with your prediction, the step-by-step trace, the actual output of your run, and the brief interpretation the task names for that problem;
- the runs of
print(total_pages([])), the traced call count, and your cousin's original version, each with what printed; and - your message to your cousin, explaining why one swimmer went missing, how changing the stopping rule fixes it, and one limit.
Keep it to roughly one page. Clear and correct beats long.
Choose how you show it. Present your report as written text, or as a recorded audio or video explanation of about three minutes with a transcript, or as labelled screen captures of your runs with a short note for each. Every form is graded by the same scoring criteria. Submit in any accessible file type or as a link; a recorded response may be submitted as audio or video with a transcript, or as text.
How you'll be graded
The full analytic rubric is attached to this assignment in Canvas.
Academic integrity and AI use
This version is designed to be completed without AI: make the predictions, run the code, and write the interpretations and the message yourself, without any AI tool. You may use a free browser Python runner, Python Tutor, and the official Python documentation. Submitting AI-generated work as your own, or copying another student's work, is an integrity violation under the course policy.
Built with The Course Maker · samples.thecoursemaker.com