Skip to content

Commit 4900539

Browse files
committed
Merge branch 'master' into enable-ruff-ICN001-rule
2 parents c901852 + efb7463 commit 4900539

35 files changed

+236
-257
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.3.3
19+
rev: v0.3.4
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format

DIRECTORY.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@
351351
* [Longest Common Subsequence](dynamic_programming/longest_common_subsequence.py)
352352
* [Longest Common Substring](dynamic_programming/longest_common_substring.py)
353353
* [Longest Increasing Subsequence](dynamic_programming/longest_increasing_subsequence.py)
354-
* [Longest Increasing Subsequence O(Nlogn)](dynamic_programming/longest_increasing_subsequence_o(nlogn).py)
354+
* [Longest Increasing Subsequence O Nlogn](dynamic_programming/longest_increasing_subsequence_o_nlogn.py)
355355
* [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py)
356356
* [Matrix Chain Multiplication](dynamic_programming/matrix_chain_multiplication.py)
357357
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
@@ -465,7 +465,7 @@
465465
* [Dijkstra Alternate](graphs/dijkstra_alternate.py)
466466
* [Dijkstra Binary Grid](graphs/dijkstra_binary_grid.py)
467467
* [Dinic](graphs/dinic.py)
468-
* [Directed And Undirected (Weighted) Graph](graphs/directed_and_undirected_(weighted)_graph.py)
468+
* [Directed And Undirected Weighted Graph](graphs/directed_and_undirected_weighted_graph.py)
469469
* [Edmonds Karp Multiple Source And Sink](graphs/edmonds_karp_multiple_source_and_sink.py)
470470
* [Eulerian Path And Circuit For Undirected Graph](graphs/eulerian_path_and_circuit_for_undirected_graph.py)
471471
* [Even Tree](graphs/even_tree.py)
@@ -792,7 +792,6 @@
792792
* [Minimum Cut](networking_flow/minimum_cut.py)
793793

794794
## Neural Network
795-
* [2 Hidden Layers Neural Network](neural_network/2_hidden_layers_neural_network.py)
796795
* Activation Functions
797796
* [Binary Step](neural_network/activation_functions/binary_step.py)
798797
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
@@ -809,6 +808,7 @@
809808
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
810809
* [Input Data](neural_network/input_data.py)
811810
* [Simple Neural Network](neural_network/simple_neural_network.py)
811+
* [Two Hidden Layers Neural Network](neural_network/two_hidden_layers_neural_network.py)
812812

813813
## Other
814814
* [Activity Selection](other/activity_selection.py)

backtracking/crossword_puzzle_solver.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def is_valid(
2828
if vertical:
2929
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
3030
return False
31-
else:
32-
if col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
33-
return False
31+
elif col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
32+
return False
3433
return True
3534

3635

cellular_automata/game_of_life.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
101101
state = True
102102
elif alive > 3:
103103
state = False
104-
else:
105-
if alive == 3:
106-
state = True
104+
elif alive == 3:
105+
state = True
107106

108107
return state
109108

ciphers/decrypt_caesar_with_chi_squared.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,19 @@ def decrypt_caesar_with_chi_squared(
206206

207207
# Add the margin of error to the total chi squared statistic
208208
chi_squared_statistic += chi_letter_value
209-
else:
210-
if letter.lower() in frequencies:
211-
# Get the amount of times the letter occurs in the message
212-
occurrences = decrypted_with_shift.count(letter)
209+
elif letter.lower() in frequencies:
210+
# Get the amount of times the letter occurs in the message
211+
occurrences = decrypted_with_shift.count(letter)
213212

214-
# Get the excepcted amount of times the letter should appear based
215-
# on letter frequencies
216-
expected = frequencies[letter] * occurrences
213+
# Get the excepcted amount of times the letter should appear based
214+
# on letter frequencies
215+
expected = frequencies[letter] * occurrences
217216

218-
# Complete the chi squared statistic formula
219-
chi_letter_value = ((occurrences - expected) ** 2) / expected
217+
# Complete the chi squared statistic formula
218+
chi_letter_value = ((occurrences - expected) ** 2) / expected
220219

221-
# Add the margin of error to the total chi squared statistic
222-
chi_squared_statistic += chi_letter_value
220+
# Add the margin of error to the total chi squared statistic
221+
chi_squared_statistic += chi_letter_value
223222

224223
# Add the data to the chi_squared_statistic_values dictionary
225224
chi_squared_statistic_values[shift] = (

data_structures/binary_tree/avl_tree.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
215215
return root
216216
else:
217217
root.set_left(del_node(left_child, data))
218-
else: # root.get_data() < data
219-
if right_child is None:
220-
return root
221-
else:
222-
root.set_right(del_node(right_child, data))
218+
# root.get_data() < data
219+
elif right_child is None:
220+
return root
221+
else:
222+
root.set_right(del_node(right_child, data))
223223

224224
if get_height(right_child) - get_height(left_child) == 2:
225225
assert right_child is not None

data_structures/binary_tree/binary_search_tree.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,11 @@ def __insert(self, value) -> None:
185185
break
186186
else:
187187
parent_node = parent_node.left
188+
elif parent_node.right is None:
189+
parent_node.right = new_node
190+
break
188191
else:
189-
if parent_node.right is None:
190-
parent_node.right = new_node
191-
break
192-
else:
193-
parent_node = parent_node.right
192+
parent_node = parent_node.right
194193
new_node.parent = parent_node
195194

196195
def insert(self, *values) -> Self:

data_structures/binary_tree/binary_search_tree_recursive.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@ def put(self, label: int) -> None:
7474
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
7575
if node is None:
7676
node = Node(label, parent)
77+
elif label < node.label:
78+
node.left = self._put(node.left, label, node)
79+
elif label > node.label:
80+
node.right = self._put(node.right, label, node)
7781
else:
78-
if label < node.label:
79-
node.left = self._put(node.left, label, node)
80-
elif label > node.label:
81-
node.right = self._put(node.right, label, node)
82-
else:
83-
msg = f"Node with label {label} already exists"
84-
raise ValueError(msg)
82+
msg = f"Node with label {label} already exists"
83+
raise ValueError(msg)
8584

8685
return node
8786

@@ -106,11 +105,10 @@ def _search(self, node: Node | None, label: int) -> Node:
106105
if node is None:
107106
msg = f"Node with label {label} does not exist"
108107
raise ValueError(msg)
109-
else:
110-
if label < node.label:
111-
node = self._search(node.left, label)
112-
elif label > node.label:
113-
node = self._search(node.right, label)
108+
elif label < node.label:
109+
node = self._search(node.left, label)
110+
elif label > node.label:
111+
node = self._search(node.right, label)
114112

115113
return node
116114

data_structures/binary_tree/binary_tree_traversals.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ def level_order(root: Node | None) -> Generator[int, None, None]:
9797
"""
9898
Returns a list of nodes value from a whole binary tree in Level Order Traverse.
9999
Level Order traverse: Visit nodes of the tree level-by-level.
100+
>>> list(level_order(make_tree()))
101+
[1, 2, 3, 4, 5]
100102
"""
101103

102104
if root is None:
@@ -120,6 +122,10 @@ def get_nodes_from_left_to_right(
120122
"""
121123
Returns a list of nodes value from a particular level:
122124
Left to right direction of the binary tree.
125+
>>> list(get_nodes_from_left_to_right(make_tree(), 1))
126+
[1]
127+
>>> list(get_nodes_from_left_to_right(make_tree(), 2))
128+
[2, 3]
123129
"""
124130

125131
def populate_output(root: Node | None, level: int) -> Generator[int, None, None]:
@@ -140,10 +146,14 @@ def get_nodes_from_right_to_left(
140146
"""
141147
Returns a list of nodes value from a particular level:
142148
Right to left direction of the binary tree.
149+
>>> list(get_nodes_from_right_to_left(make_tree(), 1))
150+
[1]
151+
>>> list(get_nodes_from_right_to_left(make_tree(), 2))
152+
[3, 2]
143153
"""
144154

145155
def populate_output(root: Node | None, level: int) -> Generator[int, None, None]:
146-
if root is None:
156+
if not root:
147157
return
148158
if level == 1:
149159
yield root.data
@@ -158,6 +168,8 @@ def zigzag(root: Node | None) -> Generator[int, None, None]:
158168
"""
159169
ZigZag traverse:
160170
Returns a list of nodes value from left to right and right to left, alternatively.
171+
>>> list(zigzag(make_tree()))
172+
[1, 3, 2, 4, 5]
161173
"""
162174
if root is None:
163175
return

data_structures/binary_tree/red_black_tree.py

+31-35
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,11 @@ def insert(self, label: int) -> RedBlackTree:
107107
else:
108108
self.left = RedBlackTree(label, 1, self)
109109
self.left._insert_repair()
110+
elif self.right:
111+
self.right.insert(label)
110112
else:
111-
if self.right:
112-
self.right.insert(label)
113-
else:
114-
self.right = RedBlackTree(label, 1, self)
115-
self.right._insert_repair()
113+
self.right = RedBlackTree(label, 1, self)
114+
self.right._insert_repair()
116115
return self.parent or self
117116

118117
def _insert_repair(self) -> None:
@@ -178,36 +177,34 @@ def remove(self, label: int) -> RedBlackTree: # noqa: PLR0912
178177
self.parent.left = None
179178
else:
180179
self.parent.right = None
181-
else:
182-
# The node is black
183-
if child is None:
184-
# This node and its child are black
185-
if self.parent is None:
186-
# The tree is now empty
187-
return RedBlackTree(None)
188-
else:
189-
self._remove_repair()
190-
if self.is_left():
191-
self.parent.left = None
192-
else:
193-
self.parent.right = None
194-
self.parent = None
180+
# The node is black
181+
elif child is None:
182+
# This node and its child are black
183+
if self.parent is None:
184+
# The tree is now empty
185+
return RedBlackTree(None)
195186
else:
196-
# This node is black and its child is red
197-
# Move the child node here and make it black
198-
self.label = child.label
199-
self.left = child.left
200-
self.right = child.right
201-
if self.left:
202-
self.left.parent = self
203-
if self.right:
204-
self.right.parent = self
187+
self._remove_repair()
188+
if self.is_left():
189+
self.parent.left = None
190+
else:
191+
self.parent.right = None
192+
self.parent = None
193+
else:
194+
# This node is black and its child is red
195+
# Move the child node here and make it black
196+
self.label = child.label
197+
self.left = child.left
198+
self.right = child.right
199+
if self.left:
200+
self.left.parent = self
201+
if self.right:
202+
self.right.parent = self
205203
elif self.label is not None and self.label > label:
206204
if self.left:
207205
self.left.remove(label)
208-
else:
209-
if self.right:
210-
self.right.remove(label)
206+
elif self.right:
207+
self.right.remove(label)
211208
return self.parent or self
212209

213210
def _remove_repair(self) -> None:
@@ -369,11 +366,10 @@ def search(self, label: int) -> RedBlackTree | None:
369366
return None
370367
else:
371368
return self.right.search(label)
369+
elif self.left is None:
370+
return None
372371
else:
373-
if self.left is None:
374-
return None
375-
else:
376-
return self.left.search(label)
372+
return self.left.search(label)
377373

378374
def floor(self, label: int) -> int | None:
379375
"""Returns the largest element in this tree which is at most label.

data_structures/binary_tree/treap.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
4343
return None, None
4444
elif root.value is None:
4545
return None, None
46+
elif value < root.value:
47+
"""
48+
Right tree's root will be current node.
49+
Now we split(with the same value) current node's left son
50+
Left tree: left part of that split
51+
Right tree's left son: right part of that split
52+
"""
53+
left, root.left = split(root.left, value)
54+
return left, root
4655
else:
47-
if value < root.value:
48-
"""
49-
Right tree's root will be current node.
50-
Now we split(with the same value) current node's left son
51-
Left tree: left part of that split
52-
Right tree's left son: right part of that split
53-
"""
54-
left, root.left = split(root.left, value)
55-
return left, root
56-
else:
57-
"""
58-
Just symmetric to previous case
59-
"""
60-
root.right, right = split(root.right, value)
61-
return root, right
56+
"""
57+
Just symmetric to previous case
58+
"""
59+
root.right, right = split(root.right, value)
60+
return root, right
6261

6362

6463
def merge(left: Node | None, right: Node | None) -> Node | None:

data_structures/heap/max_heap.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ def __swap_down(self, i: int) -> None:
4040
while self.__size >= 2 * i:
4141
if 2 * i + 1 > self.__size:
4242
bigger_child = 2 * i
43+
elif self.__heap[2 * i] > self.__heap[2 * i + 1]:
44+
bigger_child = 2 * i
4345
else:
44-
if self.__heap[2 * i] > self.__heap[2 * i + 1]:
45-
bigger_child = 2 * i
46-
else:
47-
bigger_child = 2 * i + 1
46+
bigger_child = 2 * i + 1
4847
temporary = self.__heap[i]
4948
if self.__heap[i] < self.__heap[bigger_child]:
5049
self.__heap[i] = self.__heap[bigger_child]

data_structures/stacks/infix_to_prefix_conversion.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ def infix_2_postfix(infix: str) -> str:
9595
while stack[-1] != "(":
9696
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
9797
stack.pop()
98-
else:
99-
if len(stack) == 0:
100-
stack.append(x) # If stack is empty, push x to stack
101-
else: # while priority of x is not > priority of element in the stack
102-
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
103-
post_fix.append(stack.pop()) # pop stack & add to Postfix
104-
stack.append(x) # push x to stack
98+
elif len(stack) == 0:
99+
stack.append(x) # If stack is empty, push x to stack
100+
else: # while priority of x is not > priority of element in the stack
101+
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
102+
post_fix.append(stack.pop()) # pop stack & add to Postfix
103+
stack.append(x) # push x to stack
105104

106105
print(
107106
x.center(8),

0 commit comments

Comments
 (0)