From b5fcf2c3a41db0435b3e9c4edb2af7aeccfe6859 Mon Sep 17 00:00:00 2001 From: Meysam Azad Date: Fri, 9 Oct 2020 11:32:04 +0330 Subject: [PATCH 1/4] graphs/kruskal: add doctest & type hints this is a child of a previous PR #2443 its ancestor is #2128 --- graphs/minimum_spanning_tree_kruskal.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py index 610baf4b5fe6..d6f241fa6eca 100644 --- a/graphs/minimum_spanning_tree_kruskal.py +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -1,4 +1,17 @@ -def kruskal(num_nodes, num_edges, edges): +from typing import List, Tuple + + +def kruskal(num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]]) -> int: + """ + >>> kruskal(4, 3, [(0, 1, 3), (1, 2, 5), (2, 3, 1)]) + [(2, 3, 1), (0, 1, 3), (1, 2, 5)] + + >>> kruskal(4, 5, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2)]) + [(2, 3, 1), (0, 2, 1), (0, 1, 3)] + + >>> kruskal(4, 6, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2), (2, 1, 1)]) + [(2, 3, 1), (0, 2, 1), (2, 1, 1)] + """ edges = sorted(edges, key=lambda edge: edge[2]) parent = list(range(num_nodes)) From 34bf046742e273224ab93485a399ca124778d8a1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 9 Oct 2020 08:03:19 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index d3a378c3a2ee..fa983699085f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -473,6 +473,7 @@ * [Binary Exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py) * [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py) * [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py) + * [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py) * [Euclidean Gcd](https://github.com/TheAlgorithms/Python/blob/master/other/euclidean_gcd.py) * [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py) * [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py) @@ -529,7 +530,6 @@ * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol2.py) * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol3.py) * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol4.py) - * [Test Solutions](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/test_solutions.py) * Problem 07 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol1.py) * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol2.py) @@ -595,11 +595,11 @@ * Problem 26 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_26/sol1.py) * Problem 27 - * [Problem 27 Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_27/problem_27_sol1.py) + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_27/sol1.py) * Problem 28 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_28/sol1.py) * Problem 29 - * [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py) + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/sol1.py) * Problem 30 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/sol1.py) * Problem 31 @@ -656,6 +656,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py) * Problem 67 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py) + * Problem 71 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_71/sol1.py) * Problem 76 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py) * Problem 97 From 654beeda61812140f51d5b42c7f5f8c0db98655c Mon Sep 17 00:00:00 2001 From: Meysam Azad Date: Fri, 9 Oct 2020 16:13:38 +0330 Subject: [PATCH 3/4] graphs/kruskal: fix max-line-length violation --- graphs/minimum_spanning_tree_kruskal.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py index d6f241fa6eca..5295f4941963 100644 --- a/graphs/minimum_spanning_tree_kruskal.py +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -1,7 +1,9 @@ from typing import List, Tuple -def kruskal(num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]]) -> int: +def kruskal( + num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]] +) -> int: """ >>> kruskal(4, 3, [(0, 1, 3), (1, 2, 5), (2, 3, 1)]) [(2, 3, 1), (0, 1, 3), (1, 2, 5)] @@ -9,7 +11,8 @@ def kruskal(num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]]) - >>> kruskal(4, 5, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2)]) [(2, 3, 1), (0, 2, 1), (0, 1, 3)] - >>> kruskal(4, 6, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2), (2, 1, 1)]) + >>> kruskal(4, 6, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2), + ... (2, 1, 1)]) [(2, 3, 1), (0, 2, 1), (2, 1, 1)] """ edges = sorted(edges, key=lambda edge: edge[2]) From 2ac2f34d2fcf13a9287350c2e1c0b696173947e0 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 9 Oct 2020 12:44:28 +0000 Subject: [PATCH 4/4] fixup! Format Python code with psf/black push --- graphs/minimum_spanning_tree_kruskal.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py index 5295f4941963..a51f970341f7 100644 --- a/graphs/minimum_spanning_tree_kruskal.py +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -1,9 +1,7 @@ from typing import List, Tuple -def kruskal( - num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]] -) -> int: +def kruskal(num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]]) -> int: """ >>> kruskal(4, 3, [(0, 1, 3), (1, 2, 5), (2, 3, 1)]) [(2, 3, 1), (0, 1, 3), (1, 2, 5)]