Skip to content

Commit a1ea76b

Browse files
realDuYuanChaogithub-actionscclauss
authored
Optimization problem_10 in project_euler (#2453)
* optimization for problem09 in project_euler * added benchmark code * fixup! Format Python code with psf/black push * Update project_euler/problem_09/sol1.py Co-authored-by: Christian Clauss <[email protected]> * updating DIRECTORY.md * Update project_euler/problem_09/sol1.py * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 718be54 commit a1ea76b

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@
713713
## Strings
714714
* [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py)
715715
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
716+
* [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py)
716717
* [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py)
717718
* [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py)
718719
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)

Diff for: project_euler/problem_09/sol1.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def solution():
1616
1. a < b < c
1717
2. a**2 + b**2 = c**2
1818
3. a + b + c = 1000
19-
2019
# The code below has been commented due to slow execution affecting Travis.
2120
# >>> solution()
2221
# 31875000
@@ -30,6 +29,40 @@ def solution():
3029
return a * b * c
3130

3231

32+
def solution_fast():
33+
"""
34+
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
35+
the following:
36+
1. a < b < c
37+
2. a**2 + b**2 = c**2
38+
3. a + b + c = 1000
39+
40+
# The code below has been commented due to slow execution affecting Travis.
41+
# >>> solution_fast()
42+
# 31875000
43+
"""
44+
for a in range(300):
45+
for b in range(400):
46+
c = 1000 - a - b
47+
if a < b < c and (a ** 2) + (b ** 2) == (c ** 2):
48+
return a * b * c
49+
50+
51+
def benchmark() -> None:
52+
"""
53+
Benchmark code comparing two different version function.
54+
"""
55+
import timeit
56+
57+
print(
58+
timeit.timeit("solution()", setup="from __main__ import solution", number=1000)
59+
)
60+
print(
61+
timeit.timeit(
62+
"solution_fast()", setup="from __main__ import solution_fast", number=1000
63+
)
64+
)
65+
66+
3367
if __name__ == "__main__":
34-
print("Please Wait...")
35-
print(solution())
68+
benchmark()

Diff for: project_euler/problem_09/sol3.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ def solution():
2525
# 31875000
2626
"""
2727
return [
28-
a * b * c
28+
a * b * (1000 - a - b)
2929
for a in range(1, 999)
3030
for b in range(a, 999)
31-
for c in range(b, 999)
32-
if (a * a + b * b == c * c) and (a + b + c == 1000)
31+
if (a * a + b * b == (1000 - a - b) ** 2)
3332
][0]
3433

3534

Diff for: strings/can_string_be_rearranged_as_palindrome.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
# Counter is faster for long strings and non-Counter is faster for short strings.
99

1010

11-
def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool:
11+
def can_string_be_rearranged_as_palindrome_counter(
12+
input_str: str = "",
13+
) -> bool:
1214
"""
1315
A Palindrome is a String that reads the same forward as it does backwards.
1416
Examples of Palindromes mom, dad, malayalam

0 commit comments

Comments
 (0)