mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
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>
This commit is contained in:
parent
2eaacee7b4
commit
0591968947
10
DIRECTORY.md
10
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)
|
||||
|
|
|
@ -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:
|
||||
"""
|
||||
|
|
|
@ -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__":
|
||||
|
|
|
@ -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__":
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.")
|
||||
|
|
Loading…
Reference in New Issue
Block a user