Skip to content

Commit 345c1dd

Browse files
Merge branch 'TheAlgorithms:master' into fix-mypy-errs-5
2 parents ce31aa1 + 84b6852 commit 345c1dd

File tree

25 files changed

+786
-166
lines changed

25 files changed

+786
-166
lines changed

Diff for: .github/stale.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pulls:
4545
closeComment: >
4646
Please reopen this pull request once you commit the changes requested
4747
or make improvements on the code. If this is not the case and you need
48-
some help, feel free to seek help from our [Gitter](https://app.gitter.im/#/room/#TheAlgorithms_community:gitter.im)
48+
some help, feel free to seek help from our [Gitter](https://gitter.im/TheAlgorithms/community)
4949
or ping one of the reviewers. Thank you for your contributions!
5050
5151
issues:
@@ -59,5 +59,5 @@ issues:
5959
closeComment: >
6060
Please reopen this issue once you add more information and updates here.
6161
If this is not the case and you need some help, feel free to seek help
62-
from our [Gitter](https://app.gitter.im/#/room/#TheAlgorithms_community:gitter.im) or ping one of the
62+
from our [Gitter](https://gitter.im/TheAlgorithms/community) or ping one of the
6363
reviewers. Thank you for your contributions!

Diff for: CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Before contributing
44

5-
Welcome to [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python)! Before sending your pull requests, make sure that you __read the whole guidelines__. If you have any doubt on the contributing guide, please feel free to [state it clearly in an issue](https://github.com/TheAlgorithms/Python/issues/new) or ask the community in [Gitter](https://app.gitter.im/#/room/#TheAlgorithms_community:gitter.im).
5+
Welcome to [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python)! Before sending your pull requests, make sure that you __read the whole guidelines__. If you have any doubt on the contributing guide, please feel free to [state it clearly in an issue](https://github.com/TheAlgorithms/Python/issues/new) or ask the community in [Gitter](https://gitter.im/TheAlgorithms/community).
66

77
## Contributing
88

@@ -176,7 +176,7 @@ We want your work to be readable by others; therefore, we encourage you to note
176176

177177
- Most importantly,
178178
- __Be consistent in the use of these guidelines when submitting.__
179-
- __Join__ us on [Discord](https://discord.com/invite/c7MnfGFGa6) and [Gitter](https://app.gitter.im/#/room/#TheAlgorithms_community:gitter.im) __now!__
179+
- __Join__ us on [Discord](https://discord.com/invite/c7MnfGFGa6) and [Gitter](https://gitter.im/TheAlgorithms/community) __now!__
180180
- Happy coding!
181181

182182
Writer [@poyea](https://github.com/poyea), Jun 2019.

Diff for: DIRECTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@
317317
* [Longest Sub Array](dynamic_programming/longest_sub_array.py)
318318
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
319319
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
320+
* [Max Product Subarray](dynamic_programming/max_product_subarray.py)
320321
* [Max Sub Array](dynamic_programming/max_sub_array.py)
321322
* [Max Sum Contiguous Subsequence](dynamic_programming/max_sum_contiguous_subsequence.py)
322323
* [Min Distance Up Bottom](dynamic_programming/min_distance_up_bottom.py)
@@ -936,6 +937,8 @@
936937
* [Sol1](project_euler/problem_091/sol1.py)
937938
* Problem 092
938939
* [Sol1](project_euler/problem_092/sol1.py)
940+
* Problem 094
941+
* [Sol1](project_euler/problem_094/sol1.py)
939942
* Problem 097
940943
* [Sol1](project_euler/problem_097/sol1.py)
941944
* Problem 099
@@ -990,6 +993,8 @@
990993
* [Sol1](project_euler/problem_174/sol1.py)
991994
* Problem 180
992995
* [Sol1](project_euler/problem_180/sol1.py)
996+
* Problem 187
997+
* [Sol1](project_euler/problem_187/sol1.py)
993998
* Problem 188
994999
* [Sol1](project_euler/problem_188/sol1.py)
9951000
* Problem 191
@@ -1014,6 +1019,8 @@
10141019
* [Sol1](project_euler/problem_587/sol1.py)
10151020
* Problem 686
10161021
* [Sol1](project_euler/problem_686/sol1.py)
1022+
* Problem 800
1023+
* [Sol1](project_euler/problem_800/sol1.py)
10171024

10181025
## Quantum
10191026
* [Bb84](quantum/bb84.py)

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<a href="https://discord.gg/c7MnfGFGa6">
1717
<img src="https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square" height="20" alt="Discord chat">
1818
</a>
19-
<a href="https://app.gitter.im/#/room/#TheAlgorithms_community:gitter.im">
19+
<a href="https://gitter.im/TheAlgorithms/community">
2020
<img src="https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square" height="20" alt="Gitter chat">
2121
</a>
2222
<!-- Second row: -->
@@ -42,7 +42,7 @@ Read through our [Contribution Guidelines](CONTRIBUTING.md) before you contribut
4242

4343
## Community Channels
4444

45-
We are on [Discord](https://discord.gg/c7MnfGFGa6) and [Gitter](https://app.gitter.im/#/room/#TheAlgorithms_community:gitter.im)! Community channels are a great way for you to ask questions and get help. Please join us!
45+
We are on [Discord](https://discord.gg/c7MnfGFGa6) and [Gitter](https://gitter.im/TheAlgorithms/community)! Community channels are a great way for you to ask questions and get help. Please join us!
4646

4747
## List of Algorithms
4848

Diff for: arithmetic_analysis/lu_decomposition.py

+65-26
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,101 @@
1-
"""Lower-Upper (LU) Decomposition.
1+
"""
2+
Lower–upper (LU) decomposition factors a matrix as a product of a lower
3+
triangular matrix and an upper triangular matrix. A square matrix has an LU
4+
decomposition under the following conditions:
5+
- If the matrix is invertible, then it has an LU decomposition if and only
6+
if all of its leading principal minors are non-zero (see
7+
https://en.wikipedia.org/wiki/Minor_(linear_algebra) for an explanation of
8+
leading principal minors of a matrix).
9+
- If the matrix is singular (i.e., not invertible) and it has a rank of k
10+
(i.e., it has k linearly independent columns), then it has an LU
11+
decomposition if its first k leading principal minors are non-zero.
12+
13+
This algorithm will simply attempt to perform LU decomposition on any square
14+
matrix and raise an error if no such decomposition exists.
215
3-
Reference:
4-
- https://en.wikipedia.org/wiki/LU_decomposition
16+
Reference: https://en.wikipedia.org/wiki/LU_decomposition
517
"""
618
from __future__ import annotations
719

820
import numpy as np
9-
from numpy import float64
10-
from numpy.typing import ArrayLike
11-
1221

13-
def lower_upper_decomposition(
14-
table: ArrayLike[float64],
15-
) -> tuple[ArrayLike[float64], ArrayLike[float64]]:
16-
"""Lower-Upper (LU) Decomposition
17-
18-
Example:
1922

23+
def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
24+
"""
25+
Perform LU decomposition on a given matrix and raises an error if the matrix
26+
isn't square or if no such decomposition exists
2027
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
21-
>>> outcome = lower_upper_decomposition(matrix)
22-
>>> outcome[0]
28+
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
29+
>>> lower_mat
2330
array([[1. , 0. , 0. ],
2431
[0. , 1. , 0. ],
2532
[2.5, 8. , 1. ]])
26-
>>> outcome[1]
33+
>>> upper_mat
2734
array([[ 2. , -2. , 1. ],
2835
[ 0. , 1. , 2. ],
2936
[ 0. , 0. , -17.5]])
3037
38+
>>> matrix = np.array([[4, 3], [6, 3]])
39+
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
40+
>>> lower_mat
41+
array([[1. , 0. ],
42+
[1.5, 1. ]])
43+
>>> upper_mat
44+
array([[ 4. , 3. ],
45+
[ 0. , -1.5]])
46+
47+
# Matrix is not square
3148
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
32-
>>> lower_upper_decomposition(matrix)
49+
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
3350
Traceback (most recent call last):
3451
...
3552
ValueError: 'table' has to be of square shaped array but got a 2x3 array:
3653
[[ 2 -2 1]
3754
[ 0 1 2]]
55+
56+
# Matrix is invertible, but its first leading principal minor is 0
57+
>>> matrix = np.array([[0, 1], [1, 0]])
58+
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
59+
Traceback (most recent call last):
60+
...
61+
ArithmeticError: No LU decomposition exists
62+
63+
# Matrix is singular, but its first leading principal minor is 1
64+
>>> matrix = np.array([[1, 0], [1, 0]])
65+
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
66+
>>> lower_mat
67+
array([[1., 0.],
68+
[1., 1.]])
69+
>>> upper_mat
70+
array([[1., 0.],
71+
[0., 0.]])
72+
73+
# Matrix is singular, but its first leading principal minor is 0
74+
>>> matrix = np.array([[0, 1], [0, 1]])
75+
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
76+
Traceback (most recent call last):
77+
...
78+
ArithmeticError: No LU decomposition exists
3879
"""
39-
# Table that contains our data
40-
# Table has to be a square array so we need to check first
80+
# Ensure that table is a square array
4181
rows, columns = np.shape(table)
4282
if rows != columns:
4383
raise ValueError(
44-
f"'table' has to be of square shaped array but got a {rows}x{columns} "
45-
+ f"array:\n{table}"
84+
f"'table' has to be of square shaped array but got a "
85+
f"{rows}x{columns} array:\n{table}"
4686
)
87+
4788
lower = np.zeros((rows, columns))
4889
upper = np.zeros((rows, columns))
4990
for i in range(columns):
5091
for j in range(i):
51-
total = 0
52-
for k in range(j):
53-
total += lower[i][k] * upper[k][j]
92+
total = sum(lower[i][k] * upper[k][j] for k in range(j))
93+
if upper[j][j] == 0:
94+
raise ArithmeticError("No LU decomposition exists")
5495
lower[i][j] = (table[i][j] - total) / upper[j][j]
5596
lower[i][i] = 1
5697
for j in range(i, columns):
57-
total = 0
58-
for k in range(i):
59-
total += lower[i][k] * upper[k][j]
98+
total = sum(lower[i][k] * upper[k][j] for k in range(j))
6099
upper[i][j] = table[i][j] - total
61100
return lower, upper
62101

Diff for: computer_vision/cnn_classification.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
test_image = tf.keras.preprocessing.image.img_to_array(test_image)
9494
test_image = np.expand_dims(test_image, axis=0)
9595
result = classifier.predict(test_image)
96-
training_set.class_indices
96+
# training_set.class_indices
9797
if result[0][0] == 0:
9898
prediction = "Normal"
9999
if result[0][0] == 1:

Diff for: data_structures/linked_list/circular_linked_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __iter__(self) -> Iterator[Any]:
2424
break
2525

2626
def __len__(self) -> int:
27-
return len(tuple(iter(self)))
27+
return sum(1 for _ in self)
2828

2929
def __repr__(self):
3030
return "->".join(str(item) for item in iter(self))

Diff for: data_structures/linked_list/doubly_linked_list.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __len__(self):
5151
>>> len(linked_list) == 5
5252
True
5353
"""
54-
return len(tuple(iter(self)))
54+
return sum(1 for _ in self)
5555

5656
def insert_at_head(self, data):
5757
self.insert_at_nth(0, data)
@@ -81,7 +81,9 @@ def insert_at_nth(self, index: int, data):
8181
....
8282
IndexError: list index out of range
8383
"""
84-
if not 0 <= index <= len(self):
84+
length = len(self)
85+
86+
if not 0 <= index <= length:
8587
raise IndexError("list index out of range")
8688
new_node = Node(data)
8789
if self.head is None:
@@ -90,7 +92,7 @@ def insert_at_nth(self, index: int, data):
9092
self.head.previous = new_node
9193
new_node.next = self.head
9294
self.head = new_node
93-
elif index == len(self):
95+
elif index == length:
9496
self.tail.next = new_node
9597
new_node.previous = self.tail
9698
self.tail = new_node
@@ -131,15 +133,17 @@ def delete_at_nth(self, index: int):
131133
....
132134
IndexError: list index out of range
133135
"""
134-
if not 0 <= index <= len(self) - 1:
136+
length = len(self)
137+
138+
if not 0 <= index <= length - 1:
135139
raise IndexError("list index out of range")
136140
delete_node = self.head # default first node
137-
if len(self) == 1:
141+
if length == 1:
138142
self.head = self.tail = None
139143
elif index == 0:
140144
self.head = self.head.next
141145
self.head.previous = None
142-
elif index == len(self) - 1:
146+
elif index == length - 1:
143147
delete_node = self.tail
144148
self.tail = self.tail.previous
145149
self.tail.next = None

Diff for: data_structures/linked_list/merge_two_lists.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __len__(self) -> int:
4444
>>> len(SortedLinkedList(test_data_odd))
4545
8
4646
"""
47-
return len(tuple(iter(self)))
47+
return sum(1 for _ in self)
4848

4949
def __str__(self) -> str:
5050
"""

Diff for: data_structures/linked_list/singly_linked_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __len__(self) -> int:
7272
>>> len(linked_list)
7373
0
7474
"""
75-
return len(tuple(iter(self)))
75+
return sum(1 for _ in self)
7676

7777
def __repr__(self) -> str:
7878
"""

0 commit comments

Comments
 (0)