-
-
Save lizzybrooks/dfe3cd878ce98e24eb35fdf087ae2ac3 to your computer and use it in GitHub Desktop.
Binary to decimal conversion with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binary_to_decimal_iterative(binary): | |
total = 0 | |
for i in range(len(binary)): | |
if binary[i] == '1': | |
pass | |
return total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binary_to_decimal_recursive(binary): | |
if len(binary) == 1: | |
return int(binary) | |
else: | |
rest = binary_to_decimal_recursive(binary[1:]) | |
if binary[0] == '1': | |
pass | |
else: | |
return rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment