Skip to content

Commit 172b9bf

Browse files
fix: Update to pass checks
1 parent bca27fc commit 172b9bf

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

bit_manipulation/hamming_code_generator.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ def hamming_15_11(number: str) -> str:
3838
"Input must be an 11-bit binary string containing only '0's and '1's."
3939
4040
"""
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"):
4444
is_bin = False
4545
break
4646

4747
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+
4951
total_num_1 = sum(digits)
5052
hamming_digits = [0] * 16
5153

@@ -58,28 +60,24 @@ def hamming_15_11(number: str) -> str:
5860

5961
redundant_bits = [0] * 5
6062

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():
6665
parity = 0
67-
for idx in i:
68-
parity ^= digits[idx]
66+
for idx in positions:
67+
parity ^= digits[idx]
6968
redundant_bits[redundant_bits_index] = parity
7069
redundant_bits_index += 1
7170

7271
redundant_bits[0] = (
73-
int(total_num_1 % 2)
72+
total_num_1 % 2
7473
^ redundant_bits[1]
7574
^ redundant_bits[2]
7675
^ redundant_bits[3]
7776
^ redundant_bits[4]
7877
)
79-
# this is the 0th redundancy bit which takes into account the other 15 bits
8078

81-
j = -1
82-
r = 0
79+
j = -1
80+
r = 0
8381
redundant_bit_locations = [0, 1, 2, 4, 8]
8482
for k in range(16):
8583
if k in redundant_bit_locations:
@@ -94,7 +92,6 @@ def hamming_15_11(number: str) -> str:
9492
else:
9593
return "Input must be an 11-bit binary string containing only '0's and '1's."
9694

97-
9895
if __name__ == "__main__":
9996
import doctest
10097

0 commit comments

Comments
 (0)