mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Enable ruff PLR5501 rule (#11332)
* Enable ruff PLR5501 rule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
19fd435042
commit
516a3028d1
|
@ -28,9 +28,8 @@ def is_valid(
|
||||||
if vertical:
|
if vertical:
|
||||||
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
|
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
|
||||||
return False
|
return False
|
||||||
else:
|
elif col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
|
||||||
if col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
|
return False
|
||||||
return False
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -101,9 +101,8 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
|
||||||
state = True
|
state = True
|
||||||
elif alive > 3:
|
elif alive > 3:
|
||||||
state = False
|
state = False
|
||||||
else:
|
elif alive == 3:
|
||||||
if alive == 3:
|
state = True
|
||||||
state = True
|
|
||||||
|
|
||||||
return state
|
return state
|
||||||
|
|
||||||
|
|
|
@ -206,20 +206,19 @@ def decrypt_caesar_with_chi_squared(
|
||||||
|
|
||||||
# Add the margin of error to the total chi squared statistic
|
# Add the margin of error to the total chi squared statistic
|
||||||
chi_squared_statistic += chi_letter_value
|
chi_squared_statistic += chi_letter_value
|
||||||
else:
|
elif letter.lower() in frequencies:
|
||||||
if letter.lower() in frequencies:
|
# Get the amount of times the letter occurs in the message
|
||||||
# Get the amount of times the letter occurs in the message
|
occurrences = decrypted_with_shift.count(letter)
|
||||||
occurrences = decrypted_with_shift.count(letter)
|
|
||||||
|
|
||||||
# Get the excepcted amount of times the letter should appear based
|
# Get the excepcted amount of times the letter should appear based
|
||||||
# on letter frequencies
|
# on letter frequencies
|
||||||
expected = frequencies[letter] * occurrences
|
expected = frequencies[letter] * occurrences
|
||||||
|
|
||||||
# Complete the chi squared statistic formula
|
# Complete the chi squared statistic formula
|
||||||
chi_letter_value = ((occurrences - expected) ** 2) / expected
|
chi_letter_value = ((occurrences - expected) ** 2) / expected
|
||||||
|
|
||||||
# Add the margin of error to the total chi squared statistic
|
# Add the margin of error to the total chi squared statistic
|
||||||
chi_squared_statistic += chi_letter_value
|
chi_squared_statistic += chi_letter_value
|
||||||
|
|
||||||
# Add the data to the chi_squared_statistic_values dictionary
|
# Add the data to the chi_squared_statistic_values dictionary
|
||||||
chi_squared_statistic_values[shift] = (
|
chi_squared_statistic_values[shift] = (
|
||||||
|
|
|
@ -215,11 +215,11 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
|
||||||
return root
|
return root
|
||||||
else:
|
else:
|
||||||
root.set_left(del_node(left_child, data))
|
root.set_left(del_node(left_child, data))
|
||||||
else: # root.get_data() < data
|
# root.get_data() < data
|
||||||
if right_child is None:
|
elif right_child is None:
|
||||||
return root
|
return root
|
||||||
else:
|
else:
|
||||||
root.set_right(del_node(right_child, data))
|
root.set_right(del_node(right_child, data))
|
||||||
|
|
||||||
if get_height(right_child) - get_height(left_child) == 2:
|
if get_height(right_child) - get_height(left_child) == 2:
|
||||||
assert right_child is not None
|
assert right_child is not None
|
||||||
|
|
|
@ -185,12 +185,11 @@ class BinarySearchTree:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
parent_node = parent_node.left
|
parent_node = parent_node.left
|
||||||
|
elif parent_node.right is None:
|
||||||
|
parent_node.right = new_node
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
if parent_node.right is None:
|
parent_node = parent_node.right
|
||||||
parent_node.right = new_node
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
parent_node = parent_node.right
|
|
||||||
new_node.parent = parent_node
|
new_node.parent = parent_node
|
||||||
|
|
||||||
def insert(self, *values) -> Self:
|
def insert(self, *values) -> Self:
|
||||||
|
|
|
@ -74,14 +74,13 @@ class BinarySearchTree:
|
||||||
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
|
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
|
||||||
if node is None:
|
if node is None:
|
||||||
node = Node(label, parent)
|
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:
|
else:
|
||||||
if label < node.label:
|
msg = f"Node with label {label} already exists"
|
||||||
node.left = self._put(node.left, label, node)
|
raise ValueError(msg)
|
||||||
elif label > node.label:
|
|
||||||
node.right = self._put(node.right, label, node)
|
|
||||||
else:
|
|
||||||
msg = f"Node with label {label} already exists"
|
|
||||||
raise ValueError(msg)
|
|
||||||
|
|
||||||
return node
|
return node
|
||||||
|
|
||||||
|
@ -106,11 +105,10 @@ class BinarySearchTree:
|
||||||
if node is None:
|
if node is None:
|
||||||
msg = f"Node with label {label} does not exist"
|
msg = f"Node with label {label} does not exist"
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
else:
|
elif label < node.label:
|
||||||
if label < node.label:
|
node = self._search(node.left, label)
|
||||||
node = self._search(node.left, label)
|
elif label > node.label:
|
||||||
elif label > node.label:
|
node = self._search(node.right, label)
|
||||||
node = self._search(node.right, label)
|
|
||||||
|
|
||||||
return node
|
return node
|
||||||
|
|
||||||
|
|
|
@ -107,12 +107,11 @@ class RedBlackTree:
|
||||||
else:
|
else:
|
||||||
self.left = RedBlackTree(label, 1, self)
|
self.left = RedBlackTree(label, 1, self)
|
||||||
self.left._insert_repair()
|
self.left._insert_repair()
|
||||||
|
elif self.right:
|
||||||
|
self.right.insert(label)
|
||||||
else:
|
else:
|
||||||
if self.right:
|
self.right = RedBlackTree(label, 1, self)
|
||||||
self.right.insert(label)
|
self.right._insert_repair()
|
||||||
else:
|
|
||||||
self.right = RedBlackTree(label, 1, self)
|
|
||||||
self.right._insert_repair()
|
|
||||||
return self.parent or self
|
return self.parent or self
|
||||||
|
|
||||||
def _insert_repair(self) -> None:
|
def _insert_repair(self) -> None:
|
||||||
|
@ -178,36 +177,34 @@ class RedBlackTree:
|
||||||
self.parent.left = None
|
self.parent.left = None
|
||||||
else:
|
else:
|
||||||
self.parent.right = None
|
self.parent.right = None
|
||||||
else:
|
# The node is black
|
||||||
# The node is black
|
elif child is None:
|
||||||
if child is None:
|
# This node and its child are black
|
||||||
# This node and its child are black
|
if self.parent is None:
|
||||||
if self.parent is None:
|
# The tree is now empty
|
||||||
# The tree is now empty
|
return RedBlackTree(None)
|
||||||
return RedBlackTree(None)
|
|
||||||
else:
|
|
||||||
self._remove_repair()
|
|
||||||
if self.is_left():
|
|
||||||
self.parent.left = None
|
|
||||||
else:
|
|
||||||
self.parent.right = None
|
|
||||||
self.parent = None
|
|
||||||
else:
|
else:
|
||||||
# This node is black and its child is red
|
self._remove_repair()
|
||||||
# Move the child node here and make it black
|
if self.is_left():
|
||||||
self.label = child.label
|
self.parent.left = None
|
||||||
self.left = child.left
|
else:
|
||||||
self.right = child.right
|
self.parent.right = None
|
||||||
if self.left:
|
self.parent = None
|
||||||
self.left.parent = self
|
else:
|
||||||
if self.right:
|
# This node is black and its child is red
|
||||||
self.right.parent = self
|
# 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:
|
elif self.label is not None and self.label > label:
|
||||||
if self.left:
|
if self.left:
|
||||||
self.left.remove(label)
|
self.left.remove(label)
|
||||||
else:
|
elif self.right:
|
||||||
if self.right:
|
self.right.remove(label)
|
||||||
self.right.remove(label)
|
|
||||||
return self.parent or self
|
return self.parent or self
|
||||||
|
|
||||||
def _remove_repair(self) -> None:
|
def _remove_repair(self) -> None:
|
||||||
|
@ -369,11 +366,10 @@ class RedBlackTree:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return self.right.search(label)
|
return self.right.search(label)
|
||||||
|
elif self.left is None:
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
if self.left is None:
|
return self.left.search(label)
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return self.left.search(label)
|
|
||||||
|
|
||||||
def floor(self, label: int) -> int | None:
|
def floor(self, label: int) -> int | None:
|
||||||
"""Returns the largest element in this tree which is at most label.
|
"""Returns the largest element in this tree which is at most label.
|
||||||
|
|
|
@ -43,22 +43,21 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
|
||||||
return None, None
|
return None, None
|
||||||
elif root.value is None:
|
elif root.value is None:
|
||||||
return None, 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:
|
else:
|
||||||
if value < root.value:
|
"""
|
||||||
"""
|
Just symmetric to previous case
|
||||||
Right tree's root will be current node.
|
"""
|
||||||
Now we split(with the same value) current node's left son
|
root.right, right = split(root.right, value)
|
||||||
Left tree: left part of that split
|
return root, right
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def merge(left: Node | None, right: Node | None) -> Node | None:
|
def merge(left: Node | None, right: Node | None) -> Node | None:
|
||||||
|
|
|
@ -40,11 +40,10 @@ class BinaryHeap:
|
||||||
while self.__size >= 2 * i:
|
while self.__size >= 2 * i:
|
||||||
if 2 * i + 1 > self.__size:
|
if 2 * i + 1 > self.__size:
|
||||||
bigger_child = 2 * i
|
bigger_child = 2 * i
|
||||||
|
elif self.__heap[2 * i] > self.__heap[2 * i + 1]:
|
||||||
|
bigger_child = 2 * i
|
||||||
else:
|
else:
|
||||||
if self.__heap[2 * i] > self.__heap[2 * i + 1]:
|
bigger_child = 2 * i + 1
|
||||||
bigger_child = 2 * i
|
|
||||||
else:
|
|
||||||
bigger_child = 2 * i + 1
|
|
||||||
temporary = self.__heap[i]
|
temporary = self.__heap[i]
|
||||||
if self.__heap[i] < self.__heap[bigger_child]:
|
if self.__heap[i] < self.__heap[bigger_child]:
|
||||||
self.__heap[i] = self.__heap[bigger_child]
|
self.__heap[i] = self.__heap[bigger_child]
|
||||||
|
|
|
@ -95,13 +95,12 @@ def infix_2_postfix(infix: str) -> str:
|
||||||
while stack[-1] != "(":
|
while stack[-1] != "(":
|
||||||
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
|
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
|
||||||
stack.pop()
|
stack.pop()
|
||||||
else:
|
elif len(stack) == 0:
|
||||||
if len(stack) == 0:
|
stack.append(x) # If stack is empty, push x to stack
|
||||||
stack.append(x) # If stack is empty, push x to stack
|
else: # while priority of x is not > priority of element in the 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]]:
|
||||||
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
|
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
||||||
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
stack.append(x) # push x to stack
|
||||||
stack.append(x) # push x to stack
|
|
||||||
|
|
||||||
print(
|
print(
|
||||||
x.center(8),
|
x.center(8),
|
||||||
|
|
|
@ -153,31 +153,30 @@ class RadixNode:
|
||||||
# We have word remaining so we check the next node
|
# We have word remaining so we check the next node
|
||||||
elif remaining_word != "":
|
elif remaining_word != "":
|
||||||
return incoming_node.delete(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:
|
else:
|
||||||
# If it is not a leaf, we don't have to delete
|
# We delete the nodes if no edges go from it
|
||||||
if not incoming_node.is_leaf:
|
if len(incoming_node.nodes) == 0:
|
||||||
return False
|
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:
|
else:
|
||||||
# We delete the nodes if no edges go from it
|
merging_node = next(iter(incoming_node.nodes.values()))
|
||||||
if len(incoming_node.nodes) == 0:
|
incoming_node.is_leaf = merging_node.is_leaf
|
||||||
del self.nodes[word[0]]
|
incoming_node.prefix += merging_node.prefix
|
||||||
# We merge the current node with its only child
|
incoming_node.nodes = merging_node.nodes
|
||||||
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
|
return True
|
||||||
|
|
||||||
def print_tree(self, height: int = 0) -> None:
|
def print_tree(self, height: int = 0) -> None:
|
||||||
"""Print the tree
|
"""Print the tree
|
||||||
|
|
|
@ -274,14 +274,13 @@ def convex_hull_bf(points: list[Point]) -> list[Point]:
|
||||||
points_left_of_ij = True
|
points_left_of_ij = True
|
||||||
elif det_k < 0:
|
elif det_k < 0:
|
||||||
points_right_of_ij = True
|
points_right_of_ij = True
|
||||||
else:
|
# point[i], point[j], point[k] all lie on a straight line
|
||||||
# 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
|
||||||
# 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
|
||||||
# right of point[j], then point[i], point[j] cannot be
|
# part of the convex hull of A
|
||||||
# part of the convex hull of A
|
elif points[k] < points[i] or points[k] > points[j]:
|
||||||
if points[k] < points[i] or points[k] > points[j]:
|
ij_part_of_convex_hull = False
|
||||||
ij_part_of_convex_hull = False
|
break
|
||||||
break
|
|
||||||
|
|
||||||
if points_left_of_ij and points_right_of_ij:
|
if points_left_of_ij and points_right_of_ij:
|
||||||
ij_part_of_convex_hull = False
|
ij_part_of_convex_hull = False
|
||||||
|
|
|
@ -120,29 +120,29 @@ class GraphAdjacencyList(Generic[T]):
|
||||||
else:
|
else:
|
||||||
self.adj_list[source_vertex] = [destination_vertex]
|
self.adj_list[source_vertex] = [destination_vertex]
|
||||||
self.adj_list[destination_vertex] = [source_vertex]
|
self.adj_list[destination_vertex] = [source_vertex]
|
||||||
else: # For directed graphs
|
# For directed graphs
|
||||||
# if both source vertex and destination vertex are present in adjacency
|
# if both source vertex and destination vertex are present in adjacency
|
||||||
# list, add destination vertex to source vertex list of adjacent vertices.
|
# list, add destination vertex to source vertex list of adjacent vertices.
|
||||||
if source_vertex in self.adj_list and destination_vertex in self.adj_list:
|
elif source_vertex in self.adj_list and destination_vertex in self.adj_list:
|
||||||
self.adj_list[source_vertex].append(destination_vertex)
|
self.adj_list[source_vertex].append(destination_vertex)
|
||||||
# if only source vertex is present in adjacency list, add destination
|
# if only source vertex is present in adjacency list, add destination
|
||||||
# vertex to source vertex list of adjacent vertices and create a new vertex
|
# vertex to source vertex list of adjacent vertices and create a new vertex
|
||||||
# with destination vertex as key, which has no adjacent vertex
|
# with destination vertex as key, which has no adjacent vertex
|
||||||
elif source_vertex in self.adj_list:
|
elif source_vertex in self.adj_list:
|
||||||
self.adj_list[source_vertex].append(destination_vertex)
|
self.adj_list[source_vertex].append(destination_vertex)
|
||||||
self.adj_list[destination_vertex] = []
|
self.adj_list[destination_vertex] = []
|
||||||
# if only destination vertex is present in adjacency list, create a new
|
# if only destination vertex is present in adjacency list, create a new
|
||||||
# vertex with source vertex as key and assign a list containing destination
|
# vertex with source vertex as key and assign a list containing destination
|
||||||
# vertex as first adjacent vertex
|
# vertex as first adjacent vertex
|
||||||
elif destination_vertex in self.adj_list:
|
elif destination_vertex in self.adj_list:
|
||||||
self.adj_list[source_vertex] = [destination_vertex]
|
self.adj_list[source_vertex] = [destination_vertex]
|
||||||
# if both source vertex and destination vertex are not present in adjacency
|
# if both source vertex and destination vertex are not present in adjacency
|
||||||
# list, create a new vertex with source vertex as key and a list containing
|
# list, create a new vertex with source vertex as key and a list containing
|
||||||
# destination vertex as it's first adjacent vertex. Then create a new vertex
|
# destination vertex as it's first adjacent vertex. Then create a new vertex
|
||||||
# with destination vertex as key, which has no adjacent vertex
|
# with destination vertex as key, which has no adjacent vertex
|
||||||
else:
|
else:
|
||||||
self.adj_list[source_vertex] = [destination_vertex]
|
self.adj_list[source_vertex] = [destination_vertex]
|
||||||
self.adj_list[destination_vertex] = []
|
self.adj_list[destination_vertex] = []
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,10 @@ class Heap:
|
||||||
else:
|
else:
|
||||||
if 2 * start + 2 >= size:
|
if 2 * start + 2 >= size:
|
||||||
smallest_child = 2 * start + 1
|
smallest_child = 2 * start + 1
|
||||||
|
elif heap[2 * start + 1] < heap[2 * start + 2]:
|
||||||
|
smallest_child = 2 * start + 1
|
||||||
else:
|
else:
|
||||||
if heap[2 * start + 1] < heap[2 * start + 2]:
|
smallest_child = 2 * start + 2
|
||||||
smallest_child = 2 * start + 1
|
|
||||||
else:
|
|
||||||
smallest_child = 2 * start + 2
|
|
||||||
if heap[smallest_child] < heap[start]:
|
if heap[smallest_child] < heap[start]:
|
||||||
temp, temp1 = heap[smallest_child], positions[smallest_child]
|
temp, temp1 = heap[smallest_child], positions[smallest_child]
|
||||||
heap[smallest_child], positions[smallest_child] = (
|
heap[smallest_child], positions[smallest_child] = (
|
||||||
|
|
|
@ -270,24 +270,23 @@ def multi_a_star(start: TPos, goal: TPos, n_heuristic: int):
|
||||||
back_pointer,
|
back_pointer,
|
||||||
)
|
)
|
||||||
close_list_inad.append(get_s)
|
close_list_inad.append(get_s)
|
||||||
|
elif g_function[goal] <= open_list[0].minkey():
|
||||||
|
if g_function[goal] < float("inf"):
|
||||||
|
do_something(back_pointer, goal, start)
|
||||||
else:
|
else:
|
||||||
if g_function[goal] <= open_list[0].minkey():
|
get_s = open_list[0].top_show()
|
||||||
if g_function[goal] < float("inf"):
|
visited.add(get_s)
|
||||||
do_something(back_pointer, goal, start)
|
expand_state(
|
||||||
else:
|
get_s,
|
||||||
get_s = open_list[0].top_show()
|
0,
|
||||||
visited.add(get_s)
|
visited,
|
||||||
expand_state(
|
g_function,
|
||||||
get_s,
|
close_list_anchor,
|
||||||
0,
|
close_list_inad,
|
||||||
visited,
|
open_list,
|
||||||
g_function,
|
back_pointer,
|
||||||
close_list_anchor,
|
)
|
||||||
close_list_inad,
|
close_list_anchor.append(get_s)
|
||||||
open_list,
|
|
||||||
back_pointer,
|
|
||||||
)
|
|
||||||
close_list_anchor.append(get_s)
|
|
||||||
print("No path found to goal")
|
print("No path found to goal")
|
||||||
print()
|
print()
|
||||||
for i in range(n - 1, -1, -1):
|
for i in range(n - 1, -1, -1):
|
||||||
|
|
|
@ -113,11 +113,10 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool:
|
||||||
for i in list_vote:
|
for i in list_vote:
|
||||||
if i > actual_result:
|
if i > actual_result:
|
||||||
safe = not_safe + 1
|
safe = not_safe + 1
|
||||||
|
elif abs(abs(i) - abs(actual_result)) <= 0.1:
|
||||||
|
safe += 1
|
||||||
else:
|
else:
|
||||||
if abs(abs(i) - abs(actual_result)) <= 0.1:
|
not_safe += 1
|
||||||
safe += 1
|
|
||||||
else:
|
|
||||||
not_safe += 1
|
|
||||||
return safe > not_safe
|
return safe > not_safe
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,11 +20,10 @@ def res(x, y):
|
||||||
if 0 not in (x, y):
|
if 0 not in (x, y):
|
||||||
# We use the relation x^y = y*log10(x), where 10 is the base.
|
# We use the relation x^y = y*log10(x), where 10 is the base.
|
||||||
return y * math.log10(x)
|
return y * math.log10(x)
|
||||||
else:
|
elif x == 0: # 0 raised to any number is 0
|
||||||
if x == 0: # 0 raised to any number is 0
|
return 0
|
||||||
return 0
|
elif y == 0:
|
||||||
elif y == 0:
|
return 1 # any number raised to 0 is 1
|
||||||
return 1 # any number raised to 0 is 1
|
|
||||||
raise AssertionError("This should never happen")
|
raise AssertionError("This should never happen")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -94,14 +94,13 @@ def pollard_rho(
|
||||||
if divisor == 1:
|
if divisor == 1:
|
||||||
# No common divisor yet, just keep searching.
|
# No common divisor yet, just keep searching.
|
||||||
continue
|
continue
|
||||||
|
# We found a common divisor!
|
||||||
|
elif divisor == num:
|
||||||
|
# Unfortunately, the divisor is ``num`` itself and is useless.
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
# We found a common divisor!
|
# The divisor is a nontrivial factor of ``num``!
|
||||||
if divisor == num:
|
return divisor
|
||||||
# Unfortunately, the divisor is ``num`` itself and is useless.
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# The divisor is a nontrivial factor of ``num``!
|
|
||||||
return divisor
|
|
||||||
|
|
||||||
# If we made it here, then this attempt failed.
|
# If we made it here, then this attempt failed.
|
||||||
# We need to pick a new starting seed for the tortoise and hare
|
# We need to pick a new starting seed for the tortoise and hare
|
||||||
|
|
|
@ -73,12 +73,11 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float,
|
||||||
raise ValueError("Infinite solutions. (Consistent system)")
|
raise ValueError("Infinite solutions. (Consistent system)")
|
||||||
else:
|
else:
|
||||||
raise ValueError("No solution. (Inconsistent system)")
|
raise ValueError("No solution. (Inconsistent system)")
|
||||||
|
elif determinant_x == determinant_y == 0:
|
||||||
|
# Trivial solution (Inconsistent system)
|
||||||
|
return (0.0, 0.0)
|
||||||
else:
|
else:
|
||||||
if determinant_x == determinant_y == 0:
|
x = determinant_x / determinant
|
||||||
# Trivial solution (Inconsistent system)
|
y = determinant_y / determinant
|
||||||
return (0.0, 0.0)
|
# Non-Trivial Solution (Consistent system)
|
||||||
else:
|
return (x, y)
|
||||||
x = determinant_x / determinant
|
|
||||||
y = determinant_y / determinant
|
|
||||||
# Non-Trivial Solution (Consistent system)
|
|
||||||
return (x, y)
|
|
||||||
|
|
|
@ -46,10 +46,9 @@ def solution():
|
||||||
elif day > 29 and month == 2:
|
elif day > 29 and month == 2:
|
||||||
month += 1
|
month += 1
|
||||||
day = day - 29
|
day = day - 29
|
||||||
else:
|
elif day > days_per_month[month - 1]:
|
||||||
if day > days_per_month[month - 1]:
|
month += 1
|
||||||
month += 1
|
day = day - days_per_month[month - 2]
|
||||||
day = day - days_per_month[month - 2]
|
|
||||||
|
|
||||||
if month > 12:
|
if month > 12:
|
||||||
year += 1
|
year += 1
|
||||||
|
|
|
@ -12,7 +12,6 @@ lint.ignore = [ # `ruff rule S101` for a description of that rule
|
||||||
"NPY002", # Replace legacy `np.random.choice` call with `np.random.Generator` -- FIX ME
|
"NPY002", # Replace legacy `np.random.choice` call with `np.random.Generator` -- FIX ME
|
||||||
"PGH003", # Use specific rule codes when ignoring type issues -- FIX ME
|
"PGH003", # Use specific rule codes when ignoring type issues -- FIX ME
|
||||||
"PLC1901", # `{}` can be simplified to `{}` as an empty string is falsey
|
"PLC1901", # `{}` can be simplified to `{}` as an empty string is falsey
|
||||||
"PLR5501", # Consider using `elif` instead of `else` -- FIX ME
|
|
||||||
"PLW0120", # `else` clause on loop without a `break` statement -- FIX ME
|
"PLW0120", # `else` clause on loop without a `break` statement -- FIX ME
|
||||||
"PLW060", # Using global for `{name}` but no assignment is done -- DO NOT FIX
|
"PLW060", # Using global for `{name}` but no assignment is done -- DO NOT FIX
|
||||||
"PLW2901", # PLW2901: Redefined loop variable -- FIX ME
|
"PLW2901", # PLW2901: Redefined loop variable -- FIX ME
|
||||||
|
|
|
@ -137,11 +137,10 @@ def hill_climbing(
|
||||||
if change > max_change and change > 0:
|
if change > max_change and change > 0:
|
||||||
max_change = change
|
max_change = change
|
||||||
next_state = neighbor
|
next_state = neighbor
|
||||||
else: # finding min
|
elif change < min_change and change < 0: # finding min
|
||||||
# to direction with greatest descent
|
# to direction with greatest descent
|
||||||
if change < min_change and change < 0:
|
min_change = change
|
||||||
min_change = change
|
next_state = neighbor
|
||||||
next_state = neighbor
|
|
||||||
if next_state is not None:
|
if next_state is not None:
|
||||||
# we found at least one neighbor which improved the current state
|
# we found at least one neighbor which improved the current state
|
||||||
current_state = next_state
|
current_state = next_state
|
||||||
|
|
|
@ -33,18 +33,16 @@ def interpolation_search(sorted_collection, item):
|
||||||
current_item = sorted_collection[point]
|
current_item = sorted_collection[point]
|
||||||
if current_item == item:
|
if current_item == item:
|
||||||
return point
|
return point
|
||||||
|
elif point < left:
|
||||||
|
right = left
|
||||||
|
left = point
|
||||||
|
elif point > right:
|
||||||
|
left = right
|
||||||
|
right = point
|
||||||
|
elif item < current_item:
|
||||||
|
right = point - 1
|
||||||
else:
|
else:
|
||||||
if point < left:
|
left = point + 1
|
||||||
right = left
|
|
||||||
left = point
|
|
||||||
elif point > right:
|
|
||||||
left = right
|
|
||||||
right = point
|
|
||||||
else:
|
|
||||||
if item < current_item:
|
|
||||||
right = point - 1
|
|
||||||
else:
|
|
||||||
left = point + 1
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,15 +77,14 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
|
||||||
return interpolation_search_by_recursion(sorted_collection, item, point, left)
|
return interpolation_search_by_recursion(sorted_collection, item, point, left)
|
||||||
elif point > right:
|
elif point > right:
|
||||||
return interpolation_search_by_recursion(sorted_collection, item, right, left)
|
return interpolation_search_by_recursion(sorted_collection, item, right, left)
|
||||||
|
elif sorted_collection[point] > item:
|
||||||
|
return interpolation_search_by_recursion(
|
||||||
|
sorted_collection, item, left, point - 1
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if sorted_collection[point] > item:
|
return interpolation_search_by_recursion(
|
||||||
return interpolation_search_by_recursion(
|
sorted_collection, item, point + 1, right
|
||||||
sorted_collection, item, left, point - 1
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
return interpolation_search_by_recursion(
|
|
||||||
sorted_collection, item, point + 1, right
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def __assert_sorted(collection):
|
def __assert_sorted(collection):
|
||||||
|
|
|
@ -60,19 +60,18 @@ def compute_transform_tables(
|
||||||
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
|
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
|
||||||
if i == 0 and j == 0:
|
if i == 0 and j == 0:
|
||||||
return []
|
return []
|
||||||
|
elif ops[i][j][0] in {"C", "R"}:
|
||||||
|
seq = assemble_transformation(ops, i - 1, j - 1)
|
||||||
|
seq.append(ops[i][j])
|
||||||
|
return seq
|
||||||
|
elif ops[i][j][0] == "D":
|
||||||
|
seq = assemble_transformation(ops, i - 1, j)
|
||||||
|
seq.append(ops[i][j])
|
||||||
|
return seq
|
||||||
else:
|
else:
|
||||||
if ops[i][j][0] in {"C", "R"}:
|
seq = assemble_transformation(ops, i, j - 1)
|
||||||
seq = assemble_transformation(ops, i - 1, j - 1)
|
seq.append(ops[i][j])
|
||||||
seq.append(ops[i][j])
|
return seq
|
||||||
return seq
|
|
||||||
elif ops[i][j][0] == "D":
|
|
||||||
seq = assemble_transformation(ops, i - 1, j)
|
|
||||||
seq.append(ops[i][j])
|
|
||||||
return seq
|
|
||||||
else:
|
|
||||||
seq = assemble_transformation(ops, i, j - 1)
|
|
||||||
seq.append(ops[i][j])
|
|
||||||
return seq
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user