-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
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
AyushChakraborty
wants to merge
34
commits into
TheAlgorithms:master
Choose a base branch
from
AyushChakraborty:branch1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
1870c0c
feat: Introduce hamming code generator
AyushChakraborty aac46ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 195144d
fix: Add descriptive naming
AyushChakraborty 9312ec6
Merge branch 'branch1' of
AyushChakraborty 03ef987
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 598a01a
fix: Update changes indicated by failed PR
AyushChakraborty b720b7a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4de7668
fix: Update to more readable code
AyushChakraborty e9d35aa
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty 46a2eee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4ff8ddc
fix: Add return statement in elif block
AyushChakraborty e790a90
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty a51f39a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e9a65c7
fix: Update elif to else for more general case
AyushChakraborty 2dcd836
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty 140d8c9
fix: Update primary if statement
AyushChakraborty 0a87aa2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 49325d1
fix: Modify code to pass tests
AyushChakraborty 4fc77ef
Merge branch 'branch1' of
AyushChakraborty 6d5b137
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 827d9d4
fix: Update to pass test cases
AyushChakraborty bca27fc
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty 172b9bf
fix: Update to pass checks
AyushChakraborty 6817870
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ac9983e
fix: Update for more readability of code
AyushChakraborty 2f15194
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty dbea02c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3e4ed8a
fix: Update for readability
AyushChakraborty 54e8005
Merge branch 'branch1' of
AyushChakraborty b34076c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f0ae8f3
fix: Update for readability
AyushChakraborty d3649da
Merge branch 'branch1' of
AyushChakraborty defaba9
fix: Update variable names
AyushChakraborty 68e8480
fix: Update to remove unecessary variable (r)
AyushChakraborty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
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 | ||
AyushChakraborty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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_num_1 = sum(digits) | ||
AyushChakraborty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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_num_1 % 2 | ||
^ redundant_bits[1] | ||
^ redundant_bits[2] | ||
^ redundant_bits[3] | ||
^ redundant_bits[4] | ||
) | ||
|
||
j = -1 | ||
AyushChakraborty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
r = 0 | ||
AyushChakraborty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
redundant_bit_locations = [0, 1, 2, 4, 8] | ||
for k in range(16): | ||
if k in redundant_bit_locations: | ||
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 "Input must be an 11-bit binary string containing only '0's and '1's." | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.