October 2025
Intermediate to advanced
485 pages
13h 2m
English
These are the solutions to the Exercises.
Here is one way we can convert a binary string into a decimal number:
| | def decimal(string): |
| | integer = 0 |
| | power = 0 |
| | index = len(string) - 1 |
| | |
| | while index >= 0: |
| | if string[index] == "1": |
| | integer += 2**power |
| | |
| | index -= 1 |
| | power += 1 |
| | |
| | return integer |
This function accepts a string parameter. We expect this to be a binary string such as "000101001010".
We start by initializing an integer at 0. Eventually, this will be the decimal number we return at the end of our function.
We also initialize a power variable, which we’ll use to help us compute what number each digit place of our binary string represents. The right-most digit place will be 2 to the power of 0 (that is, ...
Read now
Unlock full access