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 1 commit
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
65 changes: 65 additions & 0 deletions bit_manipulation/hamming_code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
""""
Author: Ayush Chakraborty
Date: 6th October 2024

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

Check failure on line 5 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/hamming_code_generator.py:5:56: W291 Trailing whitespace

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

Check failure on line 16 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/hamming_code_generator.py:16:89: E501 Line too long (96 > 88)
a single bit change, but can only detect more than a single bit change

Check failure on line 17 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/hamming_code_generator.py:17:75: W291 Trailing whitespace

Check failure on line 18 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/hamming_code_generator.py:18:1: W293 Blank line contains whitespace
for more theoretical knowledege about Hamming codes, refer: https://www.youtube.com/watch?v=X8jsijhllIA&t=143s
by 3B1B YT channel

>>> hamming_15_11("00110001110")
'0010101110001110'
>>> hamming_15_11("10110000110")
'0101001100000110'
>>> hamming_15_11("10111100110")
'0011001101100110'
>>> hamming_15_11("10001000010")
'0001100001000010'

Check failure on line 30 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/hamming_code_generator.py:30:1: W293 Blank line contains whitespace
"""
if len(number) == 11:
digits = [0 if number[i] == '0' else 1 for i in range(len(number))]
total_num_1 = sum(digits)
hamming_digits = [0 for i in range(16)]
bit_1 = reduce(lambda x,y:x^y, [digits[1], digits[4], digits[8], digits[0], digits[3], digits[6], digits[10]])

Check failure on line 36 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/hamming_code_generator.py:36:89: E501 Line too long (118 > 88)
bit_2 = reduce(lambda x,y:x^y, [digits[2], digits[5], digits[9], digits[0], digits[3], digits[6], digits[10]])

Check failure on line 37 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/hamming_code_generator.py:37:89: E501 Line too long (118 > 88)
bit_3 = reduce(lambda x,y:x^y, [digits[1], digits[2], digits[3], digits[7], digits[8], digits[9], digits[10]])

Check failure on line 38 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/hamming_code_generator.py:38:89: E501 Line too long (118 > 88)
bit_4 = reduce(lambda x,y:x^y, [digits[4], digits[5], digits[6], digits[7], digits[8], digits[9], digits[10]])

Check failure on line 39 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

bit_manipulation/hamming_code_generator.py:39:89: E501 Line too long (118 > 88)
bit_0 = int(total_num_1%2)^bit_1^bit_2^bit_3^bit_4

redundant_bits = [bit_0, bit_1, bit_2, bit_3, bit_4]

j = -1
r = 0
for k in range(16):
if (k == 0 or k==1 or k==2 or k==4 or k==8):

Check failure on line 47 in bit_manipulation/hamming_code_generator.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1714)

bit_manipulation/hamming_code_generator.py:47:17: PLR1714 Consider merging multiple comparisons: `k in (0, 1, 2, 4, 8)`. Use a `set` if the elements are hashable.
hamming_digits[k] = redundant_bits[r]
r += 1
else:
j += 1
hamming_digits[k] = digits[j]

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

else:
return "please provide a 11 bit binary number"

if __name__ == "__main__":
from functools import reduce
import doctest

doctest.testmod()


Loading