From 0591968947ed3af5b5474eb40af0d8c0995769d5 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 21 Aug 2020 14:39:03 +0800 Subject: [PATCH] Optimization and fix bug (#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> --- DIRECTORY.md | 10 ++++++++ .../stacks/dijkstras_two_stack_algorithm.py | 4 +-- maths/aliquot_sum.py | 4 ++- maths/average_median.py | 25 +++++++++---------- project_euler/problem_39/sol1.py | 2 +- project_euler/problem_41/sol1.py | 2 +- strings/check_anagrams.py | 4 +-- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1530ed763..70f7726d9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -138,6 +138,7 @@ * [Queue On Pseudo Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_pseudo_stack.py) * Stacks * [Balanced Parentheses](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/balanced_parentheses.py) + * [Dijkstras Two Stack Algorithm](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/dijkstras_two_stack_algorithm.py) * [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py) * [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py) * [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py) @@ -587,10 +588,18 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_35/sol1.py) * Problem 36 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_36/sol1.py) + * Problem 37 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_37/sol1.py) + * Problem 39 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_39/sol1.py) * Problem 40 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_40/sol1.py) + * Problem 41 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_41/sol1.py) * Problem 42 * [Solution42](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_42/solution42.py) + * Problem 43 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_43/sol1.py) * Problem 47 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_47/sol1.py) * Problem 48 @@ -676,6 +685,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) + * [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) * [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py) * [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py) diff --git a/data_structures/stacks/dijkstras_two_stack_algorithm.py b/data_structures/stacks/dijkstras_two_stack_algorithm.py index 59d47085a..8b4668f9f 100644 --- a/data_structures/stacks/dijkstras_two_stack_algorithm.py +++ b/data_structures/stacks/dijkstras_two_stack_algorithm.py @@ -31,10 +31,10 @@ NOTE: It only works with whole numbers. """ __author__ = "Alexander Joslin" -from .stack import Stack - import operator as op +from .stack import Stack + def dijkstras_two_stack_algorithm(equation: str) -> int: """ diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index c8635bd61..9c58aa61d 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -37,7 +37,9 @@ def aliquot_sum(input_num: int) -> int: raise ValueError("Input must be an integer") if input_num <= 0: raise ValueError("Input must be positive") - return sum(divisor for divisor in range(1, input_num) if input_num % divisor == 0) + return sum( + divisor for divisor in range(1, input_num // 2 + 1) if input_num % divisor == 0 + ) if __name__ == "__main__": diff --git a/maths/average_median.py b/maths/average_median.py index ccb250d77..0257e3f76 100644 --- a/maths/average_median.py +++ b/maths/average_median.py @@ -6,6 +6,8 @@ def median(nums): 0 >>> median([4,1,3,2]) 2.5 + >>> median([2, 70, 6, 50, 20, 8, 4]) + 8 Args: nums: List of nums @@ -14,22 +16,19 @@ def median(nums): Median. """ sorted_list = sorted(nums) - med = None - if len(sorted_list) % 2 == 0: - mid_index_1 = len(sorted_list) // 2 - mid_index_2 = (len(sorted_list) // 2) - 1 - med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2) - else: - mid_index = (len(sorted_list) - 1) // 2 - med = sorted_list[mid_index] - return med + length = len(sorted_list) + mid_index = length >> 1 + return ( + (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2 + if length % 2 == 0 + else sorted_list[mid_index] + ) def main(): - print("Odd number of numbers:") - print(median([2, 4, 6, 8, 20, 50, 70])) - print("Even number of numbers:") - print(median([2, 4, 6, 8, 20, 50])) + import doctest + + doctest.testmod() if __name__ == "__main__": diff --git a/project_euler/problem_39/sol1.py b/project_euler/problem_39/sol1.py index 5c21d4bec..b0a5d5188 100644 --- a/project_euler/problem_39/sol1.py +++ b/project_euler/problem_39/sol1.py @@ -6,8 +6,8 @@ If p is the perimeter of a right angle triangle with integral length sides, For which value of p ≤ 1000, is the number of solutions maximised? """ -from typing import Dict from collections import Counter +from typing import Dict def pythagorean_triple(max_perimeter: int) -> Dict: diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index a7ce8697b..4ed09ccb8 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -1,6 +1,6 @@ +from itertools import permutations from math import sqrt from typing import List -from itertools import permutations """ We shall say that an n-digit number is pandigital if it makes use of all the digits diff --git a/strings/check_anagrams.py b/strings/check_anagrams.py index 56c76af5f..7cc1e2978 100644 --- a/strings/check_anagrams.py +++ b/strings/check_anagrams.py @@ -17,6 +17,4 @@ if __name__ == "__main__": input_B = input("Enter the second string ").strip() status = check_anagrams(input_A, input_B) - print( - f"{input_A} and {input_B} are {'' if status else 'not '}anagrams." - ) + print(f"{input_A} and {input_B} are {'' if status else 'not '}anagrams.")