Skip to content

Commit 0000100

Browse files
manmitapre-commit-ci[bot]cclauss
authored andcommitted
Add binary_coded_decimal.py (#10656)
* added decimal to bcd sequence * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated with fixes * Update and rename bcd_sequence.py to binary_coded_decimal.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update binary_coded_decimal.py * Update binary_coded_decimal.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- 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 5bbf488 commit 0000100

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def binary_coded_decimal(number: int) -> str:
2+
"""
3+
Find binary coded decimal (bcd) of integer base 10.
4+
Each digit of the number is represented by a 4-bit binary.
5+
Example:
6+
>>> binary_coded_decimal(-2)
7+
'0b0000'
8+
>>> binary_coded_decimal(-1)
9+
'0b0000'
10+
>>> binary_coded_decimal(0)
11+
'0b0000'
12+
>>> binary_coded_decimal(3)
13+
'0b0011'
14+
>>> binary_coded_decimal(2)
15+
'0b0010'
16+
>>> binary_coded_decimal(12)
17+
'0b00010010'
18+
>>> binary_coded_decimal(987)
19+
'0b100110000111'
20+
"""
21+
return "0b" + "".join(
22+
str(bin(int(digit)))[2:].zfill(4) for digit in str(max(0, number))
23+
)
24+
25+
26+
if __name__ == "__main__":
27+
import doctest
28+
29+
doctest.testmod()

0 commit comments

Comments
 (0)