File tree 4 files changed +42
-7
lines changed
4 files changed +42
-7
lines changed Original file line number Diff line number Diff line change 713
713
## Strings
714
714
* [ Aho Corasick] ( https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py )
715
715
* [ 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 )
716
717
* [ Capitalize] ( https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py )
717
718
* [ Check Anagrams] ( https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py )
718
719
* [ Check Pangram] ( https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py )
Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ def solution():
16
16
1. a < b < c
17
17
2. a**2 + b**2 = c**2
18
18
3. a + b + c = 1000
19
-
20
19
# The code below has been commented due to slow execution affecting Travis.
21
20
# >>> solution()
22
21
# 31875000
@@ -30,6 +29,40 @@ def solution():
30
29
return a * b * c
31
30
32
31
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
+
33
67
if __name__ == "__main__" :
34
- print ("Please Wait..." )
35
- print (solution ())
68
+ benchmark ()
Original file line number Diff line number Diff line change @@ -25,11 +25,10 @@ def solution():
25
25
# 31875000
26
26
"""
27
27
return [
28
- a * b * c
28
+ a * b * ( 1000 - a - b )
29
29
for a in range (1 , 999 )
30
30
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 )
33
32
][0 ]
34
33
35
34
Original file line number Diff line number Diff line change 8
8
# Counter is faster for long strings and non-Counter is faster for short strings.
9
9
10
10
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 :
12
14
"""
13
15
A Palindrome is a String that reads the same forward as it does backwards.
14
16
Examples of Palindromes mom, dad, malayalam
You can’t perform that action at this time.
0 commit comments