|
1 | 1 | """Convert a Decimal Number to a Binary Number.""" |
2 | 2 |
|
3 | 3 |
|
4 | | -def decimal_to_binary(num: int) -> str: |
| 4 | +def decimal_to_binary_iterative(num: int) -> str: |
5 | 5 | """ |
6 | 6 | Convert an Integer Decimal Number to a Binary Number as str. |
7 | | - >>> decimal_to_binary(0) |
| 7 | + >>> decimal_to_binary_iterative(0) |
8 | 8 | '0b0' |
9 | | - >>> decimal_to_binary(2) |
| 9 | + >>> decimal_to_binary_iterative(2) |
10 | 10 | '0b10' |
11 | | - >>> decimal_to_binary(7) |
| 11 | + >>> decimal_to_binary_iterative(7) |
12 | 12 | '0b111' |
13 | | - >>> decimal_to_binary(35) |
| 13 | + >>> decimal_to_binary_iterative(35) |
14 | 14 | '0b100011' |
15 | 15 | >>> # negatives work too |
16 | | - >>> decimal_to_binary(-2) |
| 16 | + >>> decimal_to_binary_iterative(-2) |
17 | 17 | '-0b10' |
18 | 18 | >>> # other floats will error |
19 | | - >>> decimal_to_binary(16.16) # doctest: +ELLIPSIS |
| 19 | + >>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS |
20 | 20 | Traceback (most recent call last): |
21 | 21 | ... |
22 | 22 | TypeError: 'float' object cannot be interpreted as an integer |
23 | 23 | >>> # strings will error as well |
24 | | - >>> decimal_to_binary('0xfffff') # doctest: +ELLIPSIS |
| 24 | + >>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS |
25 | 25 | Traceback (most recent call last): |
26 | 26 | ... |
27 | 27 | TypeError: 'str' object cannot be interpreted as an integer |
@@ -52,7 +52,58 @@ def decimal_to_binary(num: int) -> str: |
52 | 52 | return "0b" + "".join(str(e) for e in binary) |
53 | 53 |
|
54 | 54 |
|
| 55 | +def decimal_to_binary_recursive_helper(decimal: int) -> str: |
| 56 | + """ |
| 57 | + Take a positive integer value and return its binary equivalent. |
| 58 | + >>> decimal_to_binary_recursive_helper(1000) |
| 59 | + '1111101000' |
| 60 | + >>> decimal_to_binary_recursive_helper("72") |
| 61 | + '1001000' |
| 62 | + >>> decimal_to_binary_recursive_helper("number") |
| 63 | + Traceback (most recent call last): |
| 64 | + ... |
| 65 | + ValueError: invalid literal for int() with base 10: 'number' |
| 66 | + """ |
| 67 | + decimal = int(decimal) |
| 68 | + if decimal in (0, 1): # Exit cases for the recursion |
| 69 | + return str(decimal) |
| 70 | + div, mod = divmod(decimal, 2) |
| 71 | + return decimal_to_binary_recursive_helper(div) + str(mod) |
| 72 | + |
| 73 | + |
| 74 | +def decimal_to_binary_recursive(number: str) -> str: |
| 75 | + """ |
| 76 | + Take an integer value and raise ValueError for wrong inputs, |
| 77 | + call the function above and return the output with prefix "0b" & "-0b" |
| 78 | + for positive and negative integers respectively. |
| 79 | + >>> decimal_to_binary_recursive(0) |
| 80 | + '0b0' |
| 81 | + >>> decimal_to_binary_recursive(40) |
| 82 | + '0b101000' |
| 83 | + >>> decimal_to_binary_recursive(-40) |
| 84 | + '-0b101000' |
| 85 | + >>> decimal_to_binary_recursive(40.8) |
| 86 | + Traceback (most recent call last): |
| 87 | + ... |
| 88 | + ValueError: Input value is not an integer |
| 89 | + >>> decimal_to_binary_recursive("forty") |
| 90 | + Traceback (most recent call last): |
| 91 | + ... |
| 92 | + ValueError: Input value is not an integer |
| 93 | + """ |
| 94 | + number = str(number).strip() |
| 95 | + if not number: |
| 96 | + raise ValueError("No input value was provided") |
| 97 | + negative = "-" if number.startswith("-") else "" |
| 98 | + number = number.lstrip("-") |
| 99 | + if not number.isnumeric(): |
| 100 | + raise ValueError("Input value is not an integer") |
| 101 | + return f"{negative}0b{decimal_to_binary_recursive_helper(int(number))}" |
| 102 | + |
| 103 | + |
55 | 104 | if __name__ == "__main__": |
56 | 105 | import doctest |
57 | 106 |
|
58 | 107 | doctest.testmod() |
| 108 | + |
| 109 | + print(decimal_to_binary_recursive(input("Input a decimal number: "))) |
0 commit comments