Skip to content

Enable ruff PLR5501 rule #11332

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 2 commits into from
Mar 28, 2024
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
5 changes: 2 additions & 3 deletions backtracking/crossword_puzzle_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ def is_valid(
if vertical:
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
return False
else:
if col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
return False
elif col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
return False
return True


Expand Down
5 changes: 2 additions & 3 deletions cellular_automata/game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
state = True
elif alive > 3:
state = False
else:
if alive == 3:
state = True
elif alive == 3:
state = True

return state

Expand Down
21 changes: 10 additions & 11 deletions ciphers/decrypt_caesar_with_chi_squared.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,19 @@ def decrypt_caesar_with_chi_squared(

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

# Get the excepcted amount of times the letter should appear based
# on letter frequencies
expected = frequencies[letter] * occurrences
# Get the excepcted amount of times the letter should appear based
# on letter frequencies
expected = frequencies[letter] * occurrences

# Complete the chi squared statistic formula
chi_letter_value = ((occurrences - expected) ** 2) / expected
# Complete the chi squared statistic formula
chi_letter_value = ((occurrences - expected) ** 2) / expected

# Add the margin of error to the total chi squared statistic
chi_squared_statistic += chi_letter_value
# Add the margin of error to the total chi squared statistic
chi_squared_statistic += chi_letter_value

# Add the data to the chi_squared_statistic_values dictionary
chi_squared_statistic_values[shift] = (
Expand Down
10 changes: 5 additions & 5 deletions data_structures/binary_tree/avl_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
return root
else:
root.set_left(del_node(left_child, data))
else: # root.get_data() < data
if right_child is None:
return root
else:
root.set_right(del_node(right_child, data))
# root.get_data() < data
elif right_child is None:
return root
else:
root.set_right(del_node(right_child, data))

if get_height(right_child) - get_height(left_child) == 2:
assert right_child is not None
Expand Down
9 changes: 4 additions & 5 deletions data_structures/binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ def __insert(self, value) -> None:
break
else:
parent_node = parent_node.left
elif parent_node.right is None:
parent_node.right = new_node
break
else:
if parent_node.right is None:
parent_node.right = new_node
break
else:
parent_node = parent_node.right
parent_node = parent_node.right
new_node.parent = parent_node

def insert(self, *values) -> Self:
Expand Down
22 changes: 10 additions & 12 deletions data_structures/binary_tree/binary_search_tree_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ def put(self, label: int) -> None:
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
if node is None:
node = Node(label, parent)
elif label < node.label:
node.left = self._put(node.left, label, node)
elif label > node.label:
node.right = self._put(node.right, label, node)
else:
if label < node.label:
node.left = self._put(node.left, label, node)
elif label > node.label:
node.right = self._put(node.right, label, node)
else:
msg = f"Node with label {label} already exists"
raise ValueError(msg)
msg = f"Node with label {label} already exists"
raise ValueError(msg)

return node

Expand All @@ -106,11 +105,10 @@ def _search(self, node: Node | None, label: int) -> Node:
if node is None:
msg = f"Node with label {label} does not exist"
raise ValueError(msg)
else:
if label < node.label:
node = self._search(node.left, label)
elif label > node.label:
node = self._search(node.right, label)
elif label < node.label:
node = self._search(node.left, label)
elif label > node.label:
node = self._search(node.right, label)

return node

Expand Down
66 changes: 31 additions & 35 deletions data_structures/binary_tree/red_black_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ def insert(self, label: int) -> RedBlackTree:
else:
self.left = RedBlackTree(label, 1, self)
self.left._insert_repair()
elif self.right:
self.right.insert(label)
else:
if self.right:
self.right.insert(label)
else:
self.right = RedBlackTree(label, 1, self)
self.right._insert_repair()
self.right = RedBlackTree(label, 1, self)
self.right._insert_repair()
return self.parent or self

def _insert_repair(self) -> None:
Expand Down Expand Up @@ -178,36 +177,34 @@ def remove(self, label: int) -> RedBlackTree: # noqa: PLR0912
self.parent.left = None
else:
self.parent.right = None
else:
# The node is black
if child is None:
# This node and its child are black
if self.parent is None:
# The tree is now empty
return RedBlackTree(None)
else:
self._remove_repair()
if self.is_left():
self.parent.left = None
else:
self.parent.right = None
self.parent = None
# The node is black
elif child is None:
# This node and its child are black
if self.parent is None:
# The tree is now empty
return RedBlackTree(None)
else:
# This node is black and its child is red
# Move the child node here and make it black
self.label = child.label
self.left = child.left
self.right = child.right
if self.left:
self.left.parent = self
if self.right:
self.right.parent = self
self._remove_repair()
if self.is_left():
self.parent.left = None
else:
self.parent.right = None
self.parent = None
else:
# This node is black and its child is red
# Move the child node here and make it black
self.label = child.label
self.left = child.left
self.right = child.right
if self.left:
self.left.parent = self
if self.right:
self.right.parent = self
elif self.label is not None and self.label > label:
if self.left:
self.left.remove(label)
else:
if self.right:
self.right.remove(label)
elif self.right:
self.right.remove(label)
return self.parent or self

def _remove_repair(self) -> None:
Expand Down Expand Up @@ -369,11 +366,10 @@ def search(self, label: int) -> RedBlackTree | None:
return None
else:
return self.right.search(label)
elif self.left is None:
return None
else:
if self.left is None:
return None
else:
return self.left.search(label)
return self.left.search(label)

def floor(self, label: int) -> int | None:
"""Returns the largest element in this tree which is at most label.
Expand Down
29 changes: 14 additions & 15 deletions data_structures/binary_tree/treap.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
return None, None
elif root.value is None:
return None, None
elif value < root.value:
"""
Right tree's root will be current node.
Now we split(with the same value) current node's left son
Left tree: left part of that split
Right tree's left son: right part of that split
"""
left, root.left = split(root.left, value)
return left, root
else:
if value < root.value:
"""
Right tree's root will be current node.
Now we split(with the same value) current node's left son
Left tree: left part of that split
Right tree's left son: right part of that split
"""
left, root.left = split(root.left, value)
return left, root
else:
"""
Just symmetric to previous case
"""
root.right, right = split(root.right, value)
return root, right
"""
Just symmetric to previous case
"""
root.right, right = split(root.right, value)
return root, right


def merge(left: Node | None, right: Node | None) -> Node | None:
Expand Down
7 changes: 3 additions & 4 deletions data_structures/heap/max_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ def __swap_down(self, i: int) -> None:
while self.__size >= 2 * i:
if 2 * i + 1 > self.__size:
bigger_child = 2 * i
elif self.__heap[2 * i] > self.__heap[2 * i + 1]:
bigger_child = 2 * i
else:
if self.__heap[2 * i] > self.__heap[2 * i + 1]:
bigger_child = 2 * i
else:
bigger_child = 2 * i + 1
bigger_child = 2 * i + 1
temporary = self.__heap[i]
if self.__heap[i] < self.__heap[bigger_child]:
self.__heap[i] = self.__heap[bigger_child]
Expand Down
13 changes: 6 additions & 7 deletions data_structures/stacks/infix_to_prefix_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ def infix_2_postfix(infix: str) -> str:
while stack[-1] != "(":
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
stack.pop()
else:
if len(stack) == 0:
stack.append(x) # If stack is empty, push x to stack
else: # while priority of x is not > priority of element in the stack
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
post_fix.append(stack.pop()) # pop stack & add to Postfix
stack.append(x) # push x to stack
elif len(stack) == 0:
stack.append(x) # If stack is empty, push x to stack
else: # while priority of x is not > priority of element in the stack
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
post_fix.append(stack.pop()) # pop stack & add to Postfix
stack.append(x) # push x to stack

print(
x.center(8),
Expand Down
45 changes: 22 additions & 23 deletions data_structures/trie/radix_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,31 +153,30 @@ def delete(self, word: str) -> bool:
# We have word remaining so we check the next node
elif remaining_word != "":
return incoming_node.delete(remaining_word)
# If it is not a leaf, we don't have to delete
elif not incoming_node.is_leaf:
return False
else:
# If it is not a leaf, we don't have to delete
if not incoming_node.is_leaf:
return False
# We delete the nodes if no edges go from it
if len(incoming_node.nodes) == 0:
del self.nodes[word[0]]
# We merge the current node with its only child
if len(self.nodes) == 1 and not self.is_leaf:
merging_node = next(iter(self.nodes.values()))
self.is_leaf = merging_node.is_leaf
self.prefix += merging_node.prefix
self.nodes = merging_node.nodes
# If there is more than 1 edge, we just mark it as non-leaf
elif len(incoming_node.nodes) > 1:
incoming_node.is_leaf = False
# If there is 1 edge, we merge it with its child
else:
# We delete the nodes if no edges go from it
if len(incoming_node.nodes) == 0:
del self.nodes[word[0]]
# We merge the current node with its only child
if len(self.nodes) == 1 and not self.is_leaf:
merging_node = next(iter(self.nodes.values()))
self.is_leaf = merging_node.is_leaf
self.prefix += merging_node.prefix
self.nodes = merging_node.nodes
# If there is more than 1 edge, we just mark it as non-leaf
elif len(incoming_node.nodes) > 1:
incoming_node.is_leaf = False
# If there is 1 edge, we merge it with its child
else:
merging_node = next(iter(incoming_node.nodes.values()))
incoming_node.is_leaf = merging_node.is_leaf
incoming_node.prefix += merging_node.prefix
incoming_node.nodes = merging_node.nodes

return True
merging_node = next(iter(incoming_node.nodes.values()))
incoming_node.is_leaf = merging_node.is_leaf
incoming_node.prefix += merging_node.prefix
incoming_node.nodes = merging_node.nodes

return True

def print_tree(self, height: int = 0) -> None:
"""Print the tree
Expand Down
15 changes: 7 additions & 8 deletions divide_and_conquer/convex_hull.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,13 @@ def convex_hull_bf(points: list[Point]) -> list[Point]:
points_left_of_ij = True
elif det_k < 0:
points_right_of_ij = True
else:
# point[i], point[j], point[k] all lie on a straight line
# if point[k] is to the left of point[i] or it's to the
# right of point[j], then point[i], point[j] cannot be
# part of the convex hull of A
if points[k] < points[i] or points[k] > points[j]:
ij_part_of_convex_hull = False
break
# point[i], point[j], point[k] all lie on a straight line
# if point[k] is to the left of point[i] or it's to the
# right of point[j], then point[i], point[j] cannot be
# part of the convex hull of A
elif points[k] < points[i] or points[k] > points[j]:
ij_part_of_convex_hull = False
break

if points_left_of_ij and points_right_of_ij:
ij_part_of_convex_hull = False
Expand Down
Loading