From 1870c0c420672b2cd0df5d276c62fb7e59ff88ed Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Sun, 6 Oct 2024 17:35:44 +0530 Subject: [PATCH 01/25] feat: Introduce hamming code generator Introduced a new .py file which generates 16 bit hamming codes from the given 11 bit input. Additional 5 bits are redundant, used to correct single bit errors within the data bits --- bit_manipulation/hamming_code_generator.py | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 bit_manipulation/hamming_code_generator.py diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py new file mode 100644 index 000000000000..b209f4ee5989 --- /dev/null +++ b/bit_manipulation/hamming_code_generator.py @@ -0,0 +1,65 @@ +"""" +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=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' + + """ + 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]]) + bit_2 = reduce(lambda x,y:x^y, [digits[2], digits[5], digits[9], digits[0], digits[3], digits[6], digits[10]]) + bit_3 = reduce(lambda x,y:x^y, [digits[1], digits[2], digits[3], digits[7], digits[8], digits[9], digits[10]]) + bit_4 = reduce(lambda x,y:x^y, [digits[4], digits[5], digits[6], digits[7], digits[8], digits[9], digits[10]]) + 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): + 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() + + \ No newline at end of file From aac46aecb7cadbe09a5a8d178d5a2e338b4d670a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 12:37:46 +0000 Subject: [PATCH 02/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 78 +++++++++++++++++----- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index b209f4ee5989..9471f0b1c264 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -1,21 +1,22 @@ -"""" +""" " Author: Ayush Chakraborty Date: 6th October 2024 -generate (15,11) hamming encoded bits gives 11 bit data +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 - + 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=143s by 3B1B YT channel @@ -27,39 +28,82 @@ def hamming_15_11(number: str) -> str: '0011001101100110' >>> hamming_15_11("10001000010") '0001100001000010' - + """ if len(number) == 11: - digits = [0 if number[i] == '0' else 1 for i in range(len(number))] + 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]]) - bit_2 = reduce(lambda x,y:x^y, [digits[2], digits[5], digits[9], digits[0], digits[3], digits[6], digits[10]]) - bit_3 = reduce(lambda x,y:x^y, [digits[1], digits[2], digits[3], digits[7], digits[8], digits[9], digits[10]]) - bit_4 = reduce(lambda x,y:x^y, [digits[4], digits[5], digits[6], digits[7], digits[8], digits[9], digits[10]]) - bit_0 = int(total_num_1%2)^bit_1^bit_2^bit_3^bit_4 + bit_1 = reduce( + lambda x, y: x ^ y, + [ + digits[1], + digits[4], + digits[8], + digits[0], + digits[3], + digits[6], + digits[10], + ], + ) + bit_2 = reduce( + lambda x, y: x ^ y, + [ + digits[2], + digits[5], + digits[9], + digits[0], + digits[3], + digits[6], + digits[10], + ], + ) + bit_3 = reduce( + lambda x, y: x ^ y, + [ + digits[1], + digits[2], + digits[3], + digits[7], + digits[8], + digits[9], + digits[10], + ], + ) + bit_4 = reduce( + lambda x, y: x ^ y, + [ + digits[4], + digits[5], + digits[6], + digits[7], + digits[8], + digits[9], + digits[10], + ], + ) + 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): + if k == 0 or k == 1 or k == 2 or k == 4 or k == 8: hamming_digits[k] = redundant_bits[r] r += 1 else: j += 1 - hamming_digits[k] = digits[j] + hamming_digits[k] = digits[j] + + return "".join([str(i) for i in hamming_digits]) - 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() - - \ No newline at end of file From 195144d2bd114c38a45ab9febf0cb923b78e1b16 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Sun, 6 Oct 2024 18:14:26 +0530 Subject: [PATCH 03/25] fix: Add descriptive naming changed name of x, y to temp_bit1, temp_bit2 respectively --- bit_manipulation/hamming_code_generator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index b209f4ee5989..873645816ad0 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -33,10 +33,10 @@ def hamming_15_11(number: str) -> str: 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]]) - bit_2 = reduce(lambda x,y:x^y, [digits[2], digits[5], digits[9], digits[0], digits[3], digits[6], digits[10]]) - bit_3 = reduce(lambda x,y:x^y, [digits[1], digits[2], digits[3], digits[7], digits[8], digits[9], digits[10]]) - bit_4 = reduce(lambda x,y:x^y, [digits[4], digits[5], digits[6], digits[7], digits[8], digits[9], digits[10]]) + bit_1 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[1], digits[4], digits[8], digits[0], digits[3], digits[6], digits[10]]) + bit_2 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[2], digits[5], digits[9], digits[0], digits[3], digits[6], digits[10]]) + bit_3 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[1], digits[2], digits[3], digits[7], digits[8], digits[9], digits[10]]) + bit_4 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[4], digits[5], digits[6], digits[7], digits[8], digits[9], digits[10]]) 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] From 03ef9879e8025dc53a7fda4eb3307dd71efa7cb2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 12:50:05 +0000 Subject: [PATCH 04/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 54 ++++++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 9279e7a9ff5c..e5670a8a9b2e 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -34,11 +34,55 @@ def hamming_15_11(number: str) -> str: 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 temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[1], digits[4], digits[8], digits[0], digits[3], digits[6], digits[10]]) - bit_2 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[2], digits[5], digits[9], digits[0], digits[3], digits[6], digits[10]]) - bit_3 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[1], digits[2], digits[3], digits[7], digits[8], digits[9], digits[10]]) - bit_4 = reduce(lambda temp_bit1,temp_bit2:temp_bit1^temp_bit2, [digits[4], digits[5], digits[6], digits[7], digits[8], digits[9], digits[10]]) - bit_0 = int(total_num_1%2)^bit_1^bit_2^bit_3^bit_4 + bit_1 = reduce( + lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, + [ + digits[1], + digits[4], + digits[8], + digits[0], + digits[3], + digits[6], + digits[10], + ], + ) + bit_2 = reduce( + lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, + [ + digits[2], + digits[5], + digits[9], + digits[0], + digits[3], + digits[6], + digits[10], + ], + ) + bit_3 = reduce( + lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, + [ + digits[1], + digits[2], + digits[3], + digits[7], + digits[8], + digits[9], + digits[10], + ], + ) + bit_4 = reduce( + lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, + [ + digits[4], + digits[5], + digits[6], + digits[7], + digits[8], + digits[9], + digits[10], + ], + ) + 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] From 598a01a5606ca7003220ff4c336c7c0282fdfabe Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Sun, 6 Oct 2024 18:43:34 +0530 Subject: [PATCH 05/25] fix: Update changes indicated by failed PR Changed the code according the feedback given by failed PR (#11827) --- bit_manipulation/hamming_code_generator.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index e5670a8a9b2e..86dcce9b9136 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -8,16 +8,20 @@ return: 16 bit binary hamming encoded number returned in string form """ +from functools import reduce def hamming_15_11(number: str) -> str: """ - Performs parity checks to assign values to the redundant bits, in the 16 bit number + 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 + 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=143s + 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") @@ -88,8 +92,9 @@ def hamming_15_11(number: str) -> str: j = -1 r = 0 + redundant_bit_locations = [0, 1, 2, 4, 8] for k in range(16): - if k == 0 or k == 1 or k == 2 or k == 4 or k == 8: + if k in redundant_bit_locations: hamming_digits[k] = redundant_bits[r] r += 1 else: @@ -103,7 +108,6 @@ def hamming_15_11(number: str) -> str: if __name__ == "__main__": - from functools import reduce import doctest doctest.testmod() From b720b7a0c0463939b90d530c6c47c30dd5e432e2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:18:40 +0000 Subject: [PATCH 06/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 86dcce9b9136..90eca42d43b6 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -10,17 +10,18 @@ from functools import reduce + def hamming_15_11(number: str) -> str: """ - Performs parity checks to assign values to the redundant bits, + 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 + 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: + for more theoretical knowledege about Hamming codes, refer: https://www.youtube.com/watch?v=X8jsijhllIA&t=143s by 3B1B YT channel From 4de766876cd6de2001ddf8dfde4aac31abf10e57 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 09:11:51 +0530 Subject: [PATCH 07/25] fix: Update to more readable code changed code based on suggestion by @jeffreyyancey, fixed up some docstring, links and wrote code for finding redundant bits using for loops instead of reduce and lambda, for readability --- bit_manipulation/hamming_code_generator.py | 90 ++++++++-------------- 1 file changed, 31 insertions(+), 59 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 86dcce9b9136..5e6d683c6580 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -1,4 +1,4 @@ -""" " +""" Author: Ayush Chakraborty Date: 6th October 2024 @@ -8,8 +8,6 @@ return: 16 bit binary hamming encoded number returned in string form """ -from functools import reduce - def hamming_15_11(number: str) -> str: """ Performs parity checks to assign values to the redundant bits, @@ -21,9 +19,12 @@ def hamming_15_11(number: str) -> str: single bit change for more theoretical knowledege about Hamming codes, refer: - https://www.youtube.com/watch?v=X8jsijhllIA&t=143s + 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") @@ -37,58 +38,28 @@ def hamming_15_11(number: str) -> str: 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 temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, - [ - digits[1], - digits[4], - digits[8], - digits[0], - digits[3], - digits[6], - digits[10], - ], - ) - bit_2 = reduce( - lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, - [ - digits[2], - digits[5], - digits[9], - digits[0], - digits[3], - digits[6], - digits[10], - ], - ) - bit_3 = reduce( - lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, - [ - digits[1], - digits[2], - digits[3], - digits[7], - digits[8], - digits[9], - digits[10], - ], - ) - bit_4 = reduce( - lambda temp_bit1, temp_bit2: temp_bit1 ^ temp_bit2, - [ - digits[4], - digits[5], - digits[6], - digits[7], - digits[8], - digits[9], - digits[10], - ], - ) - 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] + 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 #starting from 1 as we need to find the 0th redundancy bit once + #all the other redundancy bits have been found + for i in parity_positions.values(): + parity = 0 + for idx in i: + parity ^= digits[idx] + redundant_bits[redundant_bits_index] = parity + redundant_bits_index += 1 + + redundant_bits[0] = int(total_num_1 % 2) ^ redundant_bits[1] ^ redundant_bits[2] ^ redundant_bits[3] ^ redundant_bits[4] + #this is the 0th redundancy bit which takes into account the other 15 bits j = -1 r = 0 @@ -101,10 +72,11 @@ def hamming_15_11(number: str) -> str: j += 1 hamming_digits[k] = digits[j] - return "".join([str(i) for i in hamming_digits]) + return ''.join([str(i) for i in hamming_digits]) + - else: - return "please provide a 11 bit binary number" + if len(number) != 11 or not all(bit in '01' for bit in number): + raise ValueError("Input must be an 11-bit binary string containing only '0's and '1's.") if __name__ == "__main__": From 46a2eee2f35ff8e245064ff4f680697d5ce60806 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 03:45:58 +0000 Subject: [PATCH 08/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index c32dc9078a7f..8a918017396d 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -1,4 +1,4 @@ -""" +""" Author: Ayush Chakraborty Date: 6th October 2024 @@ -8,6 +8,7 @@ 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, @@ -18,7 +19,7 @@ def hamming_15_11(number: str) -> str: and change a single bit change, but can only detect more than a single bit change - for more theoretical knowledege about Hamming codes, refer: + for more theoretical knowledege about Hamming codes, refer: https://www.youtube.com/watch?v=X8jsijhllIA&t=0s by 3B1B YT channel @@ -38,7 +39,7 @@ def hamming_15_11(number: str) -> str: 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]*16 + hamming_digits = [0] * 16 parity_positions = { 1: [0, 1, 3, 4, 6, 8, 10], @@ -47,19 +48,27 @@ def hamming_15_11(number: str) -> str: 8: [4, 5, 6, 7, 8, 9, 10], } - redundant_bits = [0]*5 + redundant_bits = [0] * 5 - redundant_bits_index = 1 #starting from 1 as we need to find the 0th redundancy bit once - #all the other redundancy bits have been found + redundant_bits_index = ( + 1 # starting from 1 as we need to find the 0th redundancy bit once + ) + # all the other redundancy bits have been found for i in parity_positions.values(): parity = 0 for idx in i: parity ^= digits[idx] redundant_bits[redundant_bits_index] = parity redundant_bits_index += 1 - - redundant_bits[0] = int(total_num_1 % 2) ^ redundant_bits[1] ^ redundant_bits[2] ^ redundant_bits[3] ^ redundant_bits[4] - #this is the 0th redundancy bit which takes into account the other 15 bits + + redundant_bits[0] = ( + int(total_num_1 % 2) + ^ redundant_bits[1] + ^ redundant_bits[2] + ^ redundant_bits[3] + ^ redundant_bits[4] + ) + # this is the 0th redundancy bit which takes into account the other 15 bits j = -1 r = 0 @@ -72,11 +81,12 @@ def hamming_15_11(number: str) -> str: j += 1 hamming_digits[k] = digits[j] - return ''.join([str(i) for i in hamming_digits]) - + return "".join([str(i) for i in hamming_digits]) - if len(number) != 11 or not all(bit in '01' for bit in number): - raise ValueError("Input must be an 11-bit binary string containing only '0's and '1's.") + if len(number) != 11 or not all(bit in "01" for bit in number): + raise ValueError( + "Input must be an 11-bit binary string containing only '0's and '1's." + ) if __name__ == "__main__": From 4ff8ddca245fd0d76cdc04434ed7bf89dc04a8e2 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 09:28:20 +0530 Subject: [PATCH 09/25] fix: Add return statement in elif block --- bit_manipulation/hamming_code_generator.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index c32dc9078a7f..aee23e2f9bc3 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -75,9 +75,8 @@ def hamming_15_11(number: str) -> str: return ''.join([str(i) for i in hamming_digits]) - if len(number) != 11 or not all(bit in '01' for bit in number): - raise ValueError("Input must be an 11-bit binary string containing only '0's and '1's.") - + elif len(number) != 11 or not all(bit in '01' for bit in number): + return "Input must be an 11-bit binary string containing only '0's and '1's." if __name__ == "__main__": import doctest From a51f39aad9a1d04dd1a287fe22c6b3d2e9f3a685 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 04:00:21 +0000 Subject: [PATCH 10/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index d23a100e8507..f481b40836dd 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -83,9 +83,10 @@ def hamming_15_11(number: str) -> str: return "".join([str(i) for i in hamming_digits]) - elif len(number) != 11 or not all(bit in '01' for bit in number): + elif len(number) != 11 or not all(bit in "01" for bit in number): return "Input must be an 11-bit binary string containing only '0's and '1's." + if __name__ == "__main__": import doctest From e9a65c70d0a8cc4509cfdded13c1645ea39f41b2 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 09:41:55 +0530 Subject: [PATCH 11/25] fix: Update elif to else for more general case --- bit_manipulation/hamming_code_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index d23a100e8507..b44ab8f1fde0 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -83,7 +83,7 @@ def hamming_15_11(number: str) -> str: return "".join([str(i) for i in hamming_digits]) - elif len(number) != 11 or not all(bit in '01' for bit in number): + else: return "Input must be an 11-bit binary string containing only '0's and '1's." if __name__ == "__main__": From 140d8c929699dad9d7b451a7750f11c430ddc0c1 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 21:13:13 +0530 Subject: [PATCH 12/25] fix: Update primary if statement primary if statement now has check for if the number which is provided as input only has 0 and 1 or not, if it does, then the primary part of the code will run --- bit_manipulation/hamming_code_generator.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 7f2982e53927..da67e67ae09f 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -34,9 +34,17 @@ def hamming_15_11(number: str) -> str: '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." """ - if len(number) == 11: + is_bin = True #assuming its binary initially, will set it to false if found not later on + for i in number: + if (i != "0" and i != "1"): + is_bin = False + break + + if len(number) == 11 and is_bin: digits = [0 if number[i] == "0" else 1 for i in range(len(number))] total_num_1 = sum(digits) hamming_digits = [0] * 16 From 0a87aa20db56f2d8986988b7f623a74593617689 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:45:30 +0000 Subject: [PATCH 13/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index da67e67ae09f..40e0f0e0af1f 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -38,9 +38,9 @@ def hamming_15_11(number: str) -> str: "Input must be an 11-bit binary string containing only '0's and '1's." """ - is_bin = True #assuming its binary initially, will set it to false if found not later on + is_bin = True # assuming its binary initially, will set it to false if found not later on for i in number: - if (i != "0" and i != "1"): + if i != "0" and i != "1": is_bin = False break From 49325d13ba4689011aae965e6dd9e2f3edd6b499 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 21:21:34 +0530 Subject: [PATCH 14/25] fix: Modify code to pass tests --- bit_manipulation/hamming_code_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index da67e67ae09f..d48f7c0b1744 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -38,9 +38,9 @@ def hamming_15_11(number: str) -> str: "Input must be an 11-bit binary string containing only '0's and '1's." """ - is_bin = True #assuming its binary initially, will set it to false if found not later on + is_bin = True #assuming its binary initially for i in number: - if (i != "0" and i != "1"): + if (i not in ("0", "1")): is_bin = False break From 6d5b13767a5ec0fc7e71a5f479cb8d55bed22595 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:00:26 +0000 Subject: [PATCH 15/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index d48f7c0b1744..fc85a3e503e4 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -38,9 +38,9 @@ def hamming_15_11(number: str) -> str: "Input must be an 11-bit binary string containing only '0's and '1's." """ - is_bin = True #assuming its binary initially + is_bin = True # assuming its binary initially for i in number: - if (i not in ("0", "1")): + if i not in ("0", "1"): is_bin = False break From 827d9d4f418131b2beeff2d9d1aec7c0fe599689 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 21:38:40 +0530 Subject: [PATCH 16/25] fix: Update to pass test cases --- bit_manipulation/hamming_code_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index d48f7c0b1744..02f458eba8a3 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -45,7 +45,7 @@ def hamming_15_11(number: str) -> str: break if len(number) == 11 and is_bin: - digits = [0 if number[i] == "0" else 1 for i in range(len(number))] + digits = [int(number[i]) or i in range(len(number))] total_num_1 = sum(digits) hamming_digits = [0] * 16 From 172b9bfbb00fdb89aedb9109ceb947d708df7999 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 21:50:12 +0530 Subject: [PATCH 17/25] fix: Update to pass checks --- bit_manipulation/hamming_code_generator.py | 29 ++++++++++------------ 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 331b835c27ad..21c7a1d23523 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -38,14 +38,16 @@ def hamming_15_11(number: str) -> str: "Input must be an 11-bit binary string containing only '0's and '1's." """ - is_bin = True # assuming its binary initially - for i in number: - if i not in ("0", "1"): + 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]) or i in range(len(number))] + + digits = [int(number[i]) for i in range(len(number))] + total_num_1 = sum(digits) hamming_digits = [0] * 16 @@ -58,28 +60,24 @@ def hamming_15_11(number: str) -> str: redundant_bits = [0] * 5 - redundant_bits_index = ( - 1 # starting from 1 as we need to find the 0th redundancy bit once - ) - # all the other redundancy bits have been found - for i in parity_positions.values(): + redundant_bits_index = 1 + for positions in parity_positions.values(): parity = 0 - for idx in i: - parity ^= digits[idx] + for idx in positions: + parity ^= digits[idx] redundant_bits[redundant_bits_index] = parity redundant_bits_index += 1 redundant_bits[0] = ( - int(total_num_1 % 2) + total_num_1 % 2 ^ redundant_bits[1] ^ redundant_bits[2] ^ redundant_bits[3] ^ redundant_bits[4] ) - # this is the 0th redundancy bit which takes into account the other 15 bits - j = -1 - r = 0 + j = -1 + r = 0 redundant_bit_locations = [0, 1, 2, 4, 8] for k in range(16): if k in redundant_bit_locations: @@ -94,7 +92,6 @@ def hamming_15_11(number: str) -> str: else: return "Input must be an 11-bit binary string containing only '0's and '1's." - if __name__ == "__main__": import doctest From 68178707a0487fd285a1589b6345347f04af53cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:21:33 +0000 Subject: [PATCH 18/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 21c7a1d23523..7b5583d1fdce 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -39,15 +39,14 @@ def hamming_15_11(number: str) -> str: """ is_bin = True # assuming it's binary initially - for i in number: - if i not in ("0", "1"): + 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) hamming_digits = [0] * 16 @@ -60,11 +59,11 @@ def hamming_15_11(number: str) -> str: redundant_bits = [0] * 5 - redundant_bits_index = 1 + redundant_bits_index = 1 for positions in parity_positions.values(): parity = 0 - for idx in positions: - parity ^= digits[idx] + for idx in positions: + parity ^= digits[idx] redundant_bits[redundant_bits_index] = parity redundant_bits_index += 1 @@ -76,8 +75,8 @@ def hamming_15_11(number: str) -> str: ^ redundant_bits[4] ) - j = -1 - r = 0 + j = -1 + r = 0 redundant_bit_locations = [0, 1, 2, 4, 8] for k in range(16): if k in redundant_bit_locations: @@ -92,6 +91,7 @@ def hamming_15_11(number: str) -> str: else: return "Input must be an 11-bit binary string containing only '0's and '1's." + if __name__ == "__main__": import doctest From ac9983ea7747b7b169fcfa6b246e95bec496aa26 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 22:42:43 +0530 Subject: [PATCH 19/25] fix: Update for more readability of code --- bit_manipulation/hamming_code_generator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 21c7a1d23523..061cd9f06192 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -48,7 +48,7 @@ def hamming_15_11(number: str) -> str: digits = [int(number[i]) for i in range(len(number))] - total_num_1 = sum(digits) + total_ones = sum(digits) hamming_digits = [0] * 16 parity_positions = { @@ -69,7 +69,7 @@ def hamming_15_11(number: str) -> str: redundant_bits_index += 1 redundant_bits[0] = ( - total_num_1 % 2 + total_ones % 2 ^ redundant_bits[1] ^ redundant_bits[2] ^ redundant_bits[3] @@ -89,7 +89,7 @@ def hamming_15_11(number: str) -> str: return "".join([str(i) for i in hamming_digits]) - else: + if len(number) != 11 or not is_bin: return "Input must be an 11-bit binary string containing only '0's and '1's." if __name__ == "__main__": From dbea02ce04a6a3683bcd60203f8c3edcfc3ce8f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:14:29 +0000 Subject: [PATCH 20/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 0b3e040270e8..57c6471f74e2 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -46,7 +46,7 @@ def hamming_15_11(number: str) -> str: 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 From 3e4ed8a6740b47e343e491272eb8cc686d3e739e Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 22:49:17 +0530 Subject: [PATCH 21/25] fix: Update for readability --- bit_manipulation/hamming_code_generator.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 0b3e040270e8..cdec4894619a 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -88,8 +88,11 @@ def hamming_15_11(number: str) -> str: return "".join([str(i) for i in hamming_digits]) - if len(number) != 11 or not is_bin: + 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 None if __name__ == "__main__": From b34076c992942d2ecdcc7f516a84778d5816b930 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:20:29 +0000 Subject: [PATCH 22/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/hamming_code_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index effad7f5ce7a..dee391eb9587 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -90,7 +90,7 @@ def hamming_15_11(number: str) -> str: 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 None From f0ae8f31a8f7c58c9bba49cdb2fa951e3aad3f97 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 22:53:07 +0530 Subject: [PATCH 23/25] fix: Update for readability --- bit_manipulation/hamming_code_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index effad7f5ce7a..c7c3efbbffcb 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -92,7 +92,7 @@ def hamming_15_11(number: str) -> str: return "Input must be an 11-bit binary string containing only '0's and '1's." else: - return None + return "Invalid input" if __name__ == "__main__": From defaba967072103cf1d52a4fee5f52191cb5d314 Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Tue, 8 Oct 2024 23:33:06 +0530 Subject: [PATCH 24/25] fix: Update variable names --- bit_manipulation/hamming_code_generator.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 8b22c14f324c..02d9e5c138cf 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -75,16 +75,17 @@ def hamming_15_11(number: str) -> str: ^ redundant_bits[4] ) - j = -1 + data_index = 0 r = 0 redundant_bit_locations = [0, 1, 2, 4, 8] for k in range(16): if k in redundant_bit_locations: + print(k) hamming_digits[k] = redundant_bits[r] r += 1 else: - j += 1 - hamming_digits[k] = digits[j] + hamming_digits[k] = digits[data_index] + data_index += 1 return "".join([str(i) for i in hamming_digits]) From 68e84800f1f7de1f33452478d4be78153547206a Mon Sep 17 00:00:00 2001 From: AyushChakraborty Date: Wed, 9 Oct 2024 11:01:11 +0530 Subject: [PATCH 25/25] fix: Update to remove unecessary variable (r) --- bit_manipulation/hamming_code_generator.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bit_manipulation/hamming_code_generator.py b/bit_manipulation/hamming_code_generator.py index 02d9e5c138cf..f819994b25ba 100644 --- a/bit_manipulation/hamming_code_generator.py +++ b/bit_manipulation/hamming_code_generator.py @@ -76,13 +76,10 @@ def hamming_15_11(number: str) -> str: ) data_index = 0 - r = 0 redundant_bit_locations = [0, 1, 2, 4, 8] for k in range(16): if k in redundant_bit_locations: - print(k) - hamming_digits[k] = redundant_bits[r] - r += 1 + hamming_digits[k] = redundant_bits.pop(0) else: hamming_digits[k] = digits[data_index] data_index += 1