From 4a414699ebd91978abc9eab6b742d8be638d0147 Mon Sep 17 00:00:00 2001 From: zirtidik Date: Tue, 3 Dec 2024 18:18:35 -0500 Subject: [PATCH 1/5] first test --- .vscode/settings.json | 7 ++- .../directed_and_undirected_weighted_graph.py | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ef16fa1aa7ac..72c0a22447b6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,10 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "master" - ] + ], + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true } diff --git a/graphs/directed_and_undirected_weighted_graph.py b/graphs/directed_and_undirected_weighted_graph.py index 8ca645fdace8..e65c500a1a63 100644 --- a/graphs/directed_and_undirected_weighted_graph.py +++ b/graphs/directed_and_undirected_weighted_graph.py @@ -8,12 +8,37 @@ class DirectedGraph: def __init__(self): + """ + Initialize an empty directed graph. + + Example: + >>> g = DirectedGraph() + >>> g.graph + {} + """ self.graph = {} # adding vertices and edges # adding the weight is optional # handles repetition def add_pair(self, u, v, w=1): + """ + Add a directed edge with an optional weight. + + Args: + u: Starting node. + v: Destination node. + w: Weight (default is 1). + + Example: + >>> g = DirectedGraph() + >>> g.add_pair(1, 2) + >>> g.graph + {1: [[1, 2]], 2: []} + >>> g.add_pair(2, 3, 5) + >>> g.graph + {1: [[1, 2]], 2: [[5, 3]], 3: []} + """ if self.graph.get(u): if self.graph[u].count([w, v]) == 0: self.graph[u].append([w, v]) @@ -27,6 +52,21 @@ def all_nodes(self): # handles if the input does not exist def remove_pair(self, u, v): + """ + Remove a directed edge from u to v. + + Args: + u: Starting node. + v: Destination node. + + Example: + >>> g = DirectedGraph() + >>> g.add_pair(1, 2) + >>> g.add_pair(1, 3) + >>> g.remove_pair(1, 2) + >>> g.graph + {1: [[1, 3]], 2: [], 3: []} + """ if self.graph.get(u): for _ in self.graph[u]: if _[1] == v: @@ -487,3 +527,8 @@ def bfs_time(self, s=-2): self.bfs(s) end = time() return end - begin + + +if __name__ == "__main__": + import doctest + doctest.testmod() From f3bfc4f5a719022142c5050ca9bfbb1c3284f1f3 Mon Sep 17 00:00:00 2001 From: zirtidik Date: Tue, 3 Dec 2024 18:22:07 -0500 Subject: [PATCH 2/5] added tests to 10 - 138 --- graphs/directed_and_undirected_weighted_graph.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/graphs/directed_and_undirected_weighted_graph.py b/graphs/directed_and_undirected_weighted_graph.py index e65c500a1a63..a0f0c9a56ffa 100644 --- a/graphs/directed_and_undirected_weighted_graph.py +++ b/graphs/directed_and_undirected_weighted_graph.py @@ -114,6 +114,20 @@ def dfs(self, s=-2, d=-1): # c is the count of nodes you want and if you leave it or pass -1 to the function # the count will be random from 10 to 10000 def fill_graph_randomly(self, c=-1): + """ + Fill the graph with random nodes and edges. + + Args: + c: Number of nodes to generate. If -1, a random number of nodes between 10 and 10000 is used. + + Example: + >>> g = DirectedGraph() + >>> g.fill_graph_randomly(5) # Fill graph with 5 nodes and random edges. + >>> len(g.graph) # Check the number of nodes added. + 5 + >>> all(isinstance(node, int) and isinstance(edges, list) for node, edges in g.graph.items()) + True # All nodes should be integers, and edges should be lists. + """ if c == -1: c = floor(random() * 10000) + 10 for i in range(c): @@ -531,4 +545,4 @@ def bfs_time(self, s=-2): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(verbose=True) From 0aa8e1912c0510dff5ddbe320db620467f336e78 Mon Sep 17 00:00:00 2001 From: zirtidik Date: Tue, 3 Dec 2024 18:41:24 -0500 Subject: [PATCH 3/5] changed formating to match reqs. --- digital_image_processing/filters/gabor_filter.py | 6 +++--- graphs/directed_and_undirected_weighted_graph.py | 3 ++- greedy_methods/fractional_knapsack.py | 8 +++++--- machine_learning/frequent_pattern_growth.py | 4 +++- matrix/matrix_class.py | 8 +++++--- sorts/bead_sort.py | 4 +++- strings/min_cost_string_conversion.py | 4 +++- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/digital_image_processing/filters/gabor_filter.py b/digital_image_processing/filters/gabor_filter.py index aaec567f4c99..8f9212a35a79 100644 --- a/digital_image_processing/filters/gabor_filter.py +++ b/digital_image_processing/filters/gabor_filter.py @@ -48,9 +48,9 @@ def gabor_filter_kernel( _y = -sin_theta * px + cos_theta * py # fill kernel - gabor[y, x] = np.exp(-(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)) * np.cos( - 2 * np.pi * _x / lambd + psi - ) + gabor[y, x] = np.exp( + -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) + ) * np.cos(2 * np.pi * _x / lambd + psi) return gabor diff --git a/graphs/directed_and_undirected_weighted_graph.py b/graphs/directed_and_undirected_weighted_graph.py index a0f0c9a56ffa..190fbff65aac 100644 --- a/graphs/directed_and_undirected_weighted_graph.py +++ b/graphs/directed_and_undirected_weighted_graph.py @@ -10,7 +10,7 @@ class DirectedGraph: def __init__(self): """ Initialize an empty directed graph. - + Example: >>> g = DirectedGraph() >>> g.graph @@ -545,4 +545,5 @@ def bfs_time(self, s=-2): if __name__ == "__main__": import doctest + doctest.testmod(verbose=True) diff --git a/greedy_methods/fractional_knapsack.py b/greedy_methods/fractional_knapsack.py index d52b56f23569..f7455a9c9fce 100644 --- a/greedy_methods/fractional_knapsack.py +++ b/greedy_methods/fractional_knapsack.py @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n): return ( 0 if k == 0 - else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) - if k != n - else sum(vl[:k]) + else ( + sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) + if k != n + else sum(vl[:k]) + ) ) diff --git a/machine_learning/frequent_pattern_growth.py b/machine_learning/frequent_pattern_growth.py index fae2df16efb1..a09c96aef5dc 100644 --- a/machine_learning/frequent_pattern_growth.py +++ b/machine_learning/frequent_pattern_growth.py @@ -240,7 +240,9 @@ def ascend_tree(leaf_node: TreeNode, prefix_path: list[str]) -> None: ascend_tree(leaf_node.parent, prefix_path) -def find_prefix_path(base_pat: frozenset, tree_node: TreeNode | None) -> dict: # noqa: ARG001 +def find_prefix_path( + base_pat: frozenset, tree_node: TreeNode | None +) -> dict: # noqa: ARG001 """ Find the conditional pattern base for a given base pattern. diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index a5940a38e836..230eb95006fa 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -204,9 +204,11 @@ def cofactors(self) -> Matrix: return Matrix( [ [ - self.minors().rows[row][column] - if (row + column) % 2 == 0 - else self.minors().rows[row][column] * -1 + ( + self.minors().rows[row][column] + if (row + column) % 2 == 0 + else self.minors().rows[row][column] * -1 + ) for column in range(self.minors().num_columns) ] for row in range(self.minors().num_rows) diff --git a/sorts/bead_sort.py b/sorts/bead_sort.py index 8ce0619fd573..3b94a959caff 100644 --- a/sorts/bead_sort.py +++ b/sorts/bead_sort.py @@ -31,7 +31,9 @@ def bead_sort(sequence: list) -> list: if any(not isinstance(x, int) or x < 0 for x in sequence): raise TypeError("Sequence must be list of non-negative integers") for _ in range(len(sequence)): - for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])): # noqa: RUF007 + for i, (rod_upper, rod_lower) in enumerate( + zip(sequence, sequence[1:]) + ): # noqa: RUF007 if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 93791e2a7ed3..7ee1b190812c 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -132,7 +132,9 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: elif op[0] == "R": string[i] = op[2] - file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))) # noqa: UP031 + file.write( + "%-16s" % ("Replace %c" % op[1] + " with " + str(op[2])) + ) # noqa: UP031 file.write("\t\t" + "".join(string)) file.write("\r\n") From eb16b2dfc4d6033e29d4f7e0b98d5981a1766026 Mon Sep 17 00:00:00 2001 From: zirtidik Date: Tue, 3 Dec 2024 18:52:31 -0500 Subject: [PATCH 4/5] Removed test for later --- graphs/directed_and_undirected_weighted_graph.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/graphs/directed_and_undirected_weighted_graph.py b/graphs/directed_and_undirected_weighted_graph.py index 190fbff65aac..16271f6d63a6 100644 --- a/graphs/directed_and_undirected_weighted_graph.py +++ b/graphs/directed_and_undirected_weighted_graph.py @@ -114,20 +114,6 @@ def dfs(self, s=-2, d=-1): # c is the count of nodes you want and if you leave it or pass -1 to the function # the count will be random from 10 to 10000 def fill_graph_randomly(self, c=-1): - """ - Fill the graph with random nodes and edges. - - Args: - c: Number of nodes to generate. If -1, a random number of nodes between 10 and 10000 is used. - - Example: - >>> g = DirectedGraph() - >>> g.fill_graph_randomly(5) # Fill graph with 5 nodes and random edges. - >>> len(g.graph) # Check the number of nodes added. - 5 - >>> all(isinstance(node, int) and isinstance(edges, list) for node, edges in g.graph.items()) - True # All nodes should be integers, and edges should be lists. - """ if c == -1: c = floor(random() * 10000) + 10 for i in range(c): From 4cf6dbf9a40d1dc94c3ade0f25cd3a59c8033ac4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Dec 2024 23:58:25 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- digital_image_processing/filters/gabor_filter.py | 6 +++--- machine_learning/frequent_pattern_growth.py | 4 +--- sorts/bead_sort.py | 4 +--- strings/min_cost_string_conversion.py | 4 +--- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/digital_image_processing/filters/gabor_filter.py b/digital_image_processing/filters/gabor_filter.py index 8f9212a35a79..aaec567f4c99 100644 --- a/digital_image_processing/filters/gabor_filter.py +++ b/digital_image_processing/filters/gabor_filter.py @@ -48,9 +48,9 @@ def gabor_filter_kernel( _y = -sin_theta * px + cos_theta * py # fill kernel - gabor[y, x] = np.exp( - -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) - ) * np.cos(2 * np.pi * _x / lambd + psi) + gabor[y, x] = np.exp(-(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)) * np.cos( + 2 * np.pi * _x / lambd + psi + ) return gabor diff --git a/machine_learning/frequent_pattern_growth.py b/machine_learning/frequent_pattern_growth.py index a09c96aef5dc..fae2df16efb1 100644 --- a/machine_learning/frequent_pattern_growth.py +++ b/machine_learning/frequent_pattern_growth.py @@ -240,9 +240,7 @@ def ascend_tree(leaf_node: TreeNode, prefix_path: list[str]) -> None: ascend_tree(leaf_node.parent, prefix_path) -def find_prefix_path( - base_pat: frozenset, tree_node: TreeNode | None -) -> dict: # noqa: ARG001 +def find_prefix_path(base_pat: frozenset, tree_node: TreeNode | None) -> dict: # noqa: ARG001 """ Find the conditional pattern base for a given base pattern. diff --git a/sorts/bead_sort.py b/sorts/bead_sort.py index 3b94a959caff..8ce0619fd573 100644 --- a/sorts/bead_sort.py +++ b/sorts/bead_sort.py @@ -31,9 +31,7 @@ def bead_sort(sequence: list) -> list: if any(not isinstance(x, int) or x < 0 for x in sequence): raise TypeError("Sequence must be list of non-negative integers") for _ in range(len(sequence)): - for i, (rod_upper, rod_lower) in enumerate( - zip(sequence, sequence[1:]) - ): # noqa: RUF007 + for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])): # noqa: RUF007 if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 7ee1b190812c..93791e2a7ed3 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -132,9 +132,7 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: elif op[0] == "R": string[i] = op[2] - file.write( - "%-16s" % ("Replace %c" % op[1] + " with " + str(op[2])) - ) # noqa: UP031 + file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))) # noqa: UP031 file.write("\t\t" + "".join(string)) file.write("\r\n")