Skip to content

Commit 348a036

Browse files
manmitapre-commit-ci[bot]cclauss
authored andcommitted
Excess 3 code (TheAlgorithms#11001)
* added excess-3 code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated with fixes * updated with fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update excess_3_code.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 46519df commit 348a036

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: bit_manipulation/excess_3_code.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def 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+
24+
if __name__ == "__main__":
25+
import doctest
26+
27+
doctest.testmod()

0 commit comments

Comments
 (0)