← Back to Recursion
Question 596
Binary By Recursion
Binary By Recursion
Complete to_binary(n: int) -> str so it returns the binary digits of a non-negative integer as text, by recursion. The base case is written: 0 and 1 are their own binary digit, converted to text with str(). Add the recursive case, which returns the binary form of n // 2 (floor division) followed by the last bit, the remainder n % 2, converted with str() so the two pieces join as text. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one integer n, no prompt, which the tests read and pass to your function before printing the string it returns.
Expected Output:
1010= newline. The output ends with a newline — print one after the last line.
Hints2 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.