Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0591968

Browse files
realDuYuanChaogithub-actions
and
github-actions
authoredAug 21, 2020
Optimization and fix bug (TheAlgorithms#2342)
* * optimization aliquot_sum * fix bug in average_median * fixup! Format Python code with psf/black push * Update maths/average_median.py * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 2eaacee commit 0591968

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed
 

‎DIRECTORY.md

+10
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
* [Queue On Pseudo Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_pseudo_stack.py)
139139
* Stacks
140140
* [Balanced Parentheses](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/balanced_parentheses.py)
141+
* [Dijkstras Two Stack Algorithm](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/dijkstras_two_stack_algorithm.py)
141142
* [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py)
142143
* [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py)
143144
* [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py)
@@ -587,10 +588,18 @@
587588
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_35/sol1.py)
588589
* Problem 36
589590
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_36/sol1.py)
591+
* Problem 37
592+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_37/sol1.py)
593+
* Problem 39
594+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_39/sol1.py)
590595
* Problem 40
591596
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_40/sol1.py)
597+
* Problem 41
598+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_41/sol1.py)
592599
* Problem 42
593600
* [Solution42](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_42/solution42.py)
601+
* Problem 43
602+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_43/sol1.py)
594603
* Problem 47
595604
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_47/sol1.py)
596605
* Problem 48
@@ -676,6 +685,7 @@
676685
## Strings
677686
* [Aho-Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho-corasick.py)
678687
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
688+
* [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py)
679689
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)
680690
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py)
681691
* [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py)

‎data_structures/stacks/dijkstras_two_stack_algorithm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"""
3232
__author__ = "Alexander Joslin"
3333

34-
from .stack import Stack
35-
3634
import operator as op
3735

36+
from .stack import Stack
37+
3838

3939
def dijkstras_two_stack_algorithm(equation: str) -> int:
4040
"""

‎maths/aliquot_sum.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def aliquot_sum(input_num: int) -> int:
3737
raise ValueError("Input must be an integer")
3838
if input_num <= 0:
3939
raise ValueError("Input must be positive")
40-
return sum(divisor for divisor in range(1, input_num) if input_num % divisor == 0)
40+
return sum(
41+
divisor for divisor in range(1, input_num // 2 + 1) if input_num % divisor == 0
42+
)
4143

4244

4345
if __name__ == "__main__":

‎maths/average_median.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ def median(nums):
66
0
77
>>> median([4,1,3,2])
88
2.5
9+
>>> median([2, 70, 6, 50, 20, 8, 4])
10+
8
911
1012
Args:
1113
nums: List of nums
@@ -14,22 +16,19 @@ def median(nums):
1416
Median.
1517
"""
1618
sorted_list = sorted(nums)
17-
med = None
18-
if len(sorted_list) % 2 == 0:
19-
mid_index_1 = len(sorted_list) // 2
20-
mid_index_2 = (len(sorted_list) // 2) - 1
21-
med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2)
22-
else:
23-
mid_index = (len(sorted_list) - 1) // 2
24-
med = sorted_list[mid_index]
25-
return med
19+
length = len(sorted_list)
20+
mid_index = length >> 1
21+
return (
22+
(sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
23+
if length % 2 == 0
24+
else sorted_list[mid_index]
25+
)
2626

2727

2828
def main():
29-
print("Odd number of numbers:")
30-
print(median([2, 4, 6, 8, 20, 50, 70]))
31-
print("Even number of numbers:")
32-
print(median([2, 4, 6, 8, 20, 50]))
29+
import doctest
30+
31+
doctest.testmod()
3332

3433

3534
if __name__ == "__main__":

‎project_euler/problem_39/sol1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
For which value of p ≤ 1000, is the number of solutions maximised?
77
"""
88

9-
from typing import Dict
109
from collections import Counter
10+
from typing import Dict
1111

1212

1313
def pythagorean_triple(max_perimeter: int) -> Dict:

‎project_euler/problem_41/sol1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
from itertools import permutations
12
from math import sqrt
23
from typing import List
3-
from itertools import permutations
44

55
"""
66
We shall say that an n-digit number is pandigital if it makes use of all the digits

‎strings/check_anagrams.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ def check_anagrams(a: str, b: str) -> bool:
1717
input_B = input("Enter the second string ").strip()
1818

1919
status = check_anagrams(input_A, input_B)
20-
print(
21-
f"{input_A} and {input_B} are {'' if status else 'not '}anagrams."
22-
)
20+
print(f"{input_A} and {input_B} are {'' if status else 'not '}anagrams.")

0 commit comments

Comments
 (0)
Please sign in to comment.