{"id":"code-col-trace-recursion-numbered-calls","audience":"college","category":"coding","track":"coding","technique":"format","title":"Trace a recursive function one call per line","free":false,"task":"You're reviewing recursion for your CS 1 midterm and want to see every call of sum_digits(4096). Ask for exactly 5 numbered lines, one per call, each in the form 'sum_digits(4096) = 6 + sum_digits(409)', with the base case as line 5, then one last unnumbered line 'Total: N'. No code blocks and under 70 words in total.","materialLabel":"The function from lecture (attached to your prompt automatically)","material":"# Lecture 14 - Recursion (CS 101, Prof. Okonkwo)\n\ndef sum_digits(n):\n    \"\"\"Return the sum of the digits of a non-negative integer n.\"\"\"\n    if n == 0:\n        return 0\n    return n % 10 + sum_digits(n // 10)\n\n# Reminders from lecture:\n#  - n % 10 gives the last digit: 4096 % 10 == 6\n#  - n // 10 drops the last digit: 4096 // 10 == 409\n#  - every recursive function needs a base case that stops it\n#  - the call stack holds one frame per call that hasn't returned yet\n#\n# Example from the slides:\n#   sum_digits(52) = 2 + sum_digits(5)\n#                  = 2 + (5 + sum_digits(0))\n#                  = 2 + (5 + 0)\n#                  = 7\n#\n# Midterm topics: base case, recursive case, tracing, stack depth.\n# Office hours moved to Thursday 3-5pm in Gates 104.\n# My note: I keep losing track of which call I'm in when the number has a 0 in it.","rules":[{"type":"numberedItems","count":5},{"type":"codeBlocks","count":0},{"type":"maxProseWords","max":70}],"mistakes":[{"title":"Getting a paragraph","why":"Asked to explain, the AI writes a general essay on recursion instead of the trace you need.","fix":"Say: \"Exactly 5 numbered lines, one per call.\"","checks":["numberedItems","maxProseWords"]},{"title":"Not showing the line shape","why":"Without an example, the AI mixes words and math and skips the 0 digit.","fix":"Show it: \"Format each like: 1. sum_digits(4096) = 6 + sum_digits(409)\".","checks":["r0"]},{"title":"Forgetting the base case","why":"The AI may stop at sum_digits(4) and jump to the answer.","fix":"Add: \"Line 5 is the base case sum_digits(0) = 0.\"","checks":["r1"]},{"title":"No clean ending","why":"The AI adds a recap or a code block with the function again.","fix":"Add: \"Then one last line: Total: N. No code, nothing else.\"","checks":["r2","codeBlocks"]}],"promptHints":[{"label":"Sets the number of lines","patterns":["\\b(5|five)\\b.{0,20}(line|call|step)","one per call","numbered"]},{"label":"Shows the line shape","patterns":["sum_digits\\(4096\\) =","= 6 \\+","like ['\"]"]},{"label":"Asks for the base case","patterns":["base case","sum_digits\\(0\\)"]},{"label":"Asks for a total line","patterns":["total"]},{"label":"Rules out code and extras","patterns":["no code","nothing else","no explanation"]}],"hint":"A trace is easiest to study when every call gets its own line in the same shape. Show the AI one line, say how many, and how it should end.","rubricLabels":["Lines 1-4 are sum_digits(4096) = 6 + sum_digits(409), sum_digits(409) = 9 + sum_digits(40), sum_digits(40) = 0 + sum_digits(4) and sum_digits(4) = 4 + sum_digits(0), in that order and in that form","Line 5 is the base case, sum_digits(0) = 0","After line 5 there is exactly one more line, 'Total: 19', and there is no other explanation before or after the lines"]}