From cc4f646a1481aa698cd62247a6a84ca4f35dcd27 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 20 Sep 2020 21:04:20 +0800 Subject: [PATCH 1/7] optimization for problem09 in project_euler --- project_euler/problem_09/sol1.py | 8 +++----- project_euler/problem_09/sol3.py | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 3bb5c968115d..ad9db1d89da9 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -23,11 +23,9 @@ def solution(): """ for a in range(300): for b in range(400): - for c in range(500): - if a < b < c: - if (a ** 2) + (b ** 2) == (c ** 2): - if (a + b + c) == 1000: - return a * b * c + c = 1000 - a - b + if a < b < c and (a ** 2) + (b ** 2) == (c ** 2): + return a * b * c if __name__ == "__main__": diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index a6df46a3a66b..ed27f089bd40 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -25,11 +25,10 @@ def solution(): # 31875000 """ return [ - a * b * c + a * b * (1000 - a - b) for a in range(1, 999) for b in range(a, 999) - for c in range(b, 999) - if (a * a + b * b == c * c) and (a + b + c == 1000) + if (a * a + b * b == (1000 - a - b) ** 2) ][0] From 806f39c191028891555e3ca79669217077e579d0 Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 22 Sep 2020 15:54:50 +0800 Subject: [PATCH 2/7] added benchmark code --- project_euler/problem_09/sol1.py | 34 +++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index ad9db1d89da9..6eb916c2772c 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -16,11 +16,31 @@ def solution(): 1. a < b < c 2. a**2 + b**2 = c**2 3. a + b + c = 1000 - # The code below has been commented due to slow execution affecting Travis. # >>> solution() # 31875000 """ + for a in range(300): + for b in range(400): + for c in range(500): + if a < b < c: + if (a ** 2) + (b ** 2) == (c ** 2): + if (a + b + c) == 1000: + return a * b * c + + +def solution_fast(): + """ + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution_fast() + # 31875000 + """ for a in range(300): for b in range(400): c = 1000 - a - b @@ -28,6 +48,14 @@ def solution(): return a * b * c +def benchmark() -> None: + """ + Benchmark code comparing two different version function. + """ + import timeit + print(timeit.timeit("solution()", setup="from __main__ import solution", number=1)) + print(timeit.timeit("solution_fast()", setup="from __main__ import solution_fast", number=1)) + + if __name__ == "__main__": - print("Please Wait...") - print(solution()) + benchmark() From daf182c1b6f0581cdf911516ff6c92e583a134b1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 22 Sep 2020 07:57:06 +0000 Subject: [PATCH 3/7] fixup! Format Python code with psf/black push --- project_euler/problem_09/sol1.py | 7 ++++++- strings/can_string_be_rearranged_as_palindrome.py | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 6eb916c2772c..1f8793592e0e 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -53,8 +53,13 @@ def benchmark() -> None: Benchmark code comparing two different version function. """ import timeit + print(timeit.timeit("solution()", setup="from __main__ import solution", number=1)) - print(timeit.timeit("solution_fast()", setup="from __main__ import solution_fast", number=1)) + print( + timeit.timeit( + "solution_fast()", setup="from __main__ import solution_fast", number=1 + ) + ) if __name__ == "__main__": diff --git a/strings/can_string_be_rearranged_as_palindrome.py b/strings/can_string_be_rearranged_as_palindrome.py index 92bc3b95b243..7fedc5877e26 100644 --- a/strings/can_string_be_rearranged_as_palindrome.py +++ b/strings/can_string_be_rearranged_as_palindrome.py @@ -8,7 +8,9 @@ # Counter is faster for long strings and non-Counter is faster for short strings. -def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool: +def can_string_be_rearranged_as_palindrome_counter( + input_str: str = "", +) -> bool: """ A Palindrome is a String that reads the same forward as it does backwards. Examples of Palindromes mom, dad, malayalam From 466da9e7029c0efd8b1b9d9fe316a080655d8046 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Tue, 22 Sep 2020 16:09:38 +0800 Subject: [PATCH 4/7] Update project_euler/problem_09/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_09/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 1f8793592e0e..95517849d833 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -57,7 +57,7 @@ def benchmark() -> None: print(timeit.timeit("solution()", setup="from __main__ import solution", number=1)) print( timeit.timeit( - "solution_fast()", setup="from __main__ import solution_fast", number=1 + "solution_fast()", setup="from __main__ import solution_fast", number=1000 ) ) From 0d0cdf0f03328ca5b1cb6fc3485ff97620898142 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 22 Sep 2020 08:10:06 +0000 Subject: [PATCH 5/7] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d91d34803a1c..03044e01084b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -713,6 +713,7 @@ ## Strings * [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py) * [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py) + * [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py) * [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py) * [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py) * [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py) From 18d1dd5513b0975d5f257c4f69ac37bc718d1683 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Tue, 22 Sep 2020 19:57:13 +0800 Subject: [PATCH 6/7] Update project_euler/problem_09/sol1.py --- project_euler/problem_09/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 95517849d833..1f5ef56b7f73 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -54,7 +54,7 @@ def benchmark() -> None: """ import timeit - print(timeit.timeit("solution()", setup="from __main__ import solution", number=1)) + print(timeit.timeit("solution()", setup="from __main__ import solution", number=1000)) print( timeit.timeit( "solution_fast()", setup="from __main__ import solution_fast", number=1000 From 87702ede49f9a04197d2e7b8a2143edaaa0701f6 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 22 Sep 2020 11:58:13 +0000 Subject: [PATCH 7/7] fixup! Format Python code with psf/black push --- project_euler/problem_09/sol1.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 1f5ef56b7f73..caba6b1b1530 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -54,7 +54,9 @@ def benchmark() -> None: """ import timeit - print(timeit.timeit("solution()", setup="from __main__ import solution", number=1000)) + print( + timeit.timeit("solution()", setup="from __main__ import solution", number=1000) + ) print( timeit.timeit( "solution_fast()", setup="from __main__ import solution_fast", number=1000