← Back to Recursion
Question 612
Binary String To Number
Binary String To Number
Write from_binary(bits: str) -> int that returns the value of a string of 0s and 1s, by recursion: the empty string is worth 0; otherwise the value is the value of everything but the last character (the slice bits[:-1]) multiplied by 2, plus the last character bits[-1] converted with int(). Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of binary digits (it may be empty), no prompt, which the tests read and pass to your function before printing the value it returns.
Expected Output:
10= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
RecursionSlicing
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.