mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 13:33:11 +08:00
* 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>
35 lines
886 B
Python
35 lines
886 B
Python
def binary_to_excess3(binary_str: str) -> str:
|
|
"""
|
|
Convert a binary number (as a string) to its Excess-3 code.
|
|
https://en.wikipedia.org/wiki/Excess-3
|
|
|
|
Args:
|
|
binary_str (str): Binary number as a string (e.g., "1010").
|
|
|
|
Returns:
|
|
str: Excess-3 code as a binary string.
|
|
|
|
Example:
|
|
>>> binary_to_excess3("1010")
|
|
'1101'
|
|
"""
|
|
# Convert binary to decimal
|
|
decimal_value = int(binary_str, 2)
|
|
|
|
# Add 3 (Excess-3 encoding)
|
|
excess3_value = decimal_value + 3
|
|
|
|
# Convert back to 4-bit binary
|
|
excess3_binary = format(excess3_value, "04b")
|
|
|
|
return excess3_binary
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from dostest import testmod
|
|
|
|
testmod()
|
|
binary_input = input("Enter a 4-bit binary number: ")
|
|
excess3_output = binary_to_excess3(binary_input)
|
|
print(f"Excess-3 code of {binary_input} is: {excess3_output}")
|