From e7dca331dc26d36341c1f86e7eccc0b8b4017a08 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 28 Oct 2019 23:46:57 +0800 Subject: [PATCH 1/4] Doctest and comment for maximum sub-array problem More examples and description for max_sub_array.py --- dynamic_programming/max_sub_array.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index eb6ab41bf52d..22817c9c2574 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -43,15 +43,22 @@ def find_max_cross_sum(A, low, mid, high): def max_sub_array(nums: List[int]) -> int: """ - Finds the contiguous subarray (can be empty array) - which has the largest sum and return its sum. + Finds the contiguous subarray + which has the largest sum and return its sum. - >>> max_sub_array([-2,1,-3,4,-1,2,1,-5,4]) + >>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4]) 6 + An empty (sub)array has sum 0. >>> max_sub_array([]) 0 + If all elements are negative, the largest subarray would be the empty array, + having the sum 0. >>> max_sub_array([-1,-2,-3]) 0 + >>> max_sub_array([5,-2,-3]) + 5 + >>> max_sub_array([31, −41, 59, 26, −53, 58, 97, −93, −23, 84]) + 187 """ best = 0 current = 0 @@ -64,6 +71,9 @@ def max_sub_array(nums: List[int]) -> int: if __name__ == "__main__": + """ + A random simulation of this algorithm. + """ inputs = [10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000] tim = [] for i in inputs: From 8c1bbd2d7f05911964558ff9b6a7c47cfd9279c3 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 29 Oct 2019 00:11:19 +0800 Subject: [PATCH 2/4] Update max_sub_array.py --- dynamic_programming/max_sub_array.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index 22817c9c2574..62e1f6846087 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -2,9 +2,6 @@ author : Mayank Kumar Jha (mk9440) """ from typing import List -import time -import matplotlib.pyplot as plt -from random import randint def find_max_sub_array(A, low, high): @@ -48,9 +45,11 @@ def max_sub_array(nums: List[int]) -> int: >>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4]) 6 + An empty (sub)array has sum 0. >>> max_sub_array([]) 0 + If all elements are negative, the largest subarray would be the empty array, having the sum 0. >>> max_sub_array([-1,-2,-3]) @@ -74,6 +73,9 @@ def max_sub_array(nums: List[int]) -> int: """ A random simulation of this algorithm. """ + import time + import matplotlib.pyplot as plt + from random import randint inputs = [10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000] tim = [] for i in inputs: From deb0b927479f06fd7373a92c14478117cb1bcaaf Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 29 Oct 2019 00:12:37 +0800 Subject: [PATCH 3/4] Update max_sub_array.py --- dynamic_programming/max_sub_array.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index 62e1f6846087..d17b3818767f 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -40,8 +40,7 @@ def find_max_cross_sum(A, low, mid, high): def max_sub_array(nums: List[int]) -> int: """ - Finds the contiguous subarray - which has the largest sum and return its sum. + Finds the contiguous subarray which has the largest sum and return its sum. >>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4]) 6 From 322ce031fc4e724b9b5e5d85965a09c21965db27 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 29 Oct 2019 00:17:43 +0800 Subject: [PATCH 4/4] Fix doctest --- dynamic_programming/max_sub_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index d17b3818767f..f7c8209718ef 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -51,11 +51,11 @@ def max_sub_array(nums: List[int]) -> int: If all elements are negative, the largest subarray would be the empty array, having the sum 0. - >>> max_sub_array([-1,-2,-3]) + >>> max_sub_array([-1, -2, -3]) 0 - >>> max_sub_array([5,-2,-3]) + >>> max_sub_array([5, -2, -3]) 5 - >>> max_sub_array([31, −41, 59, 26, −53, 58, 97, −93, −23, 84]) + >>> max_sub_array([31, -41, 59, 26, -53, 58, 97, -93, -23, 84]) 187 """ best = 0