Skip to content

improve maintainability index mi 55.888 -> 91.1844 #2783

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

Closed
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
6 changes: 1 addition & 5 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,6 @@
* Problem 12
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol2.py)
* Problem 120
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py)
* Problem 13
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol1.py)
* Problem 14
Expand Down Expand Up @@ -599,7 +597,7 @@
* Problem 29
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py)
* Problem 30
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/sol1.py)
* [Soln](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/soln.py)
* Problem 31
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol2.py)
Expand Down Expand Up @@ -635,8 +633,6 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_47/sol1.py)
* Problem 48
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_48/sol1.py)
* Problem 49
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_49/sol1.py)
* Problem 52
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py)
* Problem 53
Expand Down
73 changes: 29 additions & 44 deletions data_structures/linked_list/print_reverse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
# Program to print the elements of a linked list in reverse


class Node:
Expand All @@ -8,63 +8,48 @@ def __init__(self, data=None):

def __repr__(self):
"""Returns a visual representation of the node and all its following nodes."""
string_rep = []
string_rep = ""
temp = self
while temp:
string_rep.append(f"{temp.data}")
string_rep += f"<{temp.data}> ---> "
temp = temp.next
return "->".join(string_rep)
string_rep += "<END>"
return string_rep


def make_linked_list(elements_list: List):
def make_linked_list(elements_list):
"""Creates a Linked List from the elements of the given sequence
(list/tuple) and returns the head of the Linked List.
>>> make_linked_list([])
Traceback (most recent call last):
...
Exception: The Elements List is empty
>>> make_linked_list([7])
7
>>> make_linked_list(['abc'])
abc
>>> make_linked_list([7, 25])
7->25
"""
(list/tuple) and returns the head of the Linked List."""

# if elements_list is empty
if not elements_list:
raise Exception("The Elements List is empty")

current = head = Node(elements_list[0])
for i in range(1, len(elements_list)):
current.next = Node(elements_list[i])
# Set first element as Head
head = Node(elements_list[0])
current = head
# Loop through elements from position 1
for data in elements_list[1:]:
current.next = Node(data)
current = current.next
return head


def print_reverse(head_node: Node) -> None:
"""Prints the elements of the given Linked List in reverse order
>>> print_reverse([])
>>> linked_list = make_linked_list([69, 88, 73])
>>> print_reverse(linked_list)
73
88
69
"""
if head_node is not None and isinstance(head_node, Node):
def print_reverse(head_node):
"""Prints the elements of the given Linked List in reverse order"""

# If reached end of the List
if head_node is None:
return None
else:
# Recurse
print_reverse(head_node.next)
print(head_node.data)


def main():
from doctest import testmod

testmod()

linked_list = make_linked_list([14, 52, 14, 12, 43])
print("Linked List:")
print(linked_list)
print("Elements in Reverse:")
print_reverse(linked_list)


if __name__ == "__main__":
main()
list_data = [14, 52, 14, 12, 43]
linked_list = make_linked_list(list_data)
print("Linked List:")
print(linked_list)
print("Elements in Reverse:")
print_reverse(linked_list)
55 changes: 32 additions & 23 deletions maths/allocation_number.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
"""
In a multi-threaded download, this algorithm could be used to provide
each worker thread with a block of non-overlapping bytes to download.
For example:
for i in allocation_list:
requests.get(url,headers={'Range':f'bytes={i}'})
"""
from __future__ import annotations


def allocation_num(number_of_bytes: int, partitions: int) -> list[str]:
"""
Divide a number of bytes into x partitions.
:param number_of_bytes: the total of bytes.
:param partitions: the number of partition need to be allocated.
:return: list of bytes to be assigned to each worker thread

In a multi-threaded download, this algorithm could be used to provide
each worker thread with a block of non-overlapping bytes to download.
For example:
for i in allocation_list:
requests.get(url,headers={'Range':f'bytes={i}'})

parameter
------------
: param number_of_bytes
: param partitions

return
------------
: return: list of bytes to be assigned to each worker thread

Examples:
------------
>>> allocation_num(16647, 4)
['1-4161', '4162-8322', '8323-12483', '12484-16647']
>>> allocation_num(50000, 5)
['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000']
['0-4161', '4162-8322', '8323-12483', '12484-16647']
>>> allocation_num(888, 888)
Traceback (most recent call last):
...
ValueError: partitions can not >= number_of_bytes!
>>> allocation_num(888, 999)
Traceback (most recent call last):
...
ValueError: partitions can not > number_of_bytes!
ValueError: partitions can not >= number_of_bytes!
>>> allocation_num(888, -4)
Traceback (most recent call last):
...
ValueError: partitions must be a positive number!
"""
if partitions <= 0:
raise ValueError("partitions must be a positive number!")
if partitions > number_of_bytes:
raise ValueError("partitions can not > number_of_bytes!")
if partitions >= number_of_bytes:
raise ValueError("partitions can not >= number_of_bytes!")
bytes_per_partition = number_of_bytes // partitions
allocation_list = []
for i in range(partitions):
start_bytes = i * bytes_per_partition + 1
end_bytes = (
number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
)
allocation_list.append(f"{start_bytes}-{end_bytes}")
allocation_list = [f"0-{bytes_per_partition}"]
for i in range(1, partitions - 1):
length = f"{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}"
allocation_list.append(length)
allocation_list.append(
f"{(bytes_per_partition * (partitions - 1)) + 1}-" f"{number_of_bytes}"
)
return allocation_list


Expand Down
95 changes: 41 additions & 54 deletions maths/extended_euclidean_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,69 @@

Finds 2 numbers a and b such that it satisfies
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)

https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
"""

# @Author: S. Sharma <silentcat>
# @Date: 2019-02-25T12:08:53-06:00
# @Email: [email protected]
# @Last modified by: pikulet
# @Last modified time: 2020-10-02
# @Last modified by: PatOnTheBack
# @Last modified time: 2019-07-05

import sys
from typing import Tuple


def extended_euclidean_algorithm(a: int, b: int) -> Tuple[int, int]:
def extended_euclidean_algorithm(m, n):
"""
Extended Euclidean Algorithm.

Finds 2 numbers a and b such that it satisfies
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)

>>> extended_euclidean_algorithm(1, 24)
(1, 0)

>>> extended_euclidean_algorithm(8, 14)
(2, -1)

>>> extended_euclidean_algorithm(240, 46)
(-9, 47)

>>> extended_euclidean_algorithm(1, -4)
(1, 0)

>>> extended_euclidean_algorithm(-2, -4)
(-1, 0)

>>> extended_euclidean_algorithm(0, -4)
(0, -1)

>>> extended_euclidean_algorithm(2, 0)
(1, 0)

"""
# base cases
if abs(a) == 1:
return a, 0
elif abs(b) == 1:
return 0, b

old_remainder, remainder = a, b
old_coeff_a, coeff_a = 1, 0
old_coeff_b, coeff_b = 0, 1

while remainder != 0:
quotient = old_remainder // remainder
old_remainder, remainder = remainder, old_remainder - quotient * remainder
old_coeff_a, coeff_a = coeff_a, old_coeff_a - quotient * coeff_a
old_coeff_b, coeff_b = coeff_b, old_coeff_b - quotient * coeff_b

# sign correction for negative numbers
if a < 0:
old_coeff_a = -old_coeff_a
if b < 0:
old_coeff_b = -old_coeff_b

return old_coeff_a, old_coeff_b
a = 0
a_prime = 1
b = 1
b_prime = 0
q = 0
r = 0
if m > n:
c = m
d = n
else:
c = n
d = m

while True:
q = int(c / d)
r = c % d
if r == 0:
break
c = d
d = r

t = a_prime
a_prime = a
a = t - q * a

t = b_prime
b_prime = b
b = t - q * b

pair = None
if m > n:
pair = (a, b)
else:
pair = (b, a)
return pair


def main():
"""Call Extended Euclidean Algorithm."""
if len(sys.argv) < 3:
print("2 integer arguments required")
exit(1)
a = int(sys.argv[1])
b = int(sys.argv[2])
print(extended_euclidean_algorithm(a, b))
m = int(sys.argv[1])
n = int(sys.argv[2])
print(extended_euclidean_algorithm(m, n))


if __name__ == "__main__":
Expand Down
59 changes: 0 additions & 59 deletions other/doomsday.py

This file was deleted.

2 changes: 1 addition & 1 deletion project_euler/problem_01/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""


def solution(n: int = 1000) -> int:
def solution(n):
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
Expand Down
Loading