@@ -38,14 +38,16 @@ def hamming_15_11(number: str) -> str:
38
38
"Input must be an 11-bit binary string containing only '0's and '1's."
39
39
40
40
"""
41
- is_bin = True # assuming its binary initially
42
- for i in number :
43
- if i not in ("0" , "1" ):
41
+ is_bin = True # assuming it's binary initially
42
+ for i in number :
43
+ if i not in ("0" , "1" ):
44
44
is_bin = False
45
45
break
46
46
47
47
if len (number ) == 11 and is_bin :
48
- digits = [int (number [i ]) or i in range (len (number ))]
48
+
49
+ digits = [int (number [i ]) for i in range (len (number ))]
50
+
49
51
total_num_1 = sum (digits )
50
52
hamming_digits = [0 ] * 16
51
53
@@ -58,28 +60,24 @@ def hamming_15_11(number: str) -> str:
58
60
59
61
redundant_bits = [0 ] * 5
60
62
61
- redundant_bits_index = (
62
- 1 # starting from 1 as we need to find the 0th redundancy bit once
63
- )
64
- # all the other redundancy bits have been found
65
- for i in parity_positions .values ():
63
+ redundant_bits_index = 1
64
+ for positions in parity_positions .values ():
66
65
parity = 0
67
- for idx in i :
68
- parity ^= digits [idx ]
66
+ for idx in positions :
67
+ parity ^= digits [idx ]
69
68
redundant_bits [redundant_bits_index ] = parity
70
69
redundant_bits_index += 1
71
70
72
71
redundant_bits [0 ] = (
73
- int ( total_num_1 % 2 )
72
+ total_num_1 % 2
74
73
^ redundant_bits [1 ]
75
74
^ redundant_bits [2 ]
76
75
^ redundant_bits [3 ]
77
76
^ redundant_bits [4 ]
78
77
)
79
- # this is the 0th redundancy bit which takes into account the other 15 bits
80
78
81
- j = - 1
82
- r = 0
79
+ j = - 1
80
+ r = 0
83
81
redundant_bit_locations = [0 , 1 , 2 , 4 , 8 ]
84
82
for k in range (16 ):
85
83
if k in redundant_bit_locations :
@@ -94,7 +92,6 @@ def hamming_15_11(number: str) -> str:
94
92
else :
95
93
return "Input must be an 11-bit binary string containing only '0's and '1's."
96
94
97
-
98
95
if __name__ == "__main__" :
99
96
import doctest
100
97
0 commit comments