Skip to content

feat: Introduce hamming code generator #11827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1870c0c
feat: Introduce hamming code generator
AyushChakraborty Oct 6, 2024
aac46ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
195144d
fix: Add descriptive naming
AyushChakraborty Oct 6, 2024
9312ec6
Merge branch 'branch1' of
AyushChakraborty Oct 6, 2024
03ef987
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
598a01a
fix: Update changes indicated by failed PR
AyushChakraborty Oct 6, 2024
b720b7a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
4de7668
fix: Update to more readable code
AyushChakraborty Oct 8, 2024
e9d35aa
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
46a2eee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
4ff8ddc
fix: Add return statement in elif block
AyushChakraborty Oct 8, 2024
e790a90
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
a51f39a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
e9a65c7
fix: Update elif to else for more general case
AyushChakraborty Oct 8, 2024
2dcd836
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
140d8c9
fix: Update primary if statement
AyushChakraborty Oct 8, 2024
0a87aa2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
49325d1
fix: Modify code to pass tests
AyushChakraborty Oct 8, 2024
4fc77ef
Merge branch 'branch1' of
AyushChakraborty Oct 8, 2024
6d5b137
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
827d9d4
fix: Update to pass test cases
AyushChakraborty Oct 8, 2024
bca27fc
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
172b9bf
fix: Update to pass checks
AyushChakraborty Oct 8, 2024
6817870
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
ac9983e
fix: Update for more readability of code
AyushChakraborty Oct 8, 2024
2f15194
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
dbea02c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
3e4ed8a
fix: Update for readability
AyushChakraborty Oct 8, 2024
54e8005
Merge branch 'branch1' of
AyushChakraborty Oct 8, 2024
b34076c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
f0ae8f3
fix: Update for readability
AyushChakraborty Oct 8, 2024
d3649da
Merge branch 'branch1' of
AyushChakraborty Oct 8, 2024
defaba9
fix: Update variable names
AyushChakraborty Oct 8, 2024
68e8480
fix: Update to remove unecessary variable (r)
AyushChakraborty Oct 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions bit_manipulation/hamming_code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
Author: Ayush Chakraborty
Date: 6th October 2024

generate (15,11) hamming encoded bits gives 11 bit data

input: 11 bit binary number with type string
return: 16 bit binary hamming encoded number returned in string form
"""


def hamming_15_11(number: str) -> str:
"""
Performs parity checks to assign values to the redundant bits,
in the 16 bit number
returned, bits at index 0, 1, 2, 4, 8 are redundant bits used for checking

Hamming generated 16 bits generated from the 11 bit binary number can only detect
and change a single bit change, but can only detect more than a
single bit change

for more theoretical knowledege about Hamming codes, refer:
https://www.youtube.com/watch?v=X8jsijhllIA&t=0s
by 3B1B YT channel

Wikipedia:
https://en.wikipedia.org/wiki/Hamming_code

>>> hamming_15_11("00110001110")
'0010101110001110'
>>> hamming_15_11("10110000110")
'0101001100000110'
>>> hamming_15_11("10111100110")
'0011001101100110'
>>> hamming_15_11("10001000010")
'0001100001000010'
>>> hamming_15_11("00010abcdef")
"Input must be an 11-bit binary string containing only '0's and '1's."

"""
is_bin = True # assuming it's binary initially
for i in number:
if i not in ("0", "1"):
is_bin = False
break

if len(number) == 11 and is_bin:
digits = [int(number[i]) for i in range(len(number))]

total_ones = sum(digits)
hamming_digits = [0] * 16

parity_positions = {
1: [0, 1, 3, 4, 6, 8, 10],
2: [0, 2, 3, 5, 6, 9, 10],
4: [1, 2, 3, 7, 8, 9, 10],
8: [4, 5, 6, 7, 8, 9, 10],
}

redundant_bits = [0] * 5

redundant_bits_index = 1
for positions in parity_positions.values():
parity = 0
for idx in positions:
parity ^= digits[idx]
redundant_bits[redundant_bits_index] = parity
redundant_bits_index += 1

redundant_bits[0] = (
total_ones % 2
^ redundant_bits[1]
^ redundant_bits[2]
^ redundant_bits[3]
^ redundant_bits[4]
)

data_index = 0
redundant_bit_locations = [0, 1, 2, 4, 8]
for k in range(16):
if k in redundant_bit_locations:
hamming_digits[k] = redundant_bits.pop(0)
else:
hamming_digits[k] = digits[data_index]
data_index += 1

return "".join([str(i) for i in hamming_digits])

elif len(number) != 11 or not is_bin:
return "Input must be an 11-bit binary string containing only '0's and '1's."

else:
return "Invalid input"


if __name__ == "__main__":
import doctest

doctest.testmod()