From cbe8a324ad997925fbe45a55b2c70f6187efcf73 Mon Sep 17 00:00:00 2001 From: surlogu Date: Sat, 18 Oct 2025 21:08:53 +0530 Subject: [PATCH 1/5] Add binary parity pattern implementation and update directory --- DIRECTORY.md | 1 + bit_manipulation/binary_parity_pattern.py | 42 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 bit_manipulation/binary_parity_pattern.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 0f9859577493..5fc35ac3d212 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -37,6 +37,7 @@ * [Binary Twos Complement](bit_manipulation/binary_twos_complement.py) * [Binary Xor Operator](bit_manipulation/binary_xor_operator.py) * [Bitwise Addition Recursive](bit_manipulation/bitwise_addition_recursive.py) + * [Binary Parity Pattern](bit_manipulation/binary_parity_pattern.py) * [Count 1S Brian Kernighan Method](bit_manipulation/count_1s_brian_kernighan_method.py) * [Count Number Of One Bits](bit_manipulation/count_number_of_one_bits.py) * [Excess 3 Code](bit_manipulation/excess_3_code.py) diff --git a/bit_manipulation/binary_parity_pattern.py b/bit_manipulation/binary_parity_pattern.py new file mode 100644 index 000000000000..0737da50f841 --- /dev/null +++ b/bit_manipulation/binary_parity_pattern.py @@ -0,0 +1,42 @@ +""" +======================================= +⚖️ Binary Parity Pattern +======================================= + +Generates a binary pattern where each bit represents +the parity (even/odd) of the cumulative sum of bits +in the original binary representation. + +Example: + 13 (1101) → cumulative sums [1,2,3,4] + parity pattern = 1010 +""" + +def binary_parity_pattern(number: int) -> str: + """ + Generate parity pattern of cumulative binary sums. + + >>> binary_parity_pattern(13) + '0b1010' + >>> binary_parity_pattern(7) + '0b111' + >>> binary_parity_pattern(4) + '0b10' + """ + if number < 0: + number = abs(number) + + binary = bin(number)[2:] + parity_pattern = "" + cum_sum = 0 + + for bit in binary: + cum_sum += int(bit) + parity_pattern += str(cum_sum % 2) + + return "0b" + parity_pattern + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 9dc42bbcf6e36fc829861c678b63a17d8792e085 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 18 Oct 2025 15:49:07 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_parity_pattern.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/binary_parity_pattern.py b/bit_manipulation/binary_parity_pattern.py index 0737da50f841..2bf89cba3ec8 100644 --- a/bit_manipulation/binary_parity_pattern.py +++ b/bit_manipulation/binary_parity_pattern.py @@ -3,8 +3,8 @@ ⚖️ Binary Parity Pattern ======================================= -Generates a binary pattern where each bit represents -the parity (even/odd) of the cumulative sum of bits +Generates a binary pattern where each bit represents +the parity (even/odd) of the cumulative sum of bits in the original binary representation. Example: @@ -12,6 +12,7 @@ parity pattern = 1010 """ + def binary_parity_pattern(number: int) -> str: """ Generate parity pattern of cumulative binary sums. @@ -39,4 +40,5 @@ def binary_parity_pattern(number: int) -> str: if __name__ == "__main__": import doctest + doctest.testmod() From 1e70a8731ed6646960514be812badda3dcbd70e3 Mon Sep 17 00:00:00 2001 From: surlogu Date: Sat, 18 Oct 2025 22:21:01 +0530 Subject: [PATCH 3/5] fix:correct binary_parity_pattern doctest and return format --- bit_manipulation/binary_parity_pattern.py | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/bit_manipulation/binary_parity_pattern.py b/bit_manipulation/binary_parity_pattern.py index 2bf89cba3ec8..393922669eef 100644 --- a/bit_manipulation/binary_parity_pattern.py +++ b/bit_manipulation/binary_parity_pattern.py @@ -1,21 +1,18 @@ """ -======================================= -⚖️ Binary Parity Pattern -======================================= +binary_parity_pattern.py +======================== -Generates a binary pattern where each bit represents -the parity (even/odd) of the cumulative sum of bits -in the original binary representation. +Generates a binary parity pattern based on the cumulative sum of bits +in the binary representation of an integer. -Example: - 13 (1101) → cumulative sums [1,2,3,4] - parity pattern = 1010 +Reference: +https://en.wikipedia.org/wiki/Parity_(mathematics) """ def binary_parity_pattern(number: int) -> str: """ - Generate parity pattern of cumulative binary sums. + Return a binary parity pattern string for a given integer. >>> binary_parity_pattern(13) '0b1010' @@ -23,19 +20,22 @@ def binary_parity_pattern(number: int) -> str: '0b111' >>> binary_parity_pattern(4) '0b10' + >>> binary_parity_pattern(0) + '0b0' """ if number < 0: - number = abs(number) + raise ValueError("Number must be non-negative") - binary = bin(number)[2:] - parity_pattern = "" + binary_str = bin(number)[2:] cum_sum = 0 + pattern = [] - for bit in binary: + for bit in binary_str: cum_sum += int(bit) - parity_pattern += str(cum_sum % 2) + pattern.append(str(cum_sum % 2)) - return "0b" + parity_pattern + result = "0b" + "".join(pattern) + return result if result != "0b" else "0b0" if __name__ == "__main__": From 659d529c34aa08cf9f0f43a547773b067e2db128 Mon Sep 17 00:00:00 2001 From: surlogu Date: Mon, 20 Oct 2025 11:11:26 +0530 Subject: [PATCH 4/5] fix: correct doctest examples in binary_parity_pattern function --- bit_manipulation/binary_parity_pattern.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bit_manipulation/binary_parity_pattern.py b/bit_manipulation/binary_parity_pattern.py index 393922669eef..c49b66f60414 100644 --- a/bit_manipulation/binary_parity_pattern.py +++ b/bit_manipulation/binary_parity_pattern.py @@ -15,17 +15,20 @@ def binary_parity_pattern(number: int) -> str: Return a binary parity pattern string for a given integer. >>> binary_parity_pattern(13) - '0b1010' + '0b1001' >>> binary_parity_pattern(7) - '0b111' + '0b101' >>> binary_parity_pattern(4) - '0b10' + '0b111' >>> binary_parity_pattern(0) '0b0' """ if number < 0: raise ValueError("Number must be non-negative") + if number == 0: + return "0b0" + binary_str = bin(number)[2:] cum_sum = 0 pattern = [] @@ -34,11 +37,10 @@ def binary_parity_pattern(number: int) -> str: cum_sum += int(bit) pattern.append(str(cum_sum % 2)) - result = "0b" + "".join(pattern) - return result if result != "0b" else "0b0" + result = ''.join(pattern).lstrip('0') + return '0b' + (result if result else '0') if __name__ == "__main__": import doctest - doctest.testmod() From adbdda857a1efd396e89475eabe74f88172a205a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Oct 2025 05:42:00 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_parity_pattern.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/binary_parity_pattern.py b/bit_manipulation/binary_parity_pattern.py index c49b66f60414..d2691e38c77f 100644 --- a/bit_manipulation/binary_parity_pattern.py +++ b/bit_manipulation/binary_parity_pattern.py @@ -28,7 +28,7 @@ def binary_parity_pattern(number: int) -> str: if number == 0: return "0b0" - + binary_str = bin(number)[2:] cum_sum = 0 pattern = [] @@ -37,10 +37,11 @@ def binary_parity_pattern(number: int) -> str: cum_sum += int(bit) pattern.append(str(cum_sum % 2)) - result = ''.join(pattern).lstrip('0') - return '0b' + (result if result else '0') + result = "".join(pattern).lstrip("0") + return "0b" + (result if result else "0") if __name__ == "__main__": import doctest + doctest.testmod()