← Back to Recursion
Question 615
Digits Reversed With An Accumulator
Digits Reversed With An Accumulator
Write reverse_digits(n: int, acc: int) -> int that returns the digits of a non-negative integer n in reverse order as a number, using the second parameter as an accumulator that carries the answer built so far: when n is 0 return acc; otherwise recurse with n with its last digit removed (n // 10) and acc moved up one place (times 10) plus that last digit (n % 10). Trailing zeros disappear, so 1200 reverses to 21. Do NOT write a main block or any top-level test code; the tests construct the calls with an accumulator of 0. Input: one integer n, no prompt, which the tests read and pass to your function before printing the value it returns.
Expected Output:
4321= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
RecursionOperators
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.