MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / excess_3_code

Function excess_3_code

bit_manipulation/excess_3_code.py:1–21  ·  view source on GitHub ↗

Find excess-3 code of integer base 10. Add 3 to all digits in a decimal number then convert to a binary-coded decimal. https://en.wikipedia.org/wiki/Excess-3 >>> excess_3_code(0) '0b0011' >>> excess_3_code(3) '0b0110' >>> excess_3_code(2) '0b0101' >>> excess

(number: int)

Source from the content-addressed store, hash-verified

1def excess_3_code(number: int) -> str:
2 """
3 Find excess-3 code of integer base 10.
4 Add 3 to all digits in a decimal number then convert to a binary-coded decimal.
5 https://en.wikipedia.org/wiki/Excess-3
6
7 >>> excess_3_code(0)
8 '0b0011'
9 >>> excess_3_code(3)
10 '0b0110'
11 >>> excess_3_code(2)
12 '0b0101'
13 >>> excess_3_code(20)
14 '0b01010011'
15 >>> excess_3_code(120)
16 '0b010001010011'
17 """
18 num = ""
19 for digit in str(max(0, number)):
20 num += str(bin(int(digit) + 3))[2:].zfill(4)
21 return "0b" + num
22
23
24if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected