mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 02:18:39 +00:00
Compare commits
3 Commits
5aefc00f0f
...
9e08c7726d
Author | SHA1 | Date | |
---|---|---|---|
|
9e08c7726d | ||
|
f7531d9874 | ||
|
93fb169627 |
@ -16,7 +16,7 @@ repos:
|
|||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.0.278
|
rev: v0.0.280
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
|
|
||||||
|
@ -25,6 +25,8 @@ We appreciate any contribution, from fixing a grammar mistake in a comment to im
|
|||||||
|
|
||||||
Your contribution will be tested by our [automated testing on GitHub Actions](https://github.com/TheAlgorithms/Python/actions) to save time and mental energy. After you have submitted your pull request, you should see the GitHub Actions tests start to run at the bottom of your submission page. If those tests fail, then click on the ___details___ button try to read through the GitHub Actions output to understand the failure. If you do not understand, please leave a comment on your submission page and a community member will try to help.
|
Your contribution will be tested by our [automated testing on GitHub Actions](https://github.com/TheAlgorithms/Python/actions) to save time and mental energy. After you have submitted your pull request, you should see the GitHub Actions tests start to run at the bottom of your submission page. If those tests fail, then click on the ___details___ button try to read through the GitHub Actions output to understand the failure. If you do not understand, please leave a comment on your submission page and a community member will try to help.
|
||||||
|
|
||||||
|
If you are interested in resolving an [open issue](https://github.com/TheAlgorithms/Python/issues), simply make a pull request with your proposed fix. __We do not assign issues in this repo__ so please do not ask for permission to work on an issue.
|
||||||
|
|
||||||
Please help us keep our issue list small by adding `Fixes #{$ISSUE_NUMBER}` to the description of pull requests that resolve open issues.
|
Please help us keep our issue list small by adding `Fixes #{$ISSUE_NUMBER}` to the description of pull requests that resolve open issues.
|
||||||
For example, if your pull request fixes issue #10, then please add the following to its description:
|
For example, if your pull request fixes issue #10, then please add the following to its description:
|
||||||
```
|
```
|
||||||
|
@ -98,7 +98,7 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
|
|||||||
if pt:
|
if pt:
|
||||||
if alive < 2:
|
if alive < 2:
|
||||||
state = False
|
state = False
|
||||||
elif alive == 2 or alive == 3:
|
elif alive in {2, 3}:
|
||||||
state = True
|
state = True
|
||||||
elif alive > 3:
|
elif alive > 3:
|
||||||
state = False
|
state = False
|
||||||
|
@ -152,7 +152,7 @@ class RedBlackTree:
|
|||||||
self.grandparent.color = 1
|
self.grandparent.color = 1
|
||||||
self.grandparent._insert_repair()
|
self.grandparent._insert_repair()
|
||||||
|
|
||||||
def remove(self, label: int) -> RedBlackTree:
|
def remove(self, label: int) -> RedBlackTree: # noqa: PLR0912
|
||||||
"""Remove label from this tree."""
|
"""Remove label from this tree."""
|
||||||
if self.label == label:
|
if self.label == label:
|
||||||
if self.left and self.right:
|
if self.left and self.right:
|
||||||
|
@ -156,7 +156,7 @@ class RadixNode:
|
|||||||
del self.nodes[word[0]]
|
del self.nodes[word[0]]
|
||||||
# We merge the current node with its only child
|
# We merge the current node with its only child
|
||||||
if len(self.nodes) == 1 and not self.is_leaf:
|
if len(self.nodes) == 1 and not self.is_leaf:
|
||||||
merging_node = list(self.nodes.values())[0]
|
merging_node = next(iter(self.nodes.values()))
|
||||||
self.is_leaf = merging_node.is_leaf
|
self.is_leaf = merging_node.is_leaf
|
||||||
self.prefix += merging_node.prefix
|
self.prefix += merging_node.prefix
|
||||||
self.nodes = merging_node.nodes
|
self.nodes = merging_node.nodes
|
||||||
@ -165,7 +165,7 @@ class RadixNode:
|
|||||||
incoming_node.is_leaf = False
|
incoming_node.is_leaf = False
|
||||||
# If there is 1 edge, we merge it with its child
|
# If there is 1 edge, we merge it with its child
|
||||||
else:
|
else:
|
||||||
merging_node = list(incoming_node.nodes.values())[0]
|
merging_node = next(iter(incoming_node.nodes.values()))
|
||||||
incoming_node.is_leaf = merging_node.is_leaf
|
incoming_node.is_leaf = merging_node.is_leaf
|
||||||
incoming_node.prefix += merging_node.prefix
|
incoming_node.prefix += merging_node.prefix
|
||||||
incoming_node.nodes = merging_node.nodes
|
incoming_node.nodes = merging_node.nodes
|
||||||
|
@ -266,7 +266,7 @@ def convex_hull_bf(points: list[Point]) -> list[Point]:
|
|||||||
points_left_of_ij = points_right_of_ij = False
|
points_left_of_ij = points_right_of_ij = False
|
||||||
ij_part_of_convex_hull = True
|
ij_part_of_convex_hull = True
|
||||||
for k in range(n):
|
for k in range(n):
|
||||||
if k != i and k != j:
|
if k not in {i, j}:
|
||||||
det_k = _det(points[i], points[j], points[k])
|
det_k = _det(points[i], points[j], points[k])
|
||||||
|
|
||||||
if det_k > 0:
|
if det_k > 0:
|
||||||
|
@ -39,7 +39,7 @@ class DirectedGraph:
|
|||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
if s == -2:
|
if s == -2:
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
ss = s
|
ss = s
|
||||||
@ -87,7 +87,7 @@ class DirectedGraph:
|
|||||||
d = deque()
|
d = deque()
|
||||||
visited = []
|
visited = []
|
||||||
if s == -2:
|
if s == -2:
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
d.append(s)
|
d.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
while d:
|
while d:
|
||||||
@ -114,7 +114,7 @@ class DirectedGraph:
|
|||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
if s == -2:
|
if s == -2:
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
ss = s
|
ss = s
|
||||||
@ -146,7 +146,7 @@ class DirectedGraph:
|
|||||||
def cycle_nodes(self):
|
def cycle_nodes(self):
|
||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
parent = -2
|
parent = -2
|
||||||
@ -199,7 +199,7 @@ class DirectedGraph:
|
|||||||
def has_cycle(self):
|
def has_cycle(self):
|
||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
parent = -2
|
parent = -2
|
||||||
@ -305,7 +305,7 @@ class Graph:
|
|||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
if s == -2:
|
if s == -2:
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
ss = s
|
ss = s
|
||||||
@ -353,7 +353,7 @@ class Graph:
|
|||||||
d = deque()
|
d = deque()
|
||||||
visited = []
|
visited = []
|
||||||
if s == -2:
|
if s == -2:
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
d.append(s)
|
d.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
while d:
|
while d:
|
||||||
@ -371,7 +371,7 @@ class Graph:
|
|||||||
def cycle_nodes(self):
|
def cycle_nodes(self):
|
||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
parent = -2
|
parent = -2
|
||||||
@ -424,7 +424,7 @@ class Graph:
|
|||||||
def has_cycle(self):
|
def has_cycle(self):
|
||||||
stack = []
|
stack = []
|
||||||
visited = []
|
visited = []
|
||||||
s = list(self.graph)[0]
|
s = next(iter(self.graph))
|
||||||
stack.append(s)
|
stack.append(s)
|
||||||
visited.append(s)
|
visited.append(s)
|
||||||
parent = -2
|
parent = -2
|
||||||
|
@ -113,7 +113,7 @@ class PushRelabelExecutor(MaximumFlowAlgorithmExecutor):
|
|||||||
vertices_list = [
|
vertices_list = [
|
||||||
i
|
i
|
||||||
for i in range(self.verticies_count)
|
for i in range(self.verticies_count)
|
||||||
if i != self.source_index and i != self.sink_index
|
if i not in {self.source_index, self.sink_index}
|
||||||
]
|
]
|
||||||
|
|
||||||
# move through list
|
# move through list
|
||||||
|
@ -55,7 +55,7 @@ def factorial_recursive(n: int) -> int:
|
|||||||
raise ValueError("factorial() only accepts integral values")
|
raise ValueError("factorial() only accepts integral values")
|
||||||
if n < 0:
|
if n < 0:
|
||||||
raise ValueError("factorial() not defined for negative values")
|
raise ValueError("factorial() not defined for negative values")
|
||||||
return 1 if n == 0 or n == 1 else n * factorial(n - 1)
|
return 1 if n in {0, 1} else n * factorial(n - 1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -154,7 +154,7 @@ def prime_factorization(number):
|
|||||||
|
|
||||||
quotient = number
|
quotient = number
|
||||||
|
|
||||||
if number == 0 or number == 1:
|
if number in {0, 1}:
|
||||||
ans.append(number)
|
ans.append(number)
|
||||||
|
|
||||||
# if 'number' not prime then builds the prime factorization of 'number'
|
# if 'number' not prime then builds the prime factorization of 'number'
|
||||||
|
@ -253,7 +253,7 @@ def find_unit_clauses(
|
|||||||
unit_symbols = []
|
unit_symbols = []
|
||||||
for clause in clauses:
|
for clause in clauses:
|
||||||
if len(clause) == 1:
|
if len(clause) == 1:
|
||||||
unit_symbols.append(list(clause.literals.keys())[0])
|
unit_symbols.append(next(iter(clause.literals.keys())))
|
||||||
else:
|
else:
|
||||||
f_count, n_count = 0, 0
|
f_count, n_count = 0, 0
|
||||||
for literal, value in clause.literals.items():
|
for literal, value in clause.literals.items():
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
A number container system that uses binary search to delete and insert values into
|
A number container system that uses binary search to delete and insert values into
|
||||||
arrays with O(n logn) write times and O(1) read times.
|
arrays with O(log n) write times and O(1) read times.
|
||||||
|
|
||||||
This container system holds integers at indexes.
|
This container system holds integers at indexes.
|
||||||
|
|
||||||
|
@ -28,12 +28,16 @@ def solution() -> int:
|
|||||||
31875000
|
31875000
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return [
|
return next(
|
||||||
a * b * (1000 - a - b)
|
iter(
|
||||||
for a in range(1, 999)
|
[
|
||||||
for b in range(a, 999)
|
a * b * (1000 - a - b)
|
||||||
if (a * a + b * b == (1000 - a - b) ** 2)
|
for a in range(1, 999)
|
||||||
][0]
|
for b in range(a, 999)
|
||||||
|
if (a * a + b * b == (1000 - a - b) ** 2)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -107,7 +107,7 @@ def ripple_adder(
|
|||||||
res = qiskit.execute(circuit, backend, shots=1).result()
|
res = qiskit.execute(circuit, backend, shots=1).result()
|
||||||
|
|
||||||
# The result is in binary. Convert it back to int
|
# The result is in binary. Convert it back to int
|
||||||
return int(list(res.get_counts())[0], 2)
|
return int(next(iter(res.get_counts())), 2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -61,7 +61,7 @@ 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 []
|
||||||
else:
|
else:
|
||||||
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
if ops[i][j][0] in {"C", "R"}:
|
||||||
seq = assemble_transformation(ops, i - 1, j - 1)
|
seq = assemble_transformation(ops, i - 1, j - 1)
|
||||||
seq.append(ops[i][j])
|
seq.append(ops[i][j])
|
||||||
return seq
|
return seq
|
||||||
|
@ -90,9 +90,7 @@ def convert(number: int) -> str:
|
|||||||
else:
|
else:
|
||||||
addition = ""
|
addition = ""
|
||||||
if counter in placevalue:
|
if counter in placevalue:
|
||||||
if current == 0 and ((temp_num % 100) // 10) == 0:
|
if current != 0 and ((temp_num % 100) // 10) != 0:
|
||||||
addition = ""
|
|
||||||
else:
|
|
||||||
addition = placevalue[counter]
|
addition = placevalue[counter]
|
||||||
if ((temp_num % 100) // 10) == 1:
|
if ((temp_num % 100) // 10) == 1:
|
||||||
words = teens[current] + addition + words
|
words = teens[current] + addition + words
|
||||||
|
Loading…
x
Reference in New Issue
Block a user