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..16271f6d63a6 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,9 @@ def bfs_time(self, s=-2): self.bfs(s) end = time() return end - begin + + +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/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)