From 4e9df4a06fa70843867faac49cbe9e8f2d8d83eb Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:12:28 +0530 Subject: [PATCH 01/42] Fix: Issue 9588 --- maths/base_neg2_conversion.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 maths/base_neg2_conversion.py diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py new file mode 100644 index 000000000000..f06c639ac291 --- /dev/null +++ b/maths/base_neg2_conversion.py @@ -0,0 +1,37 @@ +import doctest +def decimal_to_negative_base_2(n): + """ + This function returns the number negative base 2 of the decimal number of the input data. + + Args: + n (int): The decimal number to convert. + + Returns: + int: The negative base 2 number. + + Examples: + >>> decimal_to_negative_base_2(0) + 0 + >>> decimal_to_negative_base_2(-19) + 111101 + >>> decimal_to_negative_base_2(4) + 100 + >>> decimal_to_negative_base_2(7) + 11011 + """ + if n == 0: + return 0 + ans = "" + while n != 0: + rem = n % -2 + n = n // -2 + if rem < 0: + rem += 2 + n += 1 + + ans = str(rem) + ans + + return int(ans) + +if __name__ == "__main__": + doctest.testmod() From 1fab3027ccbf98562ae04c2ea4a95559bb01c2ae Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:13:13 +0530 Subject: [PATCH 02/42] Fix: Issue 9588 --- maths/base_neg2_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index f06c639ac291..e57942ba01db 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -4,7 +4,7 @@ def decimal_to_negative_base_2(n): This function returns the number negative base 2 of the decimal number of the input data. Args: - n (int): The decimal number to convert. + int: The decimal number to convert. Returns: int: The negative base 2 number. From 143764d456c9a018b9fd3570a1d504ea42fe2b37 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:49:29 +0000 Subject: [PATCH 03/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/base_neg2_conversion.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index e57942ba01db..55d0416bcb8f 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,4 +1,6 @@ import doctest + + def decimal_to_negative_base_2(n): """ This function returns the number negative base 2 of the decimal number of the input data. @@ -33,5 +35,6 @@ def decimal_to_negative_base_2(n): return int(ans) + if __name__ == "__main__": doctest.testmod() From 045652d3eaefd00aee1b3f146b657014f4f6c498 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:37:58 +0530 Subject: [PATCH 04/42] Fix: Issue 9588 --- maths/base_neg2_conversion.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index e57942ba01db..3bdcf91abe1b 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,7 +1,8 @@ import doctest -def decimal_to_negative_base_2(n): +def decimal_to_negative_base_2(n: int) -> int: """ - This function returns the number negative base 2 of the decimal number of the input data. + This function returns the number negative base 2 + of the decimal number of the input data. Args: int: The decimal number to convert. From 95161a0f6fe9f095b3d79ac70ea55a2abbfa13b5 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:41:27 +0530 Subject: [PATCH 05/42] Fix: Issue #9588 --- maths/base_neg2_conversion.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 1693852ca597..3bdcf91abe1b 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,5 +1,5 @@ import doctest -def decimal_to_negative_base_2(n): +def decimal_to_negative_base_2(n: int) -> int: """ This function returns the number negative base 2 of the decimal number of the input data. @@ -34,6 +34,5 @@ def decimal_to_negative_base_2(n): return int(ans) - if __name__ == "__main__": doctest.testmod() From 155601c61d9963ac4635fc88df05c93b15bf4fa6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:12:43 +0000 Subject: [PATCH 06/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/base_neg2_conversion.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 3bdcf91abe1b..8c86c22eaf29 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,7 +1,9 @@ import doctest + + def decimal_to_negative_base_2(n: int) -> int: """ - This function returns the number negative base 2 + This function returns the number negative base 2 of the decimal number of the input data. Args: @@ -34,5 +36,6 @@ def decimal_to_negative_base_2(n: int) -> int: return int(ans) + if __name__ == "__main__": doctest.testmod() From 376cb60519cad5f39e9d710df940677434972d62 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:49:09 +0530 Subject: [PATCH 07/42] Fix: Issue #9588 --- maths/base_neg2_conversion.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 3bdcf91abe1b..56ce246d8c6d 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,5 +1,5 @@ import doctest -def decimal_to_negative_base_2(n: int) -> int: +def decimal_to_negative_base_2(num: int) -> int: """ This function returns the number negative base 2 of the decimal number of the input data. @@ -20,15 +20,15 @@ def decimal_to_negative_base_2(n: int) -> int: >>> decimal_to_negative_base_2(7) 11011 """ - if n == 0: + if num == 0: return 0 ans = "" - while n != 0: - rem = n % -2 - n = n // -2 + while num != 0: + rem = num % -2 + num = num // -2 if rem < 0: rem += 2 - n += 1 + num += 1 ans = str(rem) + ans From a878e4ba71bc421437060f06e7eb9dc45eead74a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:21:07 +0000 Subject: [PATCH 08/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/base_neg2_conversion.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 6077d1dbe90e..b2d98536b5e3 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,4 +1,6 @@ import doctest + + def decimal_to_negative_base_2(num: int) -> int: """ This function returns the number negative base 2 From 115703ebc755c8b35e14acc1a97224ee757b1d74 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 10:41:41 +0530 Subject: [PATCH 09/42] Fix: Issue #9588 --- maths/base_neg2_conversion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 6077d1dbe90e..432c74d5c064 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -1,8 +1,7 @@ -import doctest def decimal_to_negative_base_2(num: int) -> int: """ This function returns the number negative base 2 - of the decimal number of the input data. + of the decimal number of the input data. Args: int: The decimal number to convert. @@ -36,4 +35,5 @@ def decimal_to_negative_base_2(num: int) -> int: if __name__ == "__main__": + import doctest doctest.testmod() From 23ef488cbd3794c363a7efe77b225adb4d7dcf84 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 05:12:59 +0000 Subject: [PATCH 10/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/base_neg2_conversion.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 432c74d5c064..f9c8a7f24707 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -36,4 +36,5 @@ def decimal_to_negative_base_2(num: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() From 6b474217bac391745a17b346e1ca1061ab92068b Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:31:08 +0530 Subject: [PATCH 11/42] fix: issue #9793 --- .../activation_functions}/sigmoid_linear_unit.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {maths => neural_network/activation_functions}/sigmoid_linear_unit.py (100%) diff --git a/maths/sigmoid_linear_unit.py b/neural_network/activation_functions/sigmoid_linear_unit.py similarity index 100% rename from maths/sigmoid_linear_unit.py rename to neural_network/activation_functions/sigmoid_linear_unit.py From e7bc55ce6dd7a75fb693a9d01d09c86813714cc0 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:39:12 +0530 Subject: [PATCH 12/42] fix: issue #9793 --- maths/base_neg2_conversion.py | 8 ++------ .../activation_functions => maths}/sigmoid_linear_unit.py | 0 2 files changed, 2 insertions(+), 6 deletions(-) rename {neural_network/activation_functions => maths}/sigmoid_linear_unit.py (100%) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index f9c8a7f24707..bb1f756432cd 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -23,18 +23,14 @@ def decimal_to_negative_base_2(num: int) -> int: return 0 ans = "" while num != 0: - rem = num % -2 - num = num // -2 + num,rem =divmod(num, -2) if rem < 0: rem += 2 num += 1 - ans = str(rem) + ans - return int(ans) if __name__ == "__main__": import doctest - - doctest.testmod() + doctest.testmod() \ No newline at end of file diff --git a/neural_network/activation_functions/sigmoid_linear_unit.py b/maths/sigmoid_linear_unit.py similarity index 100% rename from neural_network/activation_functions/sigmoid_linear_unit.py rename to maths/sigmoid_linear_unit.py From 8606171745e62dcbb9ce74bd55bbb1cdd17676b7 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:42:01 +0530 Subject: [PATCH 13/42] fix: issue #9588 --- maths/base_neg2_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index bb1f756432cd..35c74b2de121 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -33,4 +33,4 @@ def decimal_to_negative_base_2(num: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From f521847472d001e79ca804cf946fbe36030ea999 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:13:27 +0000 Subject: [PATCH 14/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/base_neg2_conversion.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/base_neg2_conversion.py b/maths/base_neg2_conversion.py index 35c74b2de121..81d40d37e79d 100644 --- a/maths/base_neg2_conversion.py +++ b/maths/base_neg2_conversion.py @@ -23,7 +23,7 @@ def decimal_to_negative_base_2(num: int) -> int: return 0 ans = "" while num != 0: - num,rem =divmod(num, -2) + num, rem = divmod(num, -2) if rem < 0: rem += 2 num += 1 @@ -33,4 +33,5 @@ def decimal_to_negative_base_2(num: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() From 5442a69bae2dc9865036aa077944f5b3698c6ef4 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Thu, 5 Oct 2023 23:58:52 +0530 Subject: [PATCH 15/42] fix: issue #9844 --- backtracking/match_word_pattern.py | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backtracking/match_word_pattern.py diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py new file mode 100644 index 000000000000..f0399ae55118 --- /dev/null +++ b/backtracking/match_word_pattern.py @@ -0,0 +1,54 @@ +def matchWordPattern(pattern :str, input_string: str) -> bool: + """ + Determine if a given pattern matches a string using backtracking. + + pattern: The pattern to match. + input_string: The string to match against the pattern. + return: True if the pattern matches the string, False otherwise. + + >>> matchWordPattern("aba", "GraphTreesGraph") + True + + >>> matchWordPattern("xyx", "PythonRubyPython") + True + + >>> matchWordPattern("GG", "PythonJavaPython") + False + """ + def backtrack(pattern_index, str_index): + if pattern_index == len(pattern) and str_index == len(input_string): + return True + if pattern_index == len(pattern) or str_index == len(input_string): + return False + + char = pattern[pattern_index] + + if char in pattern_map: + mapped_str = pattern_map[char] + if input_string.startswith(mapped_str, str_index): + return backtrack(pattern_index + 1, str_index + len(mapped_str)) + else: + return False + + for end in range(str_index + 1, len(input_string) + 1): + substr = input_string[str_index:end] + if substr in str_map: + continue + + pattern_map[char] = substr + str_map[substr] = char + + if backtrack(pattern_index + 1, end): + return True + + del pattern_map[char] + del str_map[substr] + return False + + pattern_map = {} + str_map = {} + return backtrack(0, 0) + +if __name__ == "__main__": + import doctest + doctest.testmod() From 4f0baa21986c351264db21b7a0d565d877d90073 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 18:34:35 +0000 Subject: [PATCH 16/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/match_word_pattern.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index f0399ae55118..5132f58525ca 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -1,4 +1,4 @@ -def matchWordPattern(pattern :str, input_string: str) -> bool: +def matchWordPattern(pattern: str, input_string: str) -> bool: """ Determine if a given pattern matches a string using backtracking. @@ -15,40 +15,43 @@ def matchWordPattern(pattern :str, input_string: str) -> bool: >>> matchWordPattern("GG", "PythonJavaPython") False """ + def backtrack(pattern_index, str_index): if pattern_index == len(pattern) and str_index == len(input_string): return True if pattern_index == len(pattern) or str_index == len(input_string): return False - + char = pattern[pattern_index] - + if char in pattern_map: mapped_str = pattern_map[char] if input_string.startswith(mapped_str, str_index): return backtrack(pattern_index + 1, str_index + len(mapped_str)) else: return False - + for end in range(str_index + 1, len(input_string) + 1): substr = input_string[str_index:end] if substr in str_map: continue - + pattern_map[char] = substr str_map[substr] = char - + if backtrack(pattern_index + 1, end): return True - + del pattern_map[char] del str_map[substr] return False - + pattern_map = {} str_map = {} return backtrack(0, 0) + if __name__ == "__main__": import doctest + doctest.testmod() From c1680baee148748a0410552d90b7705e29ff1a03 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 00:05:28 +0530 Subject: [PATCH 17/42] fix: issue #9844 --- backtracking/match_word_pattern.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index f0399ae55118..257d18bf6c42 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -1,4 +1,4 @@ -def matchWordPattern(pattern :str, input_string: str) -> bool: +def match_word_pattern(pattern :str, input_string: str) -> bool: """ Determine if a given pattern matches a string using backtracking. @@ -6,13 +6,13 @@ def matchWordPattern(pattern :str, input_string: str) -> bool: input_string: The string to match against the pattern. return: True if the pattern matches the string, False otherwise. - >>> matchWordPattern("aba", "GraphTreesGraph") + >>> match_word_pattern("aba", "GraphTreesGraph") True - >>> matchWordPattern("xyx", "PythonRubyPython") + >>> match_word_pattern("xyx", "PythonRubyPython") True - >>> matchWordPattern("GG", "PythonJavaPython") + >>> match_word_pattern("GG", "PythonJavaPython") False """ def backtrack(pattern_index, str_index): @@ -20,31 +20,24 @@ def backtrack(pattern_index, str_index): return True if pattern_index == len(pattern) or str_index == len(input_string): return False - char = pattern[pattern_index] - if char in pattern_map: mapped_str = pattern_map[char] if input_string.startswith(mapped_str, str_index): return backtrack(pattern_index + 1, str_index + len(mapped_str)) else: return False - for end in range(str_index + 1, len(input_string) + 1): substr = input_string[str_index:end] if substr in str_map: continue - pattern_map[char] = substr str_map[substr] = char - if backtrack(pattern_index + 1, end): return True - del pattern_map[char] del str_map[substr] return False - pattern_map = {} str_map = {} return backtrack(0, 0) From 7c9c36e4886d2ef1788b6e0a968f256601684ed2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 18:38:36 +0000 Subject: [PATCH 18/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/match_word_pattern.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index 257d18bf6c42..63c8af24fe0d 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -1,4 +1,4 @@ -def match_word_pattern(pattern :str, input_string: str) -> bool: +def match_word_pattern(pattern: str, input_string: str) -> bool: """ Determine if a given pattern matches a string using backtracking. @@ -15,6 +15,7 @@ def match_word_pattern(pattern :str, input_string: str) -> bool: >>> match_word_pattern("GG", "PythonJavaPython") False """ + def backtrack(pattern_index, str_index): if pattern_index == len(pattern) and str_index == len(input_string): return True @@ -38,10 +39,13 @@ def backtrack(pattern_index, str_index): del pattern_map[char] del str_map[substr] return False + pattern_map = {} str_map = {} return backtrack(0, 0) + if __name__ == "__main__": import doctest + doctest.testmod() From e3a45ebebd8187d6c55021ee61f18eb7585df82a Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 00:15:37 +0530 Subject: [PATCH 19/42] fix: issue #9844 --- backtracking/match_word_pattern.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index 257d18bf6c42..79a59a312302 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -38,8 +38,8 @@ def backtrack(pattern_index, str_index): del pattern_map[char] del str_map[substr] return False - pattern_map = {} - str_map = {} + pattern_map: dict[str,str] = {} + str_map: dict[str,str] = {} return backtrack(0, 0) if __name__ == "__main__": From c4fa0e17731254ad19b971085622a80b18c451dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 18:49:12 +0000 Subject: [PATCH 20/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/match_word_pattern.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index 1366571da4df..a1c9f7f8d5e3 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -39,8 +39,9 @@ def backtrack(pattern_index, str_index): del pattern_map[char] del str_map[substr] return False - pattern_map: dict[str,str] = {} - str_map: dict[str,str] = {} + + pattern_map: dict[str, str] = {} + str_map: dict[str, str] = {} return backtrack(0, 0) From 26e33d817d4da3f17cec02988de1aa256b94d35e Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 00:23:48 +0530 Subject: [PATCH 21/42] fix: issue #9844 --- backtracking/match_word_pattern.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index a1c9f7f8d5e3..f0bafb85a2b3 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -16,7 +16,17 @@ def match_word_pattern(pattern: str, input_string: str) -> bool: False """ - def backtrack(pattern_index, str_index): + def backtrack(pattern_index: int, str_index: int) -> bool: + """ + >>> backtrack(0, 0) + True + + >>> backtrack(0, 1) + True + + >>> backtrack(0, 4) + False + """ if pattern_index == len(pattern) and str_index == len(input_string): return True if pattern_index == len(pattern) or str_index == len(input_string): @@ -39,7 +49,6 @@ def backtrack(pattern_index, str_index): del pattern_map[char] del str_map[substr] return False - pattern_map: dict[str, str] = {} str_map: dict[str, str] = {} return backtrack(0, 0) @@ -47,5 +56,4 @@ def backtrack(pattern_index, str_index): if __name__ == "__main__": import doctest - doctest.testmod() From 8df08a3566bf2d0c58fa2f53b36676c4b11ce3ef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 18:56:40 +0000 Subject: [PATCH 22/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/match_word_pattern.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backtracking/match_word_pattern.py b/backtracking/match_word_pattern.py index f0bafb85a2b3..bfa9b1354d51 100644 --- a/backtracking/match_word_pattern.py +++ b/backtracking/match_word_pattern.py @@ -49,6 +49,7 @@ def backtrack(pattern_index: int, str_index: int) -> bool: del pattern_map[char] del str_map[substr] return False + pattern_map: dict[str, str] = {} str_map: dict[str, str] = {} return backtrack(0, 0) @@ -56,4 +57,5 @@ def backtrack(pattern_index: int, str_index: int) -> bool: if __name__ == "__main__": import doctest + doctest.testmod() From f7ca0d4b62578cbedf589b8043bf775326cb6619 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 12:30:09 +0530 Subject: [PATCH 23/42] Added: Project Euler - 60 solution #9895 --- project_euler/problem_060/__init__.py | 0 project_euler/problem_060/sol1.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 project_euler/problem_060/__init__.py create mode 100644 project_euler/problem_060/sol1.py diff --git a/project_euler/problem_060/__init__.py b/project_euler/problem_060/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py new file mode 100644 index 000000000000..e69de29bb2d1 From c068f6e9702770c79e260a2db3de0e381e42f1b1 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:19:51 +0530 Subject: [PATCH 24/42] Added: Project Euler - 60 solution #9895 --- project_euler/problem_060/sol1.py | 185 ++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index e69de29bb2d1..26a15b13dc85 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -0,0 +1,185 @@ +import random +from typing import List + +""" +The primes 3, 7, 109 and 673 are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109 both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. +Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. +""" + + +# Sieve of Eratosthenes to generate prime numbers +def sieve_of_eratosthenes(limit: int) -> List[int]: + """ + Generates prime numbers up to the specified limit using the Sieve of Eratosthenes algorithm. + + Parameters: + limit (int): The upper limit for prime number generation. + + Returns: + List[int]: A list of prime numbers up to the given limit. + + >>> sieve_of_eratosthenes(2) + [2] + >>> sieve_of_eratosthenes(5) + [2, 3] + """ + is_prime = [True] * limit + if(limit>2): + is_prime[0] = False + is_prime[1] = False + is_prime[2] = True + + for number in range(3, int(limit**0.5) + 1, 2): + if is_prime[number]: + for multiple in range(number * 2, limit, number): + is_prime[multiple] = False + + primes = [2] + for number in range(3, limit, 2): + if is_prime[number]: + primes.append(number) + + return primes + +# Miller–Rabin primality test +def is_prime(number: int, k: int = 3) -> bool: + """ + Tests if a given number is prime using the Miller-Rabin primality test. + + Parameters: + number (int): The number to be tested for primality. + k (int): The number of iterations for the Miller-Rabin test. + + Returns: + bool: True if the number is probably prime, False otherwise. + + >>> is_prime(2) + True + >>> is_prime(3) + True + >>> is_prime(6) + False + """ + if number < 6: + return [False, False, True, True, False, True][number] + elif number % 2 == 0: + return False + else: + s, d = 0, number - 1 + while d % 2 == 0: + s, d = s + 1, d >> 1 + for a in random.sample(range(2, number - 2), k): + x = pow(a, d, number) + if x != 1 and x + 1 != number: + for r in range(1, s): + x = pow(x, 2, number) + if x == 1: + return False + elif x == number - 1: + a = 0 + break + if a: + return False + return True + +# Check if two numbers form a prime pair +def forms_prime_pair(num1: int, num2: int) -> bool: + """ + Checks if two numbers form a prime pair when concatenated in any order. + + Parameters: + num1 (int): The first number. + num2 (int): The second number. + + Returns: + bool: True if the numbers form a prime pair, False otherwise. + + >>> forms_prime_pair(1,3) + True + >>> forms_prime_pair(2,3) + False + """ + num1_str = str(num1) + num2_str = str(num2) + num1_num2_concat = int(num1_str + num2_str) + num2_num1_concat = int(num2_str + num1_str) + return is_prime(num1_num2_concat) and is_prime(num2_num1_concat) + +# Finding prime numbers up to a given limit +def find_primes(limit: int) -> List[int]: + """ + Finds prime numbers up to the specified limit using the Sieve of Eratosthenes algorithm. + + Parameters: + limit (int): The upper limit for prime number generation. + + Returns: + List[int]: A list of prime numbers up to the given limit. + >>> find_primes(2) + [2] + >>> find_primes(5) + [2, 3] + + + """ + return sieve_of_eratosthenes(limit) + +# Problem solution +def find_lowest_sum_of_five_primes(primes: List[int]) -> int: + """ + Finds the lowest sum of five prime numbers that form prime pairs when concatenated in any order. + + Parameters: + primes (List[int]): A list of prime numbers. + + Returns: + int: The lowest sum of five prime numbers that satisfy the condition. + + + >>> find_lowest_sum_of_five_primes([2, 3]) + 0 + >>> find_lowest_sum_of_five_primes([2, 3, 5]) + 0 + + """ + for prime1 in primes: + for prime2 in primes: + if prime2 < prime1: + continue + if forms_prime_pair(prime1, prime2): + for prime3 in primes: + if prime3 < prime2: + continue + if forms_prime_pair(prime1, prime3) and forms_prime_pair(prime2, prime3): + for prime4 in primes: + if prime4 < prime3: + continue + if forms_prime_pair(prime1, prime4) and forms_prime_pair(prime2, prime4) and forms_prime_pair(prime3, prime4): + for prime5 in primes: + if prime5 < prime4: + continue + if forms_prime_pair(prime1, prime5) and forms_prime_pair(prime2, prime5) and forms_prime_pair(prime3, prime5) and forms_prime_pair(prime4, prime5): + return prime1 + prime2 + prime3 + prime4 + prime5 + return 0 + +def solution(primes_limit: int = 10000) -> int: + """ + the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. + Parameters: + primes_limit (int): The upper limit for prime number generation. + + Returns: + int: The lowest sum of five prime numbers. + >>> solution(1) + 0 + >>> solution(10000) + 26033 + + """ + primes_list: List[int] = find_primes(primes_limit) + # Run the function and print the output + return find_lowest_sum_of_five_primes(primes_list) + +if __name__ == "__main__": + import doctest + doctest.testmod() From 35569952c99cf1ce264d43312b507b8f691321e8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 07:50:26 +0000 Subject: [PATCH 25/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_060/sol1.py | 36 ++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index 26a15b13dc85..0f11c4e92ac3 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -24,23 +24,24 @@ def sieve_of_eratosthenes(limit: int) -> List[int]: [2, 3] """ is_prime = [True] * limit - if(limit>2): + if limit > 2: is_prime[0] = False is_prime[1] = False is_prime[2] = True - + for number in range(3, int(limit**0.5) + 1, 2): if is_prime[number]: for multiple in range(number * 2, limit, number): is_prime[multiple] = False - + primes = [2] for number in range(3, limit, 2): if is_prime[number]: primes.append(number) - + return primes + # Miller–Rabin primality test def is_prime(number: int, k: int = 3) -> bool: """ @@ -82,6 +83,7 @@ def is_prime(number: int, k: int = 3) -> bool: return False return True + # Check if two numbers form a prime pair def forms_prime_pair(num1: int, num2: int) -> bool: """ @@ -105,6 +107,7 @@ def forms_prime_pair(num1: int, num2: int) -> bool: num2_num1_concat = int(num2_str + num1_str) return is_prime(num1_num2_concat) and is_prime(num2_num1_concat) + # Finding prime numbers up to a given limit def find_primes(limit: int) -> List[int]: """ @@ -124,6 +127,7 @@ def find_primes(limit: int) -> List[int]: """ return sieve_of_eratosthenes(limit) + # Problem solution def find_lowest_sum_of_five_primes(primes: List[int]) -> int: """ @@ -150,18 +154,32 @@ def find_lowest_sum_of_five_primes(primes: List[int]) -> int: for prime3 in primes: if prime3 < prime2: continue - if forms_prime_pair(prime1, prime3) and forms_prime_pair(prime2, prime3): + if forms_prime_pair(prime1, prime3) and forms_prime_pair( + prime2, prime3 + ): for prime4 in primes: if prime4 < prime3: continue - if forms_prime_pair(prime1, prime4) and forms_prime_pair(prime2, prime4) and forms_prime_pair(prime3, prime4): + if ( + forms_prime_pair(prime1, prime4) + and forms_prime_pair(prime2, prime4) + and forms_prime_pair(prime3, prime4) + ): for prime5 in primes: if prime5 < prime4: continue - if forms_prime_pair(prime1, prime5) and forms_prime_pair(prime2, prime5) and forms_prime_pair(prime3, prime5) and forms_prime_pair(prime4, prime5): - return prime1 + prime2 + prime3 + prime4 + prime5 + if ( + forms_prime_pair(prime1, prime5) + and forms_prime_pair(prime2, prime5) + and forms_prime_pair(prime3, prime5) + and forms_prime_pair(prime4, prime5) + ): + return ( + prime1 + prime2 + prime3 + prime4 + prime5 + ) return 0 + def solution(primes_limit: int = 10000) -> int: """ the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. @@ -180,6 +198,8 @@ def solution(primes_limit: int = 10000) -> int: # Run the function and print the output return find_lowest_sum_of_five_primes(primes_list) + if __name__ == "__main__": import doctest + doctest.testmod() From a314c4509ba1c2d6fe029b443c6a7788d6110fd2 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:29:36 +0530 Subject: [PATCH 26/42] Added: Project Euler - 60 solution #9895 --- project_euler/problem_060/sol1.py | 77 ++++++++++++------------------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index 0f11c4e92ac3..653f1dca5109 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -1,22 +1,26 @@ import random -from typing import List """ -The primes 3, 7, 109 and 673 are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109 both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. -Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. +The primes 3, 7, 109 and 673 are quite remarkable. By taking any +two primes and concatenating them in any order the result will always be prime. +For example, taking 7 and 109 both 7109 and 1097 are prime. +The sum of these four primes, 792, represents the lowest sum +for a set of four primes with this property. +Find the lowest sum for a set of five primes +for which any two primes concatenate to produce another prime. """ -# Sieve of Eratosthenes to generate prime numbers -def sieve_of_eratosthenes(limit: int) -> List[int]: +def sieve_of_eratosthenes(limit: int) -> list[int]: """ - Generates prime numbers up to the specified limit using the Sieve of Eratosthenes algorithm. + Generates prime numbers up to the specified + limit using the Sieve of Eratosthenes algorithm. Parameters: limit (int): The upper limit for prime number generation. Returns: - List[int]: A list of prime numbers up to the given limit. + list[int]: A list of prime numbers up to the given limit. >>> sieve_of_eratosthenes(2) [2] @@ -24,25 +28,21 @@ def sieve_of_eratosthenes(limit: int) -> List[int]: [2, 3] """ is_prime = [True] * limit - if limit > 2: + if (limit > 2): is_prime[0] = False is_prime[1] = False is_prime[2] = True - for number in range(3, int(limit**0.5) + 1, 2): if is_prime[number]: for multiple in range(number * 2, limit, number): is_prime[multiple] = False - primes = [2] for number in range(3, limit, 2): if is_prime[number]: primes.append(number) - return primes -# Miller–Rabin primality test def is_prime(number: int, k: int = 3) -> bool: """ Tests if a given number is prime using the Miller-Rabin primality test. @@ -84,7 +84,6 @@ def is_prime(number: int, k: int = 3) -> bool: return True -# Check if two numbers form a prime pair def forms_prime_pair(num1: int, num2: int) -> bool: """ Checks if two numbers form a prime pair when concatenated in any order. @@ -107,44 +106,42 @@ def forms_prime_pair(num1: int, num2: int) -> bool: num2_num1_concat = int(num2_str + num1_str) return is_prime(num1_num2_concat) and is_prime(num2_num1_concat) - # Finding prime numbers up to a given limit -def find_primes(limit: int) -> List[int]: + + +def find_primes(limit: int) -> list[int]: """ - Finds prime numbers up to the specified limit using the Sieve of Eratosthenes algorithm. + Finds prime numbers up to the specified limit + using the Sieve of Eratosthenes algorithm. Parameters: limit (int): The upper limit for prime number generation. Returns: - List[int]: A list of prime numbers up to the given limit. + list[int]: A list of prime numbers up to the given limit. >>> find_primes(2) [2] >>> find_primes(5) [2, 3] - - """ return sieve_of_eratosthenes(limit) -# Problem solution -def find_lowest_sum_of_five_primes(primes: List[int]) -> int: +def find_lowest_sum_of_five_primes(primes: list[int]) -> int: """ - Finds the lowest sum of five prime numbers that form prime pairs when concatenated in any order. + Finds the lowest sum of five prime numbers that + form prime pairs when concatenated in any order. Parameters: - primes (List[int]): A list of prime numbers. + primes (list[int]): A list of prime numbers. Returns: int: The lowest sum of five prime numbers that satisfy the condition. - >>> find_lowest_sum_of_five_primes([2, 3]) 0 >>> find_lowest_sum_of_five_primes([2, 3, 5]) 0 - """ for prime1 in primes: for prime2 in primes: @@ -154,35 +151,24 @@ def find_lowest_sum_of_five_primes(primes: List[int]) -> int: for prime3 in primes: if prime3 < prime2: continue - if forms_prime_pair(prime1, prime3) and forms_prime_pair( - prime2, prime3 - ): + if forms_prime_pair(prime1, prime3) and forms_prime_pair(prime2, prime3): for prime4 in primes: if prime4 < prime3: continue - if ( - forms_prime_pair(prime1, prime4) - and forms_prime_pair(prime2, prime4) - and forms_prime_pair(prime3, prime4) - ): + if forms_prime_pair(prime1, prime4) and forms_prime_pair(prime2, prime4) and forms_prime_pair(prime3, prime4): for prime5 in primes: if prime5 < prime4: continue - if ( - forms_prime_pair(prime1, prime5) - and forms_prime_pair(prime2, prime5) - and forms_prime_pair(prime3, prime5) - and forms_prime_pair(prime4, prime5) - ): - return ( - prime1 + prime2 + prime3 + prime4 + prime5 - ) + if forms_prime_pair(prime1, prime5) and forms_prime_pair(prime2, prime5) and forms_prime_pair(prime3, prime5) and forms_prime_pair(prime4, prime5): + return prime1 + prime2 + prime3 + prime4 + prime5 return 0 def solution(primes_limit: int = 10000) -> int: """ - the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. + the lowest sum for a set of five primes for which + any two primes concatenate to produce another prime. + Parameters: primes_limit (int): The upper limit for prime number generation. @@ -192,14 +178,11 @@ def solution(primes_limit: int = 10000) -> int: 0 >>> solution(10000) 26033 - """ - primes_list: List[int] = find_primes(primes_limit) - # Run the function and print the output + primes_list: list[int] = find_primes(primes_limit) return find_lowest_sum_of_five_primes(primes_list) if __name__ == "__main__": import doctest - doctest.testmod() From 8a4f18e95bbb54fbc44cd82fd3c49689f16c353f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 08:00:34 +0000 Subject: [PATCH 27/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_060/sol1.py | 45 ++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index 653f1dca5109..2778b4fd8f37 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -1,19 +1,19 @@ import random """ -The primes 3, 7, 109 and 673 are quite remarkable. By taking any -two primes and concatenating them in any order the result will always be prime. -For example, taking 7 and 109 both 7109 and 1097 are prime. -The sum of these four primes, 792, represents the lowest sum +The primes 3, 7, 109 and 673 are quite remarkable. By taking any +two primes and concatenating them in any order the result will always be prime. +For example, taking 7 and 109 both 7109 and 1097 are prime. +The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. -Find the lowest sum for a set of five primes +Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. """ def sieve_of_eratosthenes(limit: int) -> list[int]: """ - Generates prime numbers up to the specified + Generates prime numbers up to the specified limit using the Sieve of Eratosthenes algorithm. Parameters: @@ -28,7 +28,7 @@ def sieve_of_eratosthenes(limit: int) -> list[int]: [2, 3] """ is_prime = [True] * limit - if (limit > 2): + if limit > 2: is_prime[0] = False is_prime[1] = False is_prime[2] = True @@ -106,12 +106,13 @@ def forms_prime_pair(num1: int, num2: int) -> bool: num2_num1_concat = int(num2_str + num1_str) return is_prime(num1_num2_concat) and is_prime(num2_num1_concat) + # Finding prime numbers up to a given limit def find_primes(limit: int) -> list[int]: """ - Finds prime numbers up to the specified limit + Finds prime numbers up to the specified limit using the Sieve of Eratosthenes algorithm. Parameters: @@ -129,7 +130,7 @@ def find_primes(limit: int) -> list[int]: def find_lowest_sum_of_five_primes(primes: list[int]) -> int: """ - Finds the lowest sum of five prime numbers that + Finds the lowest sum of five prime numbers that form prime pairs when concatenated in any order. Parameters: @@ -151,24 +152,37 @@ def find_lowest_sum_of_five_primes(primes: list[int]) -> int: for prime3 in primes: if prime3 < prime2: continue - if forms_prime_pair(prime1, prime3) and forms_prime_pair(prime2, prime3): + if forms_prime_pair(prime1, prime3) and forms_prime_pair( + prime2, prime3 + ): for prime4 in primes: if prime4 < prime3: continue - if forms_prime_pair(prime1, prime4) and forms_prime_pair(prime2, prime4) and forms_prime_pair(prime3, prime4): + if ( + forms_prime_pair(prime1, prime4) + and forms_prime_pair(prime2, prime4) + and forms_prime_pair(prime3, prime4) + ): for prime5 in primes: if prime5 < prime4: continue - if forms_prime_pair(prime1, prime5) and forms_prime_pair(prime2, prime5) and forms_prime_pair(prime3, prime5) and forms_prime_pair(prime4, prime5): - return prime1 + prime2 + prime3 + prime4 + prime5 + if ( + forms_prime_pair(prime1, prime5) + and forms_prime_pair(prime2, prime5) + and forms_prime_pair(prime3, prime5) + and forms_prime_pair(prime4, prime5) + ): + return ( + prime1 + prime2 + prime3 + prime4 + prime5 + ) return 0 def solution(primes_limit: int = 10000) -> int: """ - the lowest sum for a set of five primes for which + the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. - + Parameters: primes_limit (int): The upper limit for prime number generation. @@ -185,4 +199,5 @@ def solution(primes_limit: int = 10000) -> int: if __name__ == "__main__": import doctest + doctest.testmod() From f884189f33b8baa2cd321f804ef8ed51c79d7447 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:04:30 +0530 Subject: [PATCH 28/42] Added: Project Euler - 60 solution #9895 --- project_euler/problem_060/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index 653f1dca5109..b244aaf09c91 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -72,7 +72,7 @@ def is_prime(number: int, k: int = 3) -> bool: for a in random.sample(range(2, number - 2), k): x = pow(a, d, number) if x != 1 and x + 1 != number: - for r in range(1, s): + for _ in range(1, s): x = pow(x, 2, number) if x == 1: return False From 1817d80e421e21f1784d7061b81d9ca12ea2c74e Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:54:09 +0530 Subject: [PATCH 29/42] changed name of the k --- project_euler/problem_060/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index 0b4cac7f3abf..e60bf3dffad9 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -43,13 +43,13 @@ def sieve_of_eratosthenes(limit: int) -> list[int]: return primes -def is_prime(number: int, k: int = 3) -> bool: +def is_prime(number: int, test_size: int = 3) -> bool: """ Tests if a given number is prime using the Miller-Rabin primality test. Parameters: number (int): The number to be tested for primality. - k (int): The number of iterations for the Miller-Rabin test. + test_size (int): The number of iterations for the Miller-Rabin test. Returns: bool: True if the number is probably prime, False otherwise. @@ -69,7 +69,7 @@ def is_prime(number: int, k: int = 3) -> bool: s, d = 0, number - 1 while d % 2 == 0: s, d = s + 1, d >> 1 - for a in random.sample(range(2, number - 2), k): + for a in random.sample(range(2, number - 2), test_size): x = pow(a, d, number) if x != 1 and x + 1 != number: for _ in range(1, s): From 3af271761ae5ab7f099c12b3526dd1c42effd204 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 01:46:37 +0530 Subject: [PATCH 30/42] Add: new algo remove_invalid_parentheses --- backtracking/remove_invalid_parenthese.py | 105 ++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 backtracking/remove_invalid_parenthese.py diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py new file mode 100644 index 000000000000..3aa2775db920 --- /dev/null +++ b/backtracking/remove_invalid_parenthese.py @@ -0,0 +1,105 @@ +def remove_invalid_parentheses(input_string: str) -> list[str]: + """ + Removes the minimum number of invalid parentheses + to make the input string valid. + + Args: + input_string (str): The input string containing parentheses. + + Returns: + List[str]: A list of valid strings after removing the minimum number of parentheses. + + Examples: + >>> remove_invalid_parentheses(")(a)())") + ['(a())', '(a)()'] + + >>> remove_invalid_parentheses("()())()") + ['(())()', '()()()'] + + >>> remove_invalid_parentheses(")(") + [''] + """ + def is_valid(next_string: str) -> bool: + """ + Helper function to check if a string has valid parentheses. + + Params: + next_string (str): The input string to be checked. + + Returns: + bool: True if the parentheses in the string are valid, False otherwise. + + Examples: + >>> is_valid("(a)())") + False + + >>> is_valid("(a())") + True + + >>> is_valid(")(a)()") + False + + """ + count: int = 0 + for char in next_string: + if char == '(': + count += 1 + elif char == ')': + count -= 1 + if count < 0: + return False + return count == 0 + + def dfs(current_string: str, start_index: int, left_removed: int, right_removed: int) -> None: + """ + Depth-first search function to generate valid combinations + of the input string. + + Params: + current_string (str): The current string being processed. + start_index (int): The index to start processing from. + left_removed (int): The number of left parentheses to be removed. + right_removed (int): The number of right parentheses to be removed. + Returns: + None + Examples: + >>> dfs("()())()", 0, 0, 1) + None + + >>> dfs("()())(", 6, 0, 0) + None + + >>> dfs(")(a)())", 0, 0,, 2) + None + """ + if left_removed == 0 and right_removed == 0: + if is_valid(current_string): + valid_parentheses.add(current_string) + return + for i in range(start_index, len(current_string)): + if i > start_index and current_string[i] == current_string[i - 1]: + continue + if current_string[i] == '(' and left_removed > 0: + dfs(current_string[:i] + current_string[i + 1:], + i, left_removed - 1, right_removed) + elif current_string[i] == ')' and right_removed > 0: + dfs(current_string[:i] + current_string[i + 1:], + i, left_removed, right_removed - 1) + valid_parentheses: set = set() + left_removed_count: int = 0 + right_removed_count: int = 0 + for char in input_string: + if char == '(': + left_removed_count += 1 + elif char == ')': + if left_removed_count > 0: + left_removed_count -= 1 + else: + right_removed_count += 1 + dfs(input_string, 0, left_removed_count, right_removed_count) + return sorted(list(valid_parentheses)) + + +if __name__ == "__main__": + import doctest + doctest.testmod() From d8d28a69553cc9de741eb56e3ffb4e5fcca00da2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 20:17:35 +0000 Subject: [PATCH 31/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/remove_invalid_parenthese.py | 39 +++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 3aa2775db920..cc6069050b25 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -1,6 +1,6 @@ def remove_invalid_parentheses(input_string: str) -> list[str]: """ - Removes the minimum number of invalid parentheses + Removes the minimum number of invalid parentheses to make the input string valid. Args: @@ -19,6 +19,7 @@ def remove_invalid_parentheses(input_string: str) -> list[str]: >>> remove_invalid_parentheses(")(") [''] """ + def is_valid(next_string: str) -> bool: """ Helper function to check if a string has valid parentheses. @@ -28,7 +29,7 @@ def is_valid(next_string: str) -> bool: Returns: bool: True if the parentheses in the string are valid, False otherwise. - + Examples: >>> is_valid("(a)())") False @@ -42,15 +43,17 @@ def is_valid(next_string: str) -> bool: """ count: int = 0 for char in next_string: - if char == '(': + if char == "(": count += 1 - elif char == ')': + elif char == ")": count -= 1 if count < 0: return False return count == 0 - def dfs(current_string: str, start_index: int, left_removed: int, right_removed: int) -> None: + def dfs( + current_string: str, start_index: int, left_removed: int, right_removed: int + ) -> None: """ Depth-first search function to generate valid combinations of the input string. @@ -79,19 +82,28 @@ def dfs(current_string: str, start_index: int, left_removed: int, right_removed: for i in range(start_index, len(current_string)): if i > start_index and current_string[i] == current_string[i - 1]: continue - if current_string[i] == '(' and left_removed > 0: - dfs(current_string[:i] + current_string[i + 1:], - i, left_removed - 1, right_removed) - elif current_string[i] == ')' and right_removed > 0: - dfs(current_string[:i] + current_string[i + 1:], - i, left_removed, right_removed - 1) + if current_string[i] == "(" and left_removed > 0: + dfs( + current_string[:i] + current_string[i + 1 :], + i, + left_removed - 1, + right_removed, + ) + elif current_string[i] == ")" and right_removed > 0: + dfs( + current_string[:i] + current_string[i + 1 :], + i, + left_removed, + right_removed - 1, + ) + valid_parentheses: set = set() left_removed_count: int = 0 right_removed_count: int = 0 for char in input_string: - if char == '(': + if char == "(": left_removed_count += 1 - elif char == ')': + elif char == ")": if left_removed_count > 0: left_removed_count -= 1 else: @@ -102,4 +114,5 @@ def dfs(current_string: str, start_index: int, left_removed: int, right_removed: if __name__ == "__main__": import doctest + doctest.testmod() From 7c1426e29999b03ae2d1a33e8d8c73d05c0f94d8 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 01:50:07 +0530 Subject: [PATCH 32/42] Add: new algo remove_invalid_parentheses --- backtracking/remove_invalid_parenthese.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 3aa2775db920..10bc9a2a3148 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -1,13 +1,14 @@ def remove_invalid_parentheses(input_string: str) -> list[str]: """ - Removes the minimum number of invalid parentheses + Removes the minimum number of invalid parentheses to make the input string valid. Args: input_string (str): The input string containing parentheses. Returns: - List[str]: A list of valid strings after removing the minimum number of parentheses. + List[str]: A list of valid strings after removing + the minimum number of parentheses. Examples: >>> remove_invalid_parentheses(")(a)())") From 7506dce53d712dc5666343ea1286893ef064265f Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 01:55:01 +0530 Subject: [PATCH 33/42] Add: new algo remove_invalid_parentheses --- backtracking/remove_invalid_parenthese.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index f2ec90c11b0f..976caab0dd7c 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -7,7 +7,7 @@ def remove_invalid_parentheses(input_string: str) -> list[str]: input_string (str): The input string containing parentheses. Returns: - List[str]: A list of valid strings after removing + List[str]: A list of valid strings after removing the minimum number of parentheses. Examples: @@ -27,7 +27,6 @@ def is_valid(next_string: str) -> bool: Params: next_string (str): The input string to be checked. - Returns: bool: True if the parentheses in the string are valid, False otherwise. @@ -51,10 +50,9 @@ def is_valid(next_string: str) -> bool: if count < 0: return False return count == 0 - def dfs( - current_string: str, start_index: int, left_removed: int, right_removed: int - ) -> None: + current_string: str, start_index: int, left_removed: int, + right_removed: int) -> None: """ Depth-first search function to generate valid combinations of the input string. @@ -85,19 +83,18 @@ def dfs( continue if current_string[i] == "(" and left_removed > 0: dfs( - current_string[:i] + current_string[i + 1 :], + current_string[:i] + current_string[i + 1:], i, left_removed - 1, right_removed, ) elif current_string[i] == ")" and right_removed > 0: dfs( - current_string[:i] + current_string[i + 1 :], + current_string[:i] + current_string[i + 1:], i, left_removed, right_removed - 1, ) - valid_parentheses: set = set() left_removed_count: int = 0 right_removed_count: int = 0 @@ -110,7 +107,9 @@ def dfs( else: right_removed_count += 1 dfs(input_string, 0, left_removed_count, right_removed_count) - return sorted(list(valid_parentheses)) + output_list: list = list(valid_parentheses) + output_list.sort() + return output_list if __name__ == "__main__": From b7724b9508b1efabc5adbffd599d0da2219fb94d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 20:26:46 +0000 Subject: [PATCH 34/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/remove_invalid_parenthese.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 976caab0dd7c..15dc55fce8e2 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -50,9 +50,10 @@ def is_valid(next_string: str) -> bool: if count < 0: return False return count == 0 + def dfs( - current_string: str, start_index: int, left_removed: int, - right_removed: int) -> None: + current_string: str, start_index: int, left_removed: int, right_removed: int + ) -> None: """ Depth-first search function to generate valid combinations of the input string. @@ -83,18 +84,19 @@ def dfs( continue if current_string[i] == "(" and left_removed > 0: dfs( - current_string[:i] + current_string[i + 1:], + current_string[:i] + current_string[i + 1 :], i, left_removed - 1, right_removed, ) elif current_string[i] == ")" and right_removed > 0: dfs( - current_string[:i] + current_string[i + 1:], + current_string[:i] + current_string[i + 1 :], i, left_removed, right_removed - 1, ) + valid_parentheses: set = set() left_removed_count: int = 0 right_removed_count: int = 0 From 600e4705d8f8626722d70ab8b0138f121b8b8649 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 02:01:45 +0530 Subject: [PATCH 35/42] final --- backtracking/remove_invalid_parenthese.py | 118 ---------------------- 1 file changed, 118 deletions(-) delete mode 100644 backtracking/remove_invalid_parenthese.py diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py deleted file mode 100644 index 976caab0dd7c..000000000000 --- a/backtracking/remove_invalid_parenthese.py +++ /dev/null @@ -1,118 +0,0 @@ -def remove_invalid_parentheses(input_string: str) -> list[str]: - """ - Removes the minimum number of invalid parentheses - to make the input string valid. - - Args: - input_string (str): The input string containing parentheses. - - Returns: - List[str]: A list of valid strings after removing - the minimum number of parentheses. - - Examples: - >>> remove_invalid_parentheses(")(a)())") - ['(a())', '(a)()'] - - >>> remove_invalid_parentheses("()())()") - ['(())()', '()()()'] - - >>> remove_invalid_parentheses(")(") - [''] - """ - - def is_valid(next_string: str) -> bool: - """ - Helper function to check if a string has valid parentheses. - - Params: - next_string (str): The input string to be checked. - Returns: - bool: True if the parentheses in the string are valid, False otherwise. - - Examples: - >>> is_valid("(a)())") - False - - >>> is_valid("(a())") - True - - >>> is_valid(")(a)()") - False - - """ - count: int = 0 - for char in next_string: - if char == "(": - count += 1 - elif char == ")": - count -= 1 - if count < 0: - return False - return count == 0 - def dfs( - current_string: str, start_index: int, left_removed: int, - right_removed: int) -> None: - """ - Depth-first search function to generate valid combinations - of the input string. - - Params: - current_string (str): The current string being processed. - start_index (int): The index to start processing from. - left_removed (int): The number of left parentheses to be removed. - right_removed (int): The number of right parentheses to be removed. - Returns: - None - Examples: - >>> dfs("()())()", 0, 0, 1) - None - - >>> dfs("()())(", 6, 0, 0) - None - - >>> dfs(")(a)())", 0, 0,, 2) - None - """ - if left_removed == 0 and right_removed == 0: - if is_valid(current_string): - valid_parentheses.add(current_string) - return - for i in range(start_index, len(current_string)): - if i > start_index and current_string[i] == current_string[i - 1]: - continue - if current_string[i] == "(" and left_removed > 0: - dfs( - current_string[:i] + current_string[i + 1:], - i, - left_removed - 1, - right_removed, - ) - elif current_string[i] == ")" and right_removed > 0: - dfs( - current_string[:i] + current_string[i + 1:], - i, - left_removed, - right_removed - 1, - ) - valid_parentheses: set = set() - left_removed_count: int = 0 - right_removed_count: int = 0 - for char in input_string: - if char == "(": - left_removed_count += 1 - elif char == ")": - if left_removed_count > 0: - left_removed_count -= 1 - else: - right_removed_count += 1 - dfs(input_string, 0, left_removed_count, right_removed_count) - output_list: list = list(valid_parentheses) - output_list.sort() - return output_list - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From c3c2381754b95b8196084f86bc3f9564893e9c75 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 02:26:06 +0530 Subject: [PATCH 36/42] Add: new algo remove_invalid_parentheses --- project_euler/problem_060/__init__.py | 0 project_euler/problem_060/sol1.py | 203 -------------------------- 2 files changed, 203 deletions(-) delete mode 100644 project_euler/problem_060/__init__.py delete mode 100644 project_euler/problem_060/sol1.py diff --git a/project_euler/problem_060/__init__.py b/project_euler/problem_060/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py deleted file mode 100644 index e60bf3dffad9..000000000000 --- a/project_euler/problem_060/sol1.py +++ /dev/null @@ -1,203 +0,0 @@ -import random - -""" -The primes 3, 7, 109 and 673 are quite remarkable. By taking any -two primes and concatenating them in any order the result will always be prime. -For example, taking 7 and 109 both 7109 and 1097 are prime. -The sum of these four primes, 792, represents the lowest sum -for a set of four primes with this property. -Find the lowest sum for a set of five primes -for which any two primes concatenate to produce another prime. -""" - - -def sieve_of_eratosthenes(limit: int) -> list[int]: - """ - Generates prime numbers up to the specified - limit using the Sieve of Eratosthenes algorithm. - - Parameters: - limit (int): The upper limit for prime number generation. - - Returns: - list[int]: A list of prime numbers up to the given limit. - - >>> sieve_of_eratosthenes(2) - [2] - >>> sieve_of_eratosthenes(5) - [2, 3] - """ - is_prime = [True] * limit - if limit > 2: - is_prime[0] = False - is_prime[1] = False - is_prime[2] = True - for number in range(3, int(limit**0.5) + 1, 2): - if is_prime[number]: - for multiple in range(number * 2, limit, number): - is_prime[multiple] = False - primes = [2] - for number in range(3, limit, 2): - if is_prime[number]: - primes.append(number) - return primes - - -def is_prime(number: int, test_size: int = 3) -> bool: - """ - Tests if a given number is prime using the Miller-Rabin primality test. - - Parameters: - number (int): The number to be tested for primality. - test_size (int): The number of iterations for the Miller-Rabin test. - - Returns: - bool: True if the number is probably prime, False otherwise. - - >>> is_prime(2) - True - >>> is_prime(3) - True - >>> is_prime(6) - False - """ - if number < 6: - return [False, False, True, True, False, True][number] - elif number % 2 == 0: - return False - else: - s, d = 0, number - 1 - while d % 2 == 0: - s, d = s + 1, d >> 1 - for a in random.sample(range(2, number - 2), test_size): - x = pow(a, d, number) - if x != 1 and x + 1 != number: - for _ in range(1, s): - x = pow(x, 2, number) - if x == 1: - return False - elif x == number - 1: - a = 0 - break - if a: - return False - return True - - -def forms_prime_pair(num1: int, num2: int) -> bool: - """ - Checks if two numbers form a prime pair when concatenated in any order. - - Parameters: - num1 (int): The first number. - num2 (int): The second number. - - Returns: - bool: True if the numbers form a prime pair, False otherwise. - - >>> forms_prime_pair(1,3) - True - >>> forms_prime_pair(2,3) - False - """ - num1_str = str(num1) - num2_str = str(num2) - num1_num2_concat = int(num1_str + num2_str) - num2_num1_concat = int(num2_str + num1_str) - return is_prime(num1_num2_concat) and is_prime(num2_num1_concat) - - -# Finding prime numbers up to a given limit - - -def find_primes(limit: int) -> list[int]: - """ - Finds prime numbers up to the specified limit - using the Sieve of Eratosthenes algorithm. - - Parameters: - limit (int): The upper limit for prime number generation. - - Returns: - list[int]: A list of prime numbers up to the given limit. - >>> find_primes(2) - [2] - >>> find_primes(5) - [2, 3] - """ - return sieve_of_eratosthenes(limit) - - -def find_lowest_sum_of_five_primes(primes: list[int]) -> int: - """ - Finds the lowest sum of five prime numbers that - form prime pairs when concatenated in any order. - - Parameters: - primes (list[int]): A list of prime numbers. - - Returns: - int: The lowest sum of five prime numbers that satisfy the condition. - - >>> find_lowest_sum_of_five_primes([2, 3]) - 0 - >>> find_lowest_sum_of_five_primes([2, 3, 5]) - 0 - """ - for prime1 in primes: - for prime2 in primes: - if prime2 < prime1: - continue - if forms_prime_pair(prime1, prime2): - for prime3 in primes: - if prime3 < prime2: - continue - if forms_prime_pair(prime1, prime3) and forms_prime_pair( - prime2, prime3 - ): - for prime4 in primes: - if prime4 < prime3: - continue - if ( - forms_prime_pair(prime1, prime4) - and forms_prime_pair(prime2, prime4) - and forms_prime_pair(prime3, prime4) - ): - for prime5 in primes: - if prime5 < prime4: - continue - if ( - forms_prime_pair(prime1, prime5) - and forms_prime_pair(prime2, prime5) - and forms_prime_pair(prime3, prime5) - and forms_prime_pair(prime4, prime5) - ): - return ( - prime1 + prime2 + prime3 + prime4 + prime5 - ) - return 0 - - -def solution(primes_limit: int = 10000) -> int: - """ - the lowest sum for a set of five primes for which - any two primes concatenate to produce another prime. - - Parameters: - primes_limit (int): The upper limit for prime number generation. - - Returns: - int: The lowest sum of five prime numbers. - >>> solution(1) - 0 - >>> solution(10000) - 26033 - """ - primes_list: list[int] = find_primes(primes_limit) - return find_lowest_sum_of_five_primes(primes_list) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From e3b5ce52b4e7e41710a9fdffc46ada34e4d68c62 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:04:54 +0530 Subject: [PATCH 37/42] Update backtracking/remove_invalid_parenthese.py Co-authored-by: Caeden Perelli-Harris --- backtracking/remove_invalid_parenthese.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 15dc55fce8e2..7cc12fcfced5 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -97,9 +97,9 @@ def dfs( right_removed - 1, ) - valid_parentheses: set = set() - left_removed_count: int = 0 - right_removed_count: int = 0 + valid_parentheses = set() + left_removed_count = 0 + right_removed_count = 0 for char in input_string: if char == "(": left_removed_count += 1 From b261baffea642fcb93bf8c8666f88a1fc5b1e120 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:24:42 +0530 Subject: [PATCH 38/42] new algo added to remove invalid parentheses --- backtracking/remove_invalid_parenthese.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 7cc12fcfced5..4d1379627efa 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -96,8 +96,7 @@ def dfs( left_removed, right_removed - 1, ) - - valid_parentheses = set() + valid_parentheses:set = set() left_removed_count = 0 right_removed_count = 0 for char in input_string: From 289f73f8b47a616508936c5f496f8218b45a4ba9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 03:55:27 +0000 Subject: [PATCH 39/42] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/remove_invalid_parenthese.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 4d1379627efa..9f66bb912487 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -96,7 +96,8 @@ def dfs( left_removed, right_removed - 1, ) - valid_parentheses:set = set() + + valid_parentheses: set = set() left_removed_count = 0 right_removed_count = 0 for char in input_string: From ac8f6ec19fb441c35a7a43c2f0d5498842bea9b2 Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:26:46 +0530 Subject: [PATCH 40/42] Update backtracking/remove_invalid_parenthese.py Co-authored-by: Christian Clauss --- backtracking/remove_invalid_parenthese.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backtracking/remove_invalid_parenthese.py b/backtracking/remove_invalid_parenthese.py index 9f66bb912487..756c169b9d58 100644 --- a/backtracking/remove_invalid_parenthese.py +++ b/backtracking/remove_invalid_parenthese.py @@ -109,9 +109,7 @@ def dfs( else: right_removed_count += 1 dfs(input_string, 0, left_removed_count, right_removed_count) - output_list: list = list(valid_parentheses) - output_list.sort() - return output_list + return sorted(valid_parentheses) if __name__ == "__main__": From f84a8e223c828c6768e476e91ff9d09ce1a02bfa Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:07:08 +0530 Subject: [PATCH 41/42] added decimal to gray conversion --- conversions/binary_to_gray.py | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 conversions/binary_to_gray.py diff --git a/conversions/binary_to_gray.py b/conversions/binary_to_gray.py new file mode 100644 index 000000000000..e94fe67b77f6 --- /dev/null +++ b/conversions/binary_to_gray.py @@ -0,0 +1,42 @@ +""" +This code converts gray code to binary +and +binary code to gray code. +""" + + +def binary_to_gray(binary: str) -> str: + """ + Convert a binary number to Gray code + binary: A binary number as a string. + return: The Gray code representation as a string. + >>> binary_to_gray("1101101") + '1011011' + + """ + gray = binary[0] + for i in range(1, len(binary)): + gray_bit = int(binary[i]) ^ int(binary[i - 1]) + gray += str(gray_bit) + return gray + + +def gray_to_binary(gray: str) -> str: + """ + Convert Gray code to binary. + + gray: A Gray code representation as a string. + return: The binary number as a string. + >>> gray_to_binary("1011110") + '1101011' + """ + binary = gray[0] + for i in range(1, len(gray)): + binary_bit = int(gray[i]) ^ int(binary[i - 1]) + binary += str(binary_bit) + return binary + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 0d6eb7f736ff3b93e751f048b989526afa5e707e Mon Sep 17 00:00:00 2001 From: Abul Hasan <33129246+haxkd@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:09:29 +0530 Subject: [PATCH 42/42] added decimal to gray conversion --- conversions/binary_to_decimal.py | 43 -------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 conversions/binary_to_decimal.py diff --git a/conversions/binary_to_decimal.py b/conversions/binary_to_decimal.py deleted file mode 100644 index 914a9318c225..000000000000 --- a/conversions/binary_to_decimal.py +++ /dev/null @@ -1,43 +0,0 @@ -def bin_to_decimal(bin_string: str) -> int: - """ - Convert a binary value to its decimal equivalent - - >>> bin_to_decimal("101") - 5 - >>> bin_to_decimal(" 1010 ") - 10 - >>> bin_to_decimal("-11101") - -29 - >>> bin_to_decimal("0") - 0 - >>> bin_to_decimal("a") - Traceback (most recent call last): - ... - ValueError: Non-binary value was passed to the function - >>> bin_to_decimal("") - Traceback (most recent call last): - ... - ValueError: Empty string was passed to the function - >>> bin_to_decimal("39") - Traceback (most recent call last): - ... - ValueError: Non-binary value was passed to the function - """ - bin_string = str(bin_string).strip() - if not bin_string: - raise ValueError("Empty string was passed to the function") - is_negative = bin_string[0] == "-" - if is_negative: - bin_string = bin_string[1:] - if not all(char in "01" for char in bin_string): - raise ValueError("Non-binary value was passed to the function") - decimal_number = 0 - for char in bin_string: - decimal_number = 2 * decimal_number + int(char) - return -decimal_number if is_negative else decimal_number - - -if __name__ == "__main__": - from doctest import testmod - - testmod()