Skip to content

Optimization and fix bug #2342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions data_structures/stacks/dijkstras_two_stack_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
"""
__author__ = "Alexander Joslin"

from .stack import Stack

import operator as op

from .stack import Stack


def dijkstras_two_stack_algorithm(equation: str) -> int:
"""
Expand Down
4 changes: 3 additions & 1 deletion maths/aliquot_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
25 changes: 12 additions & 13 deletions maths/average_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_39/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
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:
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_41/sol1.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions strings/check_anagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ def check_anagrams(a: str, b: str) -> bool:
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.")