From 5be2a1fd3ddf33bc377f6beb5d3d301d565e2ee9 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Tue, 1 Nov 2022 21:33:19 +0000 Subject: [PATCH 01/12] ci: Add `B023` to `.flake8` ignores --- .flake8 | 1 + genetic_algorithm/basic_string.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.flake8 b/.flake8 index 0d9ef18d142b..582234724b78 100644 --- a/.flake8 +++ b/.flake8 @@ -6,3 +6,4 @@ extend-ignore = # Formatting style for `black` E203 # Whitespace before ':' W503 # Line break occurred before a binary operator + B023 # Function definition does not bind loop variable \ No newline at end of file diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index 45b8be651f6e..7c9b31f18b8b 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -129,9 +129,7 @@ def select(parent_1: tuple[str, float]) -> list[str]: child_n = int(parent_1[1] * 100) + 1 child_n = 10 if child_n >= 10 else child_n for _ in range(child_n): - parent_2 = population_score[ # noqa: B023 - random.randint(0, N_SELECTED) - ][0] + parent_2 = population_score[random.randint(0, N_SELECTED)][0] child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list. From 8cebc9fef64db8c18b1da8988cccba919f929956 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 2 Nov 2022 16:48:11 +0000 Subject: [PATCH 02/12] refactor: Return `bool`/raise Exception --- boolean_algebra/quine_mc_cluskey.py | 7 +- ciphers/shuffled_shift_cipher.py | 2 +- computer_vision/harris_corner.py | 3 +- .../binary_tree/segment_tree_other.py | 112 +++++++++--------- data_structures/binary_tree/wavelet_tree.py | 6 +- .../linked_list/doubly_linked_list.py | 2 +- data_structures/queue/double_ended_queue.py | 2 +- graphs/breadth_first_search_shortest_path.py | 6 +- graphs/page_rank.py | 2 +- linear_algebra/src/polynom_for_points.py | 2 +- maths/monte_carlo_dice.py | 3 - matrix/cramers_rule_2x2.py | 16 +-- other/password.py | 30 ++--- strings/dna.py | 15 ++- 14 files changed, 105 insertions(+), 103 deletions(-) diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py index 5bd7117bb3e7..d2a4e6ae978c 100644 --- a/boolean_algebra/quine_mc_cluskey.py +++ b/boolean_algebra/quine_mc_cluskey.py @@ -1,15 +1,16 @@ from __future__ import annotations from collections.abc import Sequence +from typing import Literal -def compare_string(string1: str, string2: str) -> str: +def compare_string(string1: str, string2: str) -> str | Literal[False]: """ >>> compare_string('0010','0110') '0_10' >>> compare_string('0110','1101') - 'X' + False """ list1 = list(string1) list2 = list(string2) @@ -19,7 +20,7 @@ def compare_string(string1: str, string2: str) -> str: count += 1 list1[i] = "_" if count > 1: - return "X" + return False else: return "".join(list1) diff --git a/ciphers/shuffled_shift_cipher.py b/ciphers/shuffled_shift_cipher.py index 714acd4b1afc..08b2cab97c69 100644 --- a/ciphers/shuffled_shift_cipher.py +++ b/ciphers/shuffled_shift_cipher.py @@ -42,7 +42,7 @@ def __str__(self) -> str: """ :return: passcode of the cipher object """ - return "Passcode is: " + "".join(self.__passcode) + return "".join(self.__passcode) def __neg_pos(self, iterlist: list[int]) -> list[int]: """ diff --git a/computer_vision/harris_corner.py b/computer_vision/harris_corner.py index 7850085f8935..35394bca4dd9 100644 --- a/computer_vision/harris_corner.py +++ b/computer_vision/harris_corner.py @@ -22,8 +22,7 @@ def __init__(self, k: float, window_size: int): raise ValueError("invalid k value") def __str__(self) -> str: - - return f"Harris Corner detection with k : {self.k}" + return self.k def detect(self, img_path: str) -> tuple[cv2.Mat, list[list[int]]]: diff --git a/data_structures/binary_tree/segment_tree_other.py b/data_structures/binary_tree/segment_tree_other.py index 90afd7ca8b71..dc9b51b69daa 100644 --- a/data_structures/binary_tree/segment_tree_other.py +++ b/data_structures/binary_tree/segment_tree_other.py @@ -16,8 +16,8 @@ def __init__(self, start, end, val, left=None, right=None): self.left = left self.right = right - def __str__(self): - return f"val: {self.val}, start: {self.start}, end: {self.end}" + def __repr__(self): + return f"" class SegmentTree: @@ -27,29 +27,29 @@ class SegmentTree: >>> for node in num_arr.traverse(): ... print(node) ... - val: 15, start: 0, end: 4 - val: 8, start: 0, end: 2 - val: 7, start: 3, end: 4 - val: 3, start: 0, end: 1 - val: 5, start: 2, end: 2 - val: 3, start: 3, end: 3 - val: 4, start: 4, end: 4 - val: 2, start: 0, end: 0 - val: 1, start: 1, end: 1 + + + + + + + + + >>> >>> num_arr.update(1, 5) >>> for node in num_arr.traverse(): ... print(node) ... - val: 19, start: 0, end: 4 - val: 12, start: 0, end: 2 - val: 7, start: 3, end: 4 - val: 7, start: 0, end: 1 - val: 5, start: 2, end: 2 - val: 3, start: 3, end: 3 - val: 4, start: 4, end: 4 - val: 2, start: 0, end: 0 - val: 5, start: 1, end: 1 + + + + + + + + + >>> >>> num_arr.query_range(3, 4) 7 @@ -62,29 +62,29 @@ class SegmentTree: >>> for node in max_arr.traverse(): ... print(node) ... - val: 5, start: 0, end: 4 - val: 5, start: 0, end: 2 - val: 4, start: 3, end: 4 - val: 2, start: 0, end: 1 - val: 5, start: 2, end: 2 - val: 3, start: 3, end: 3 - val: 4, start: 4, end: 4 - val: 2, start: 0, end: 0 - val: 1, start: 1, end: 1 + + + + + + + + + >>> >>> max_arr.update(1, 5) >>> for node in max_arr.traverse(): ... print(node) ... - val: 5, start: 0, end: 4 - val: 5, start: 0, end: 2 - val: 4, start: 3, end: 4 - val: 5, start: 0, end: 1 - val: 5, start: 2, end: 2 - val: 3, start: 3, end: 3 - val: 4, start: 4, end: 4 - val: 2, start: 0, end: 0 - val: 5, start: 1, end: 1 + + + + + + + + + >>> >>> max_arr.query_range(3, 4) 4 @@ -97,29 +97,29 @@ class SegmentTree: >>> for node in min_arr.traverse(): ... print(node) ... - val: 1, start: 0, end: 4 - val: 1, start: 0, end: 2 - val: 3, start: 3, end: 4 - val: 1, start: 0, end: 1 - val: 5, start: 2, end: 2 - val: 3, start: 3, end: 3 - val: 4, start: 4, end: 4 - val: 2, start: 0, end: 0 - val: 1, start: 1, end: 1 + + + + + + + + + >>> >>> min_arr.update(1, 5) >>> for node in min_arr.traverse(): ... print(node) ... - val: 2, start: 0, end: 4 - val: 2, start: 0, end: 2 - val: 3, start: 3, end: 4 - val: 2, start: 0, end: 1 - val: 5, start: 2, end: 2 - val: 3, start: 3, end: 3 - val: 4, start: 4, end: 4 - val: 2, start: 0, end: 0 - val: 5, start: 1, end: 1 + + + + + + + + + >>> >>> min_arr.query_range(3, 4) 3 diff --git a/data_structures/binary_tree/wavelet_tree.py b/data_structures/binary_tree/wavelet_tree.py index 8d7145189018..4e3759e945a5 100644 --- a/data_structures/binary_tree/wavelet_tree.py +++ b/data_structures/binary_tree/wavelet_tree.py @@ -24,11 +24,11 @@ def __repr__(self) -> str: """ >>> node = Node(length=27) >>> repr(node) - 'min_value: -1, max_value: -1' + '' >>> repr(node) == str(node) True """ - return f"min_value: {self.minn}, max_value: {self.maxx}" + return f"" def build_tree(arr: list[int]) -> Node | None: @@ -37,7 +37,7 @@ def build_tree(arr: list[int]) -> Node | None: of the constructed tree >>> build_tree(test_array) - min_value: 0, max_value: 9 + """ root = Node(len(arr)) root.minn, root.maxx = min(arr), max(arr) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 90b6b6eb2a32..6c81493fff85 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -159,7 +159,7 @@ def delete(self, data) -> str: if current.next: current = current.next else: # We have reached the end an no value matches - return "No data matching given value" + raise ValueError("No data matching given value") if current == self.head: self.delete_head() diff --git a/data_structures/queue/double_ended_queue.py b/data_structures/queue/double_ended_queue.py index 7053879d4512..704fe44443dc 100644 --- a/data_structures/queue/double_ended_queue.py +++ b/data_structures/queue/double_ended_queue.py @@ -426,7 +426,7 @@ def __repr__(self) -> str: values_list.append(aux.val) aux = aux.next - return "[" + ", ".join(repr(val) for val in values_list) + "]" + return f"[{', '.join(repr(val) for val in values_list)}]" if __name__ == "__main__": diff --git a/graphs/breadth_first_search_shortest_path.py b/graphs/breadth_first_search_shortest_path.py index 697a8c634859..e81cd20db276 100644 --- a/graphs/breadth_first_search_shortest_path.py +++ b/graphs/breadth_first_search_shortest_path.py @@ -58,7 +58,9 @@ def shortest_path(self, target_vertex: str) -> str: Case 1 - No path is found. >>> g.shortest_path("Foo") - 'No path from vertex:G to vertex:Foo' + Traceback (most recent call last): + ... + ValueError: No path from vertex: G to vertex: Foo Case 2 - The path is found. >>> g.shortest_path("D") @@ -71,7 +73,7 @@ def shortest_path(self, target_vertex: str) -> str: target_vertex_parent = self.parent.get(target_vertex) if target_vertex_parent is None: - return f"No path from vertex:{self.source_vertex} to vertex:{target_vertex}" + raise ValueError(f"No path from vertex: {self.source_vertex} to vertex: {target_vertex}") return self.shortest_path(target_vertex_parent) + f"->{target_vertex}" diff --git a/graphs/page_rank.py b/graphs/page_rank.py index e1af35b34749..b9e4c4a72a93 100644 --- a/graphs/page_rank.py +++ b/graphs/page_rank.py @@ -27,7 +27,7 @@ def add_outbound(self, node): self.outbound.append(node) def __repr__(self): - return f"Node {self.name}: Inbound: {self.inbound} ; Outbound: {self.outbound}" + return f"" def page_rank(nodes, limit=3, d=0.85): diff --git a/linear_algebra/src/polynom_for_points.py b/linear_algebra/src/polynom_for_points.py index 091849542ffe..21d66ac4908a 100644 --- a/linear_algebra/src/polynom_for_points.py +++ b/linear_algebra/src/polynom_for_points.py @@ -113,7 +113,7 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str: elif check == 2: return solved else: - return "The program cannot work out a fitting polynomial." + raise ValueError("The program cannot work out a fitting polynomial.") if __name__ == "__main__": diff --git a/maths/monte_carlo_dice.py b/maths/monte_carlo_dice.py index c4150b88f6cc..362f70b49828 100644 --- a/maths/monte_carlo_dice.py +++ b/maths/monte_carlo_dice.py @@ -13,9 +13,6 @@ def __init__(self): def roll(self): return random.choice(self.sides) - def _str_(self): - return "Fair Dice" - def throw_dice(num_throws: int, num_dice: int = 2) -> list[float]: """ diff --git a/matrix/cramers_rule_2x2.py b/matrix/cramers_rule_2x2.py index a635d66fbb6c..67dbfdcd6246 100644 --- a/matrix/cramers_rule_2x2.py +++ b/matrix/cramers_rule_2x2.py @@ -2,7 +2,7 @@ # https://en.wikipedia.org/wiki/Cramer%27s_rule -def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str: +def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float]: """ Solves the system of linear equation in 2 variables. :param: equation1: list of 3 numbers @@ -14,13 +14,13 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str: determinant_y = [[a1, d1], [a2, d2]] >>> cramers_rule_2x2([2, 3, 0], [5, 1, 0]) - 'Trivial solution. (Consistent system) x = 0 and y = 0' + (0, 0) >>> cramers_rule_2x2([0, 4, 50], [2, 0, 26]) - 'Non-Trivial Solution (Consistent system) x = 13.0, y = 12.5' + (13.0, 12.5) >>> cramers_rule_2x2([11, 2, 30], [1, 0, 4]) - 'Non-Trivial Solution (Consistent system) x = 4.0, y = -7.0' + (4.0, -7.0) >>> cramers_rule_2x2([4, 7, 1], [1, 2, 0]) - 'Non-Trivial Solution (Consistent system) x = 2.0, y = -1.0' + (2.0, -1.0) >>> cramers_rule_2x2([1, 2, 3], [2, 4, 6]) Traceback (most recent call last): @@ -75,8 +75,10 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> str: raise ValueError("No solution. (Inconsistent system)") else: if determinant_x == determinant_y == 0: - return "Trivial solution. (Consistent system) x = 0 and y = 0" + # Trivial solution (Inconsistent system) + return (0, 0) else: x = determinant_x / determinant y = determinant_y / determinant - return f"Non-Trivial Solution (Consistent system) x = {x}, y = {y}" + # Non-Trivial Solution (Consistent system) + return (x, y) diff --git a/other/password.py b/other/password.py index 8f6833073288..f1764b7ec168 100644 --- a/other/password.py +++ b/other/password.py @@ -66,26 +66,23 @@ def random_characters(chars_incl, i): # This Will Check Whether A Given Password Is Strong Or Not # It Follows The Rule that Length Of Password Should Be At Least 8 Characters # And At Least 1 Lower, 1 Upper, 1 Number And 1 Special Character -def strong_password_detector(password: str, min_length: int = 8) -> str: +def is_strong_password(password: str, min_length: int = 8) -> bool: """ - >>> strong_password_detector('Hwea7$2!') - 'This is a strong Password' - + >>> is_strong_password('Hwea7$2!') + True >>> strong_password_detector('Sh0r1') - 'Your Password must be at least 8 characters long' - + False >>> strong_password_detector('Hello123') - 'Password should contain UPPERCASE, lowercase, numbers, special characters' - + False >>> strong_password_detector('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1') - 'This is a strong Password' - + True >>> strong_password_detector('0') - 'Your Password must be at least 8 characters long' + False """ if len(password) < min_length: - return "Your Password must be at least 8 characters long" + # Your Password must be at least 8 characters long + return False upper = any(char in ascii_uppercase for char in password) lower = any(char in ascii_lowercase for char in password) @@ -93,13 +90,12 @@ def strong_password_detector(password: str, min_length: int = 8) -> str: spec_char = any(char in punctuation for char in password) if upper and lower and num and spec_char: - return "This is a strong Password" + return True else: - return ( - "Password should contain UPPERCASE, lowercase, " - "numbers, special characters" - ) + # Passwords should contain UPPERCASE, lowerase + # numbers, and special characters + return False def main(): diff --git a/strings/dna.py b/strings/dna.py index 46e271d689db..c2b96110b893 100644 --- a/strings/dna.py +++ b/strings/dna.py @@ -14,13 +14,18 @@ def dna(dna: str) -> str: >>> dna("CTGA") 'GACT' >>> dna("GFGG") - 'Invalid Strand' + Traceback (most recent call last): + ... + ValueError: Invalid Strand """ - r = len(re.findall("[ATCG]", dna)) != len(dna) - val = dna.translate(dna.maketrans("ATCG", "TAGC")) - return "Invalid Strand" if r else val + if len(re.findall("[ATCG]", dna)) != len(dna): + raise ValueError("Invalid Strand") + + return dna.translate(dna.maketrans("ATCG", "TAGC")) if __name__ == "__main__": - __import__("doctest").testmod() + import doctest + + doctest.testmod() From 5d0cafa22391e75c643673aaaa332e7d1f2f63bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 16:50:05 +0000 Subject: [PATCH 03/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/breadth_first_search_shortest_path.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphs/breadth_first_search_shortest_path.py b/graphs/breadth_first_search_shortest_path.py index e81cd20db276..cb21076f91d2 100644 --- a/graphs/breadth_first_search_shortest_path.py +++ b/graphs/breadth_first_search_shortest_path.py @@ -73,7 +73,9 @@ def shortest_path(self, target_vertex: str) -> str: target_vertex_parent = self.parent.get(target_vertex) if target_vertex_parent is None: - raise ValueError(f"No path from vertex: {self.source_vertex} to vertex: {target_vertex}") + raise ValueError( + f"No path from vertex: {self.source_vertex} to vertex: {target_vertex}" + ) return self.shortest_path(target_vertex_parent) + f"->{target_vertex}" From 982f45811736227398b3b5b8c8c7a89b19bf46e3 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 2 Nov 2022 16:51:57 +0000 Subject: [PATCH 04/12] revert: Remove previous branch commit --- .flake8 | 1 - 1 file changed, 1 deletion(-) diff --git a/.flake8 b/.flake8 index 582234724b78..0d9ef18d142b 100644 --- a/.flake8 +++ b/.flake8 @@ -6,4 +6,3 @@ extend-ignore = # Formatting style for `black` E203 # Whitespace before ':' W503 # Line break occurred before a binary operator - B023 # Function definition does not bind loop variable \ No newline at end of file From d3b1f155c42af2337851d5e79d9378b645d5e22d Mon Sep 17 00:00:00 2001 From: Caeden Perelli-Harris Date: Wed, 2 Nov 2022 16:58:07 +0000 Subject: [PATCH 05/12] Update data_structures/binary_tree/segment_tree_other.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/segment_tree_other.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/segment_tree_other.py b/data_structures/binary_tree/segment_tree_other.py index dc9b51b69daa..f32ee5213306 100644 --- a/data_structures/binary_tree/segment_tree_other.py +++ b/data_structures/binary_tree/segment_tree_other.py @@ -17,7 +17,7 @@ def __init__(self, start, end, val, left=None, right=None): self.right = right def __repr__(self): - return f"" + return f"SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})" class SegmentTree: From ae1f7bc6462b72a096314a50fff1682133f37dcb Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 2 Nov 2022 17:05:13 +0000 Subject: [PATCH 06/12] feat: Apply `__repr__` changes --- boolean_algebra/quine_mc_cluskey.py | 4 +- computer_vision/harris_corner.py | 2 +- .../binary_tree/segment_tree_other.py | 108 +++++++++--------- data_structures/binary_tree/wavelet_tree.py | 6 +- matrix/cramers_rule_2x2.py | 4 +- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py index d2a4e6ae978c..6788dfb28ba1 100644 --- a/boolean_algebra/quine_mc_cluskey.py +++ b/boolean_algebra/quine_mc_cluskey.py @@ -37,10 +37,10 @@ def check(binary: list[str]) -> list[str]: for i in range(len(binary)): for j in range(i + 1, len(binary)): k = compare_string(binary[i], binary[j]) - if k != "X": + if k is False: check1[i] = "*" check1[j] = "*" - temp.append(k) + temp.append("X") for i in range(len(binary)): if check1[i] == "$": pi.append(binary[i]) diff --git a/computer_vision/harris_corner.py b/computer_vision/harris_corner.py index 35394bca4dd9..c8905bb6a9cd 100644 --- a/computer_vision/harris_corner.py +++ b/computer_vision/harris_corner.py @@ -22,7 +22,7 @@ def __init__(self, k: float, window_size: int): raise ValueError("invalid k value") def __str__(self) -> str: - return self.k + return str(self.k) def detect(self, img_path: str) -> tuple[cv2.Mat, list[list[int]]]: diff --git a/data_structures/binary_tree/segment_tree_other.py b/data_structures/binary_tree/segment_tree_other.py index f32ee5213306..409d3464975c 100644 --- a/data_structures/binary_tree/segment_tree_other.py +++ b/data_structures/binary_tree/segment_tree_other.py @@ -27,29 +27,29 @@ class SegmentTree: >>> for node in num_arr.traverse(): ... print(node) ... - - - - - - - - - + SegmentTreeNode(start=0, end=4, val=15) + SegmentTreeNode(start=0, end=2, val=8) + SegmentTreeNode(start=3, end=4, val=7) + SegmentTreeNode(start=0, end=1, val=3) + SegmentTreeNode(start=2, end=2, val=5) + SegmentTreeNode(start=3, end=3, val=3) + SegmentTreeNode(start=4, end=4, val=4) + SegmentTreeNode(start=0, end=0, val=2) + SegmentTreeNode(start=1, end=1, val=1) >>> >>> num_arr.update(1, 5) >>> for node in num_arr.traverse(): ... print(node) ... - - - - - - - - - + SegmentTreeNode(start=0, end=4, val=19) + SegmentTreeNode(start=0, end=2, val=12) + SegmentTreeNode(start=3, end=4, val=7) + SegmentTreeNode(start=0, end=1, val=7) + SegmentTreeNode(start=2, end=2, val=5) + SegmentTreeNode(start=3, end=3, val=3) + SegmentTreeNode(start=4, end=4, val=4) + SegmentTreeNode(start=0, end=0, val=2) + SegmentTreeNode(start=1, end=1, val=5) >>> >>> num_arr.query_range(3, 4) 7 @@ -62,29 +62,29 @@ class SegmentTree: >>> for node in max_arr.traverse(): ... print(node) ... - - - - - - - - - + SegmentTreeNode(start=0, end=4, val=5) + SegmentTreeNode(start=0, end=2, val=5) + SegmentTreeNode(start=3, end=4, val=4) + SegmentTreeNode(start=0, end=1, val=2) + SegmentTreeNode(start=2, end=2, val=5) + SegmentTreeNode(start=3, end=3, val=3) + SegmentTreeNode(start=4, end=4, val=4) + SegmentTreeNode(start=0, end=0, val=2) + SegmentTreeNode(start=1, end=1, val=1) >>> >>> max_arr.update(1, 5) >>> for node in max_arr.traverse(): ... print(node) ... - - - - - - - - - + SegmentTreeNode(start=0, end=4, val=5) + SegmentTreeNode(start=0, end=2, val=5) + SegmentTreeNode(start=3, end=4, val=4) + SegmentTreeNode(start=0, end=1, val=5) + SegmentTreeNode(start=2, end=2, val=5) + SegmentTreeNode(start=3, end=3, val=3) + SegmentTreeNode(start=4, end=4, val=4) + SegmentTreeNode(start=0, end=0, val=2) + SegmentTreeNode(start=1, end=1, val=5) >>> >>> max_arr.query_range(3, 4) 4 @@ -97,29 +97,29 @@ class SegmentTree: >>> for node in min_arr.traverse(): ... print(node) ... - - - - - - - - - + SegmentTreeNode(start=0, end=4, val=1) + SegmentTreeNode(start=0, end=2, val=1) + SegmentTreeNode(start=3, end=4, val=3) + SegmentTreeNode(start=0, end=1, val=1) + SegmentTreeNode(start=2, end=2, val=5) + SegmentTreeNode(start=3, end=3, val=3) + SegmentTreeNode(start=4, end=4, val=4) + SegmentTreeNode(start=0, end=0, val=2) + SegmentTreeNode(start=1, end=1, val=1) >>> >>> min_arr.update(1, 5) >>> for node in min_arr.traverse(): ... print(node) ... - - - - - - - - - + SegmentTreeNode(start=0, end=4, val=2) + SegmentTreeNode(start=0, end=2, val=2) + SegmentTreeNode(start=3, end=4, val=3) + SegmentTreeNode(start=0, end=1, val=2) + SegmentTreeNode(start=2, end=2, val=5) + SegmentTreeNode(start=3, end=3, val=3) + SegmentTreeNode(start=4, end=4, val=4) + SegmentTreeNode(start=0, end=0, val=2) + SegmentTreeNode(start=1, end=1, val=5) >>> >>> min_arr.query_range(3, 4) 3 diff --git a/data_structures/binary_tree/wavelet_tree.py b/data_structures/binary_tree/wavelet_tree.py index 4e3759e945a5..041e140f5b15 100644 --- a/data_structures/binary_tree/wavelet_tree.py +++ b/data_structures/binary_tree/wavelet_tree.py @@ -24,11 +24,11 @@ def __repr__(self) -> str: """ >>> node = Node(length=27) >>> repr(node) - '' + 'Node(min_value=-1 max_value=-1)' >>> repr(node) == str(node) True """ - return f"" + return f"Node(min_value={self.minn} max_value={self.maxx})" def build_tree(arr: list[int]) -> Node | None: @@ -37,7 +37,7 @@ def build_tree(arr: list[int]) -> Node | None: of the constructed tree >>> build_tree(test_array) - + Node(min_value=0 max_value=9) """ root = Node(len(arr)) root.minn, root.maxx = min(arr), max(arr) diff --git a/matrix/cramers_rule_2x2.py b/matrix/cramers_rule_2x2.py index 67dbfdcd6246..2199103a4dec 100644 --- a/matrix/cramers_rule_2x2.py +++ b/matrix/cramers_rule_2x2.py @@ -2,7 +2,7 @@ # https://en.wikipedia.org/wiki/Cramer%27s_rule -def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float]: +def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float, float]: """ Solves the system of linear equation in 2 variables. :param: equation1: list of 3 numbers @@ -76,7 +76,7 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float] else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) - return (0, 0) + return (0.0, 0.0) else: x = determinant_x / determinant y = determinant_y / determinant From 5f347e96516db8908c86746dc94a0462933114cb Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 2 Nov 2022 17:10:37 +0000 Subject: [PATCH 07/12] chore: Fix failing tests --- linear_algebra/src/polynom_for_points.py | 8 ++++++-- matrix/cramers_rule_2x2.py | 2 +- other/password.py | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/linear_algebra/src/polynom_for_points.py b/linear_algebra/src/polynom_for_points.py index 21d66ac4908a..065ac78670d5 100644 --- a/linear_algebra/src/polynom_for_points.py +++ b/linear_algebra/src/polynom_for_points.py @@ -4,9 +4,13 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str: number of points you want to use >>> print(points_to_polynomial([])) - The program cannot work out a fitting polynomial. + Traceback (most recent call last): + ... + ValueError: The program cannot work out a fitting polynomial. >>> print(points_to_polynomial([[]])) - The program cannot work out a fitting polynomial. + Traceback (most recent call last): + ... + ValueError: The program cannot work out a fitting polynomial. >>> print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) f(x)=x^2*0.0+x^1*-0.0+x^0*0.0 >>> print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) diff --git a/matrix/cramers_rule_2x2.py b/matrix/cramers_rule_2x2.py index 2199103a4dec..4f52dbe646ad 100644 --- a/matrix/cramers_rule_2x2.py +++ b/matrix/cramers_rule_2x2.py @@ -14,7 +14,7 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float, determinant_y = [[a1, d1], [a2, d2]] >>> cramers_rule_2x2([2, 3, 0], [5, 1, 0]) - (0, 0) + (0.0, 0.0) >>> cramers_rule_2x2([0, 4, 50], [2, 0, 26]) (13.0, 12.5) >>> cramers_rule_2x2([11, 2, 30], [1, 0, 4]) diff --git a/other/password.py b/other/password.py index f1764b7ec168..f463c7564536 100644 --- a/other/password.py +++ b/other/password.py @@ -70,13 +70,13 @@ def is_strong_password(password: str, min_length: int = 8) -> bool: """ >>> is_strong_password('Hwea7$2!') True - >>> strong_password_detector('Sh0r1') + >>> is_strong_password('Sh0r1') False - >>> strong_password_detector('Hello123') + >>> is_strong_password('Hello123') False - >>> strong_password_detector('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1') + >>> is_strong_password('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1') True - >>> strong_password_detector('0') + >>> is_strong_password('0') False """ From 34a1d64106e865d1953aa861a77aad0da451996a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 17:11:35 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/src/polynom_for_points.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linear_algebra/src/polynom_for_points.py b/linear_algebra/src/polynom_for_points.py index 065ac78670d5..cf209b8e9ab3 100644 --- a/linear_algebra/src/polynom_for_points.py +++ b/linear_algebra/src/polynom_for_points.py @@ -4,11 +4,11 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str: number of points you want to use >>> print(points_to_polynomial([])) - Traceback (most recent call last): + Traceback (most recent call last): ... ValueError: The program cannot work out a fitting polynomial. >>> print(points_to_polynomial([[]])) - Traceback (most recent call last): + Traceback (most recent call last): ... ValueError: The program cannot work out a fitting polynomial. >>> print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) From 70252525b8bfc70005469d52fc3600688226f54b Mon Sep 17 00:00:00 2001 From: Caeden Perelli-Harris Date: Wed, 2 Nov 2022 17:42:05 +0000 Subject: [PATCH 09/12] Update data_structures/binary_tree/segment_tree_other.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/segment_tree_other.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/data_structures/binary_tree/segment_tree_other.py b/data_structures/binary_tree/segment_tree_other.py index 409d3464975c..55c65df24282 100644 --- a/data_structures/binary_tree/segment_tree_other.py +++ b/data_structures/binary_tree/segment_tree_other.py @@ -24,9 +24,11 @@ class SegmentTree: """ >>> import operator >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) - >>> for node in num_arr.traverse(): - ... print(node) - ... + >>> tuple(num_arr.traverse()) # doctest: +NORMALIZE_WHITESPACE + ( + SegmentTreeNode(start=0, end=4, val=15), + [ ... ] + ) SegmentTreeNode(start=0, end=4, val=15) SegmentTreeNode(start=0, end=2, val=8) SegmentTreeNode(start=3, end=4, val=7) From 53a925933784df131d43b357d381e772e2ec7bf1 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 6 Nov 2022 11:31:07 +0000 Subject: [PATCH 10/12] test: Fix doctests --- .../binary_tree/segment_tree_other.py | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/data_structures/binary_tree/segment_tree_other.py b/data_structures/binary_tree/segment_tree_other.py index 55c65df24282..cc77c4951f1a 100644 --- a/data_structures/binary_tree/segment_tree_other.py +++ b/data_structures/binary_tree/segment_tree_other.py @@ -25,33 +25,27 @@ class SegmentTree: >>> import operator >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) >>> tuple(num_arr.traverse()) # doctest: +NORMALIZE_WHITESPACE - ( - SegmentTreeNode(start=0, end=4, val=15), - [ ... ] - ) - SegmentTreeNode(start=0, end=4, val=15) - SegmentTreeNode(start=0, end=2, val=8) - SegmentTreeNode(start=3, end=4, val=7) - SegmentTreeNode(start=0, end=1, val=3) - SegmentTreeNode(start=2, end=2, val=5) - SegmentTreeNode(start=3, end=3, val=3) - SegmentTreeNode(start=4, end=4, val=4) - SegmentTreeNode(start=0, end=0, val=2) - SegmentTreeNode(start=1, end=1, val=1) + (SegmentTreeNode(start=0, end=4, val=15), + SegmentTreeNode(start=0, end=2, val=8), + SegmentTreeNode(start=3, end=4, val=7), + SegmentTreeNode(start=0, end=1, val=3), + SegmentTreeNode(start=2, end=2, val=5), + SegmentTreeNode(start=3, end=3, val=3), + SegmentTreeNode(start=4, end=4, val=4), + SegmentTreeNode(start=0, end=0, val=2), + SegmentTreeNode(start=1, end=1, val=1)) >>> >>> num_arr.update(1, 5) - >>> for node in num_arr.traverse(): - ... print(node) - ... - SegmentTreeNode(start=0, end=4, val=19) - SegmentTreeNode(start=0, end=2, val=12) - SegmentTreeNode(start=3, end=4, val=7) - SegmentTreeNode(start=0, end=1, val=7) - SegmentTreeNode(start=2, end=2, val=5) - SegmentTreeNode(start=3, end=3, val=3) - SegmentTreeNode(start=4, end=4, val=4) - SegmentTreeNode(start=0, end=0, val=2) - SegmentTreeNode(start=1, end=1, val=5) + >>> tuple(num_arr.traverse()) # doctest: +NORMALIZE_WHITESPACE + (SegmentTreeNode(start=0, end=4, val=19), + SegmentTreeNode(start=0, end=2, val=12), + SegmentTreeNode(start=3, end=4, val=7), + SegmentTreeNode(start=0, end=1, val=7), + SegmentTreeNode(start=2, end=2, val=5), + SegmentTreeNode(start=3, end=3, val=3), + SegmentTreeNode(start=4, end=4, val=4), + SegmentTreeNode(start=0, end=0, val=2), + SegmentTreeNode(start=1, end=1, val=5)) >>> >>> num_arr.query_range(3, 4) 7 @@ -130,7 +124,6 @@ class SegmentTree: >>> min_arr.query_range(1, 3) 3 >>> - """ def __init__(self, collection: Sequence, function): From 5b18251b4ef4d3356d29eefab558b458098a2162 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 6 Nov 2022 15:38:28 +0100 Subject: [PATCH 11/12] random.choice(population_score[:N_SELECTED])[0] --- genetic_algorithm/basic_string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index 7c9b31f18b8b..9a2c60e57dfc 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -129,7 +129,7 @@ def select(parent_1: tuple[str, float]) -> list[str]: child_n = int(parent_1[1] * 100) + 1 child_n = 10 if child_n >= 10 else child_n for _ in range(child_n): - parent_2 = population_score[random.randint(0, N_SELECTED)][0] + parent_2 = random.choice(population_score[:N_SELECTED])[0] child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list. From 8cab3ebd0a946a0da46ca692774d90f234effd18 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 6 Nov 2022 15:48:00 +0100 Subject: [PATCH 12/12] Update basic_string.py --- genetic_algorithm/basic_string.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index 9a2c60e57dfc..45b8be651f6e 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -129,7 +129,9 @@ def select(parent_1: tuple[str, float]) -> list[str]: child_n = int(parent_1[1] * 100) + 1 child_n = 10 if child_n >= 10 else child_n for _ in range(child_n): - parent_2 = random.choice(population_score[:N_SELECTED])[0] + parent_2 = population_score[ # noqa: B023 + random.randint(0, N_SELECTED) + ][0] child_1, child_2 = crossover(parent_1[0], parent_2) # Append new string to the population list.