mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
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 <cclauss@me.com> * 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 <cclauss@me.com>
This commit is contained in:
parent
718be54dbb
commit
a1ea76bcf3
@ -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)
|
||||
|
@ -16,7 +16,6 @@ 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
|
||||
@ -30,6 +29,40 @@ def solution():
|
||||
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
|
||||
if a < b < c and (a ** 2) + (b ** 2) == (c ** 2):
|
||||
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=1000)
|
||||
)
|
||||
print(
|
||||
timeit.timeit(
|
||||
"solution_fast()", setup="from __main__ import solution_fast", number=1000
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Please Wait...")
|
||||
print(solution())
|
||||
benchmark()
|
||||
|
@ -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]
|
||||
|
||||
|
||||
|
@ -8,7 +8,9 @@ from timeit import timeit
|
||||
# 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
|
||||
|
Loading…
Reference in New Issue
Block a user