From 2daf7c3dc668b4b3c829cd4252c177810bb46ce0 Mon Sep 17 00:00:00 2001 From: Scarfinos Date: Wed, 6 Nov 2024 16:17:58 +0100 Subject: [PATCH 1/8] #9943 : Adding coverage test for basic_graphs.py --- graphs/basic_graphs.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index 25c8045b3d2b..73861a01a435 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -8,6 +8,15 @@ def _input(message): def initialize_unweighted_directed_graph( node_count: int, edge_count: int ) -> dict[int, list[int]]: + """ + Example: + Edge 1: 1 2 + Edge 2: 3 2 + Edge 3: 2 4 + >>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2','3 2', '2 4'])) # input + >>> initialize_unweighted_directed_graph(4, 3) + Edge 1: Edge 2: Edge 3: {1: [2], 2: [4], 3: [2], 4: []} + """ graph: dict[int, list[int]] = {} for i in range(node_count): graph[i + 1] = [] @@ -21,6 +30,15 @@ def initialize_unweighted_directed_graph( def initialize_unweighted_undirected_graph( node_count: int, edge_count: int ) -> dict[int, list[int]]: + """ + Example: + Edge 1: 1 2 + Edge 2: 3 2 + Edge 3: 2 4 + >>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2','3 2', '2 4'])) # input + >>> initialize_unweighted_undirected_graph(4, 3) + Edge 1: Edge 2: Edge 3: {1: [2], 2: [1, 3, 4], 3: [2], 4: [2]} + """ graph: dict[int, list[int]] = {} for i in range(node_count): graph[i + 1] = [] @@ -35,6 +53,17 @@ def initialize_unweighted_undirected_graph( def initialize_weighted_undirected_graph( node_count: int, edge_count: int ) -> dict[int, list[tuple[int, int]]]: + """ + Example: + Edge 1: 1 2 1 + Edge 2: 3 2 6 + Edge 3: 2 4 10 + Edge 4: 4 1 7 + Edge 5: 4 3 12 + >>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2 1','3 2 6', '2 4 10', '4 1 7', '4 3 12'])) # input + >>> initialize_weighted_undirected_graph(4, 5) + Edge 1: Edge 2: Edge 3: Edge 4: Edge 5: {1: [(2, 1), (4, 7)], 2: [(1, 1), (3, 6), (4, 10)], 3: [(2, 6), (4, 12)], 4: [(2, 10), (1, 7), (3, 12)]} + """ graph: dict[int, list[tuple[int, int]]] = {} for i in range(node_count): graph[i + 1] = [] @@ -77,6 +106,14 @@ def initialize_weighted_undirected_graph( def dfs(g, s): + """ + >>> dfs({1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}, 1) + 1 + 2 + 4 + 5 + 3 + """ vis, _s = {s}, [s] print(s) while _s: @@ -104,6 +141,17 @@ def dfs(g, s): def bfs(g, s): + """ + >>> bfs({1: [2, 3], 2: [4, 5], 3: [6, 7], 4: [], 5: [8], 6: [], 7: [], 8: []}, 1) + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + """ vis, q = {s}, deque([s]) print(s) while q: @@ -128,6 +176,14 @@ def bfs(g, s): def dijk(g, s): + """ + dijk({1: [(2, 7), (3, 9), (6, 14)], 2: [(1, 7), (3, 10), (4, 15)], 3: [(1, 9), (2, 10), (4, 11), (6, 2)], 4: [(2, 15), (3, 11), (5, 6)], 5: [(4, 6), (6, 9)], 6: [(1, 14), (3, 2), (5, 9)]}, 1) + 7 + 9 + 11 + 20 + 20 + """ dist, known, path = {s: 0}, set(), {s: 0} while True: if len(known) == len(g) - 1: From 5a8f4462815bee08e7d1f66ba1037ca52d7dfe46 Mon Sep 17 00:00:00 2001 From: Scarfinos Date: Wed, 6 Nov 2024 16:31:41 +0100 Subject: [PATCH 2/8] #9943 : Adding coverage test for basic_graphs.py --- graphs/basic_graphs.py | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index 73861a01a435..08431e832442 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -8,15 +8,7 @@ def _input(message): def initialize_unweighted_directed_graph( node_count: int, edge_count: int ) -> dict[int, list[int]]: - """ - Example: - Edge 1: 1 2 - Edge 2: 3 2 - Edge 3: 2 4 - >>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2','3 2', '2 4'])) # input - >>> initialize_unweighted_directed_graph(4, 3) - Edge 1: Edge 2: Edge 3: {1: [2], 2: [4], 3: [2], 4: []} - """ + graph: dict[int, list[int]] = {} for i in range(node_count): graph[i + 1] = [] @@ -30,15 +22,7 @@ def initialize_unweighted_directed_graph( def initialize_unweighted_undirected_graph( node_count: int, edge_count: int ) -> dict[int, list[int]]: - """ - Example: - Edge 1: 1 2 - Edge 2: 3 2 - Edge 3: 2 4 - >>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2','3 2', '2 4'])) # input - >>> initialize_unweighted_undirected_graph(4, 3) - Edge 1: Edge 2: Edge 3: {1: [2], 2: [1, 3, 4], 3: [2], 4: [2]} - """ + graph: dict[int, list[int]] = {} for i in range(node_count): graph[i + 1] = [] @@ -53,17 +37,7 @@ def initialize_unweighted_undirected_graph( def initialize_weighted_undirected_graph( node_count: int, edge_count: int ) -> dict[int, list[tuple[int, int]]]: - """ - Example: - Edge 1: 1 2 1 - Edge 2: 3 2 6 - Edge 3: 2 4 10 - Edge 4: 4 1 7 - Edge 5: 4 3 12 - >>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2 1','3 2 6', '2 4 10', '4 1 7', '4 3 12'])) # input - >>> initialize_weighted_undirected_graph(4, 5) - Edge 1: Edge 2: Edge 3: Edge 4: Edge 5: {1: [(2, 1), (4, 7)], 2: [(1, 1), (3, 6), (4, 10)], 3: [(2, 6), (4, 12)], 4: [(2, 10), (1, 7), (3, 12)]} - """ + graph: dict[int, list[tuple[int, int]]] = {} for i in range(node_count): graph[i + 1] = [] From 111bd6b6ee879aef75b0022ad9d1f2995603145b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:35:14 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/basic_graphs.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index 08431e832442..c92fb6d7a640 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -8,7 +8,6 @@ def _input(message): def initialize_unweighted_directed_graph( node_count: int, edge_count: int ) -> dict[int, list[int]]: - graph: dict[int, list[int]] = {} for i in range(node_count): graph[i + 1] = [] @@ -22,7 +21,6 @@ def initialize_unweighted_directed_graph( def initialize_unweighted_undirected_graph( node_count: int, edge_count: int ) -> dict[int, list[int]]: - graph: dict[int, list[int]] = {} for i in range(node_count): graph[i + 1] = [] @@ -37,7 +35,6 @@ def initialize_unweighted_undirected_graph( def initialize_weighted_undirected_graph( node_count: int, edge_count: int ) -> dict[int, list[tuple[int, int]]]: - graph: dict[int, list[tuple[int, int]]] = {} for i in range(node_count): graph[i + 1] = [] From dcbab8eee91d725c68dad5941707d26b125488e8 Mon Sep 17 00:00:00 2001 From: Scarfinos Date: Wed, 6 Nov 2024 16:43:13 +0100 Subject: [PATCH 4/8] Solve problem of line too long --- graphs/basic_graphs.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index 08431e832442..314a48a8ba19 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -151,7 +151,12 @@ def bfs(g, s): def dijk(g, s): """ - dijk({1: [(2, 7), (3, 9), (6, 14)], 2: [(1, 7), (3, 10), (4, 15)], 3: [(1, 9), (2, 10), (4, 11), (6, 2)], 4: [(2, 15), (3, 11), (5, 6)], 5: [(4, 6), (6, 9)], 6: [(1, 14), (3, 2), (5, 9)]}, 1) + dijk({1: [(2, 7), (3, 9), (6, 14)], + 2: [(1, 7), (3, 10), (4, 15)], + 3: [(1, 9), (2, 10), (4, 11), (6, 2)], + 4: [(2, 15), (3, 11), (5, 6)], + 5: [(4, 6), (6, 9)], + 6: [(1, 14), (3, 2), (5, 9)]}, 1) 7 9 11 From 3136833d4a1bc014309763221a3ee3a67731611e Mon Sep 17 00:00:00 2001 From: Scarfinos Date: Wed, 20 Nov 2024 12:28:43 +0100 Subject: [PATCH 5/8] Improving coverage for matrix_exponentiation.py --- maths/matrix_exponentiation.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py index 7c37151c87ca..18570f3899aa 100644 --- a/maths/matrix_exponentiation.py +++ b/maths/matrix_exponentiation.py @@ -39,6 +39,21 @@ def modular_exponentiation(a, b): def fibonacci_with_matrix_exponentiation(n, f1, f2): + """ + Returns the n number of the Fibonacci sequence that + start with f1 and f2 + Use the matrix exponentiation + >>> fibonacci_with_matrix_exponentiation(1, 5, 6) + 5 + >>> fibonacci_with_matrix_exponentiation(2, 10, 11) + 11 + >>> fibonacci_with_matrix_exponentiation(13, 0, 1) + 144 + >>> fibonacci_with_matrix_exponentiation(10, 5, 9) + 411 + >>> fibonacci_with_matrix_exponentiation(9, 2, 3) + 89 + """ # Trivial Cases if n == 1: return f1 @@ -50,6 +65,21 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2): def simple_fibonacci(n, f1, f2): + """ + Returns the n number of the Fibonacci sequence that + start with f1 and f2 + Use the definition + >>> simple_fibonacci(1, 5, 6) + 5 + >>> simple_fibonacci(2, 10, 11) + 11 + >>> simple_fibonacci(13, 0, 1) + 144 + >>> simple_fibonacci(10, 5, 9) + 411 + >>> simple_fibonacci(9, 2, 3) + 89 + """ # Trivial Cases if n == 1: return f1 @@ -61,10 +91,10 @@ def simple_fibonacci(n, f1, f2): n -= 2 while n > 0: - fn_1, fn_2 = fn_1 + fn_2, fn_1 + fn_2, fn_1 = fn_1 + fn_2, fn_2 n -= 1 - return fn_1 + return fn_2 def matrix_exponentiation_time(): From eeeae8463191428ca3320af8a52d836dcc611b25 Mon Sep 17 00:00:00 2001 From: Scarfinos Date: Wed, 20 Nov 2024 12:37:03 +0100 Subject: [PATCH 6/8] fix more than one file --- graphs/basic_graphs.py | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index f8643af1cfce..25c8045b3d2b 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -77,14 +77,6 @@ def initialize_weighted_undirected_graph( def dfs(g, s): - """ - >>> dfs({1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}, 1) - 1 - 2 - 4 - 5 - 3 - """ vis, _s = {s}, [s] print(s) while _s: @@ -112,17 +104,6 @@ def dfs(g, s): def bfs(g, s): - """ - >>> bfs({1: [2, 3], 2: [4, 5], 3: [6, 7], 4: [], 5: [8], 6: [], 7: [], 8: []}, 1) - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - """ vis, q = {s}, deque([s]) print(s) while q: @@ -147,19 +128,6 @@ def bfs(g, s): def dijk(g, s): - """ - dijk({1: [(2, 7), (3, 9), (6, 14)], - 2: [(1, 7), (3, 10), (4, 15)], - 3: [(1, 9), (2, 10), (4, 11), (6, 2)], - 4: [(2, 15), (3, 11), (5, 6)], - 5: [(4, 6), (6, 9)], - 6: [(1, 14), (3, 2), (5, 9)]}, 1) - 7 - 9 - 11 - 20 - 20 - """ dist, known, path = {s: 0}, set(), {s: 0} while True: if len(known) == len(g) - 1: From 4ada6a20f445f3a1251b16e541f3bb4ac2936e0d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 28 Dec 2024 01:34:03 +0300 Subject: [PATCH 7/8] Update matrix_exponentiation.py --- maths/matrix_exponentiation.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py index 18570f3899aa..98feca6006af 100644 --- a/maths/matrix_exponentiation.py +++ b/maths/matrix_exponentiation.py @@ -40,9 +40,9 @@ def modular_exponentiation(a, b): def fibonacci_with_matrix_exponentiation(n, f1, f2): """ - Returns the n number of the Fibonacci sequence that - start with f1 and f2 - Use the matrix exponentiation + Returns the nth number of the Fibonacci sequence that + starts with f1 and f2 + Uses the matrix exponentiation >>> fibonacci_with_matrix_exponentiation(1, 5, 6) 5 >>> fibonacci_with_matrix_exponentiation(2, 10, 11) @@ -66,9 +66,9 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2): def simple_fibonacci(n, f1, f2): """ - Returns the n number of the Fibonacci sequence that - start with f1 and f2 - Use the definition + Returns the nth number of the Fibonacci sequence that + starts with f1 and f2 + Uses the definition >>> simple_fibonacci(1, 5, 6) 5 >>> simple_fibonacci(2, 10, 11) From d5a423b14ae19ae72cc0d12d967d8e6d29245e49 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 28 Dec 2024 01:36:30 +0300 Subject: [PATCH 8/8] Update matrix_exponentiation.py --- maths/matrix_exponentiation.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py index 98feca6006af..7cdac9d34674 100644 --- a/maths/matrix_exponentiation.py +++ b/maths/matrix_exponentiation.py @@ -86,15 +86,13 @@ def simple_fibonacci(n, f1, f2): elif n == 2: return f2 - fn_1 = f1 - fn_2 = f2 n -= 2 while n > 0: - fn_2, fn_1 = fn_1 + fn_2, fn_2 + f2, f1 = f1 + f2, f2 n -= 1 - return fn_2 + return f2 def matrix_exponentiation_time():