Skip to content

Commit 42aea72

Browse files
Craniacepre-commit-ci[bot]cclauss
authored
Created bianry_to_excessthree.py (#13831)
* Create bianry_to_excessthree.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix filename typos in binary to excess-3 conversion --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 0dffbb2 commit 42aea72

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

conversions/binary_to_excess3.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def binary_to_excess3(binary_str: str) -> str:
2+
"""
3+
Convert a binary number (as a string) to its Excess-3 code.
4+
https://en.wikipedia.org/wiki/Excess-3
5+
6+
Args:
7+
binary_str (str): Binary number as a string (e.g., "1010").
8+
9+
Returns:
10+
str: Excess-3 code as a binary string.
11+
12+
Example:
13+
>>> binary_to_excess3("1010")
14+
'1101'
15+
"""
16+
# Convert binary to decimal
17+
decimal_value = int(binary_str, 2)
18+
19+
# Add 3 (Excess-3 encoding)
20+
excess3_value = decimal_value + 3
21+
22+
# Convert back to 4-bit binary
23+
excess3_binary = format(excess3_value, "04b")
24+
25+
return excess3_binary
26+
27+
28+
if __name__ == "__main__":
29+
from dostest import testmod
30+
31+
testmod()
32+
binary_input = input("Enter a 4-bit binary number: ")
33+
excess3_output = binary_to_excess3(binary_input)
34+
print(f"Excess-3 code of {binary_input} is: {excess3_output}")

0 commit comments

Comments
 (0)