Dynamic Programming21 sections · 916 units
Open in Course

Sum of Digits - Walkthrough

Tracing the calls

Let me trace sum_of_digits(12341234) step by step:

Call 11: sum_of_digits(12341234) \rightarrow returns 4+4 + sum_of_digits(123123)

Call 22: sum_of_digits(123123) \rightarrow returns 3+3 + sum_of_digits(1212)

Call 33: sum_of_digits(1212) \rightarrow returns 2+2 + sum_of_digits(11)

Call 44: sum_of_digits(11) hits base case, returns 11 Then the results come back: 2+1=32 + 1 = 3, then 3+3=63 + 3 = 6, then 4+6=104 + 6 = 10. Four calls total, each peeling off one digit.

unnamed (10) (1).jpg