-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathbinary_parity_pattern.py
More file actions
47 lines (35 loc) · 986 Bytes
/
binary_parity_pattern.py
File metadata and controls
47 lines (35 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
binary_parity_pattern.py
========================
Generates a binary parity pattern based on the cumulative sum of bits
in the binary representation of an integer.
Reference:
https://en.wikipedia.org/wiki/Parity_(mathematics)
"""
def binary_parity_pattern(number: int) -> str:
"""
Return a binary parity pattern string for a given integer.
>>> binary_parity_pattern(13)
'0b1001'
>>> binary_parity_pattern(7)
'0b101'
>>> binary_parity_pattern(4)
'0b111'
>>> binary_parity_pattern(0)
'0b0'
"""
if number < 0:
raise ValueError("Number must be non-negative")
if number == 0:
return "0b0"
binary_str = bin(number)[2:]
cum_sum = 0
pattern = []
for bit in binary_str:
cum_sum += int(bit)
pattern.append(str(cum_sum % 2))
result = "".join(pattern).lstrip("0")
return "0b" + (result if result else "0")
if __name__ == "__main__":
import doctest
doctest.testmod()