From fb6bf62a60aee830417e6084018782012d0b309c Mon Sep 17 00:00:00 2001 From: Rainethhh Date: Tue, 21 Dec 2021 20:54:28 -0700 Subject: [PATCH 1/7] commit on 'shortest_path_sum' --- graphs/minimum_path_sum.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 graphs/minimum_path_sum.py diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py new file mode 100644 index 000000000000..49958625a4b5 --- /dev/null +++ b/graphs/minimum_path_sum.py @@ -0,0 +1,5 @@ +def main(): + print("Hello world!") + +if __name__ == "__main__": + main() \ No newline at end of file From b6ef3b3d282f2bc5252a04de0a190b37d5d4e047 Mon Sep 17 00:00:00 2001 From: Rainethhh Date: Thu, 23 Dec 2021 10:58:25 -0700 Subject: [PATCH 2/7] minimum_path_sum updated --- graphs/minimum_path_sum.py | 61 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py index 49958625a4b5..bfba0117ac89 100644 --- a/graphs/minimum_path_sum.py +++ b/graphs/minimum_path_sum.py @@ -1,5 +1,60 @@ -def main(): - print("Hello world!") +def min_path(grid): + + """ + >>> min_path([ + ... [1, 3, 1], + ... [1, 5, 1], + ... [4, 2, 1], + ... ]) + 7 + + >>> min_path([ + ... [1, 0, 5, 6, 7], + ... [8, 9, 0, 4, 2], + ... [4, 4, 4, 5, 1], + ... [9, 6, 3, 1, 0], + ... [8, 4, 3, 2, 7], + ... ]) + 20 + + >>> min_path(None) + Traceback (most recent call last): + ... + TypeError: The grid does not contain the appropriate information + + >>> min_path([[]]) + Traceback (most recent call last): + ... + TypeError: The grid does not contain the appropriate information + """ + + if not grid or not grid[0]: + raise TypeError("The grid does not contain the appropriate information") + + for cell_n in range(1, len(grid[0])): + grid[0][cell_n] += grid[0][cell_n - 1] + row_above = grid[0] + + for row_n in range(1, len(grid)): + current_row = grid[row_n] + grid[row_n] = fill_row(current_row, row_above) + row_above = grid[row_n] + + return grid[-1][-1] + + +def fill_row(current_row, row_above): + if row_above is None: + return current_row + + current_row[0] += row_above[0] + for cell_n in range(1, len(current_row)): + current_row[cell_n] += min(current_row[cell_n - 1], row_above[cell_n]) + + return current_row + if __name__ == "__main__": - main() \ No newline at end of file + import doctest + + doctest.testmod() From d33066333b6edf6ecc20804cbe03fc8b86d761b7 Mon Sep 17 00:00:00 2001 From: Rainethhh Date: Thu, 23 Dec 2021 11:02:26 -0700 Subject: [PATCH 3/7] commit to 'minimum_path_sum' --- graphs/minimum_path_sum.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py index bfba0117ac89..a50d7a3ccb05 100644 --- a/graphs/minimum_path_sum.py +++ b/graphs/minimum_path_sum.py @@ -44,8 +44,6 @@ def min_path(grid): def fill_row(current_row, row_above): - if row_above is None: - return current_row current_row[0] += row_above[0] for cell_n in range(1, len(current_row)): From 966fe8d6dda81e2376dbc10e5754e677973d6f36 Mon Sep 17 00:00:00 2001 From: Rainethhh Date: Fri, 24 Dec 2021 15:26:24 -0700 Subject: [PATCH 4/7] added description to minimum_path_sum --- graphs/minimum_path_sum.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py index a50d7a3ccb05..faf104c2e107 100644 --- a/graphs/minimum_path_sum.py +++ b/graphs/minimum_path_sum.py @@ -1,6 +1,8 @@ def min_path(grid): """ + Find the path from top left to bottom right of array of numbers + with the lowest possible sum and return the sum along this path. >>> min_path([ ... [1, 3, 1], ... [1, 5, 1], From da91a3e4dff175f1dd687d3b6dcfa16516e44baf Mon Sep 17 00:00:00 2001 From: Rainethhh <64663183+Rainethhh@users.noreply.github.com> Date: Fri, 24 Dec 2021 15:49:19 -0700 Subject: [PATCH 5/7] bot requirements fixed for --- graphs/minimum_path_sum.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py index faf104c2e107..cfe8acf47428 100644 --- a/graphs/minimum_path_sum.py +++ b/graphs/minimum_path_sum.py @@ -1,4 +1,4 @@ -def min_path(grid): +def min_path(grid: list) -> int: """ Find the path from top left to bottom right of array of numbers @@ -45,7 +45,11 @@ def min_path(grid): return grid[-1][-1] -def fill_row(current_row, row_above): +def fill_row(current_row: list, row_above: list) -> list: + """ + >>> fill_row([2, 2, 2], [1, 2, 3]) + [3, 4, 5] + """ current_row[0] += row_above[0] for cell_n in range(1, len(current_row)): From 2dde88f575c02775d3931f723f8a0a0e31fc85d0 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 25 May 2022 13:25:26 +0800 Subject: [PATCH 6/7] Update minimum_path_sum.py --- graphs/minimum_path_sum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py index cfe8acf47428..d241ea170c9e 100644 --- a/graphs/minimum_path_sum.py +++ b/graphs/minimum_path_sum.py @@ -1,5 +1,4 @@ def min_path(grid: list) -> int: - """ Find the path from top left to bottom right of array of numbers with the lowest possible sum and return the sum along this path. From a77cb2f41ca1a9e184aa65d364f77ba0f90eb53f Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 25 May 2022 13:30:15 +0800 Subject: [PATCH 7/7] Update minimum_path_sum.py --- graphs/minimum_path_sum.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/graphs/minimum_path_sum.py b/graphs/minimum_path_sum.py index d241ea170c9e..df1e545df3d0 100644 --- a/graphs/minimum_path_sum.py +++ b/graphs/minimum_path_sum.py @@ -1,15 +1,15 @@ -def min_path(grid: list) -> int: +def min_path_sum(grid: list) -> int: """ Find the path from top left to bottom right of array of numbers with the lowest possible sum and return the sum along this path. - >>> min_path([ + >>> min_path_sum([ ... [1, 3, 1], ... [1, 5, 1], ... [4, 2, 1], ... ]) 7 - >>> min_path([ + >>> min_path_sum([ ... [1, 0, 5, 6, 7], ... [8, 9, 0, 4, 2], ... [4, 4, 4, 5, 1], @@ -18,12 +18,12 @@ def min_path(grid: list) -> int: ... ]) 20 - >>> min_path(None) + >>> min_path_sum(None) Traceback (most recent call last): ... TypeError: The grid does not contain the appropriate information - >>> min_path([[]]) + >>> min_path_sum([[]]) Traceback (most recent call last): ... TypeError: The grid does not contain the appropriate information