From 43b1b72e686dfb99a823329995965f47d3ad68e0 Mon Sep 17 00:00:00 2001 From: zhk319 <3680097025@qq.com> Date: Wed, 27 May 2026 08:51:18 +0800 Subject: [PATCH 1/6] Add Hamming Code error-correction algorithm Implemented modular Hamming Code system with encoding, decoding, and single-bit error auto-correction logic. --- strings/hamming_code.py | 90 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 strings/hamming_code.py diff --git a/strings/hamming_code.py b/strings/hamming_code.py new file mode 100644 index 000000000000..a85dff7d898c --- /dev/null +++ b/strings/hamming_code.py @@ -0,0 +1,90 @@ +def calculate_parity_bits(data_bits: list[int]) -> int: + """ + Calculates the number of redundant parity bits needed for Hamming Code. + >>> calculate_parity_bits([1, 0, 1, 1]) + 3 + """ + m = len(data_bits) + r = 0 + while (2**r) < (m + r + 1): + r += 1 + return r + + +def hamming_encode(data_str: str) -> str: + """ + Encodes a binary string into a Hamming Code (7,4 or adaptive format). + Features error detection parity implementation. + >>> hamming_encode("1011") + '1010101' + """ + data = [int(x) for x in data_str] + r = calculate_parity_bits(data) + m = len(data) + + # Initialize code word with placeholders (0) + code = [0] * (m + r) + + # Place data bits into non-parity positions + j = 0 + for i in range(1, len(code) + 1): + if (i & (i - 1)) != 0: # Not a power of 2 + code[i - 1] = data[j] + j += 1 + + # Calculate parity bits using XOR logic + for i in range(r): + parity_pos = 2**i + parity_val = 0 + for j in range(1, len(code) + 1): + if j & parity_pos and j != parity_pos: + parity_val ^= code[j - 1] + code[parity_pos - 1] = parity_val + + return "".join(map(str, code)) + + +def hamming_decode_and_correct(code_str: str) -> tuple[str, int]: + """ + Decodes the Hamming code, detects, and automatically corrects a single-bit error. + Returns a tuple of (corrected_data_bits_string, error_position). + >>> hamming_decode_and_correct("1010101") + ('1011', 0) + >>> hamming_decode_and_correct("1010111") # Error injected at position 6 + ('1011', 6) + """ + code = [int(x) for x in code_str] + n = len(code) + + # Determine number of parity bits r + r = 0 + while (2**r) <= n: + r += 1 + + error_pos = 0 + # Check each parity bit syndrome + for i in range(r): + parity_pos = 2**i + parity_sum = 0 + for j in range(1, n + 1): + if j & parity_pos: + parity_sum ^= code[j - 1] + if parity_sum != 0: + error_pos += parity_pos + + # Correct the error if detected + if error_pos > 0: + code[error_pos - 1] ^= 1 # Flip the corrupted bit + + # Extract original data bits + original_data = [] + for i in range(1, n + 1): + if (i & (i - 1)) != 0: + original_data.append(code[i - 1]) + + return "".join(map(str, original_data)), error_pos + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From 8982cee7ccb1b41b6f17cdc805c565c7a14cd432 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 00:55:02 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/hamming_code.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/strings/hamming_code.py b/strings/hamming_code.py index a85dff7d898c..e78e72864aaa 100644 --- a/strings/hamming_code.py +++ b/strings/hamming_code.py @@ -21,17 +21,17 @@ def hamming_encode(data_str: str) -> str: data = [int(x) for x in data_str] r = calculate_parity_bits(data) m = len(data) - + # Initialize code word with placeholders (0) code = [0] * (m + r) - + # Place data bits into non-parity positions j = 0 for i in range(1, len(code) + 1): if (i & (i - 1)) != 0: # Not a power of 2 code[i - 1] = data[j] j += 1 - + # Calculate parity bits using XOR logic for i in range(r): parity_pos = 2**i @@ -40,7 +40,7 @@ def hamming_encode(data_str: str) -> str: if j & parity_pos and j != parity_pos: parity_val ^= code[j - 1] code[parity_pos - 1] = parity_val - + return "".join(map(str, code)) @@ -55,12 +55,12 @@ def hamming_decode_and_correct(code_str: str) -> tuple[str, int]: """ code = [int(x) for x in code_str] n = len(code) - + # Determine number of parity bits r r = 0 while (2**r) <= n: r += 1 - + error_pos = 0 # Check each parity bit syndrome for i in range(r): @@ -71,20 +71,21 @@ def hamming_decode_and_correct(code_str: str) -> tuple[str, int]: parity_sum ^= code[j - 1] if parity_sum != 0: error_pos += parity_pos - + # Correct the error if detected if error_pos > 0: code[error_pos - 1] ^= 1 # Flip the corrupted bit - + # Extract original data bits original_data = [] for i in range(1, n + 1): if (i & (i - 1)) != 0: original_data.append(code[i - 1]) - + return "".join(map(str, original_data)), error_pos if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From b515d4a9f0c43fcd40db298a32dd9ac3d27ad993 Mon Sep 17 00:00:00 2001 From: zhk319 <3680097025@qq.com> Date: Wed, 27 May 2026 09:06:35 +0800 Subject: [PATCH 3/6] Fix type hints compatibility --- strings/hamming_code.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/strings/hamming_code.py b/strings/hamming_code.py index a85dff7d898c..f4f23000cd85 100644 --- a/strings/hamming_code.py +++ b/strings/hamming_code.py @@ -1,4 +1,6 @@ -def calculate_parity_bits(data_bits: list[int]) -> int: +from typing import List, Tuple + +def calculate_parity_bits(data_bits: List[int]) -> int: """ Calculates the number of redundant parity bits needed for Hamming Code. >>> calculate_parity_bits([1, 0, 1, 1]) @@ -44,7 +46,7 @@ def hamming_encode(data_str: str) -> str: return "".join(map(str, code)) -def hamming_decode_and_correct(code_str: str) -> tuple[str, int]: +def hamming_decode_and_correct(code_str: str) -> Tuple[str, int]: """ Decodes the Hamming code, detects, and automatically corrects a single-bit error. Returns a tuple of (corrected_data_bits_string, error_position). From de57d59d9ab5bb426344edfd81c68b01241143c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 01:10:37 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/hamming_code.py | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/hamming_code.py b/strings/hamming_code.py index 5e083fd3ad96..c0a1f15b3918 100644 --- a/strings/hamming_code.py +++ b/strings/hamming_code.py @@ -1,5 +1,6 @@ from typing import List, Tuple + def calculate_parity_bits(data_bits: List[int]) -> int: """ Calculates the number of redundant parity bits needed for Hamming Code. From 4a39de2cc887279703a73bef2a619b457adc7bc5 Mon Sep 17 00:00:00 2001 From: zhk319 <3680097025@qq.com> Date: Wed, 27 May 2026 09:37:05 +0800 Subject: [PATCH 5/6] Update hamming_code.py --- strings/hamming_code.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/strings/hamming_code.py b/strings/hamming_code.py index 5e083fd3ad96..0b424fc61815 100644 --- a/strings/hamming_code.py +++ b/strings/hamming_code.py @@ -1,6 +1,4 @@ -from typing import List, Tuple - -def calculate_parity_bits(data_bits: List[int]) -> int: +def calculate_parity_bits(data_bits: list[int]) -> int: """ Calculates the number of redundant parity bits needed for Hamming Code. >>> calculate_parity_bits([1, 0, 1, 1]) @@ -46,7 +44,7 @@ def hamming_encode(data_str: str) -> str: return "".join(map(str, code)) -def hamming_decode_and_correct(code_str: str) -> Tuple[str, int]: +def hamming_decode_and_correct(code_str: str) -> tuple[str, int]: """ Decodes the Hamming code, detects, and automatically corrects a single-bit error. Returns a tuple of (corrected_data_bits_string, error_position). @@ -90,4 +88,4 @@ def hamming_decode_and_correct(code_str: str) -> Tuple[str, int]: if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file From acb046288b5438d19dad802383274263ff690929 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 01:44:48 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/hamming_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/hamming_code.py b/strings/hamming_code.py index 0b424fc61815..e78e72864aaa 100644 --- a/strings/hamming_code.py +++ b/strings/hamming_code.py @@ -88,4 +88,4 @@ def hamming_decode_and_correct(code_str: str) -> tuple[str, int]: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod()