From 23e85f6ee7efc4267528a852d8097d204faeaf1c Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 11:44:29 +0530 Subject: [PATCH 1/8] Add files via upload --- maths/Kadane's_algorithm.py | 69 +++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 maths/Kadane's_algorithm.py diff --git a/maths/Kadane's_algorithm.py b/maths/Kadane's_algorithm.py new file mode 100644 index 000000000000..f521242065f2 --- /dev/null +++ b/maths/Kadane's_algorithm.py @@ -0,0 +1,69 @@ +""" + Kadane's algorithm to get maximum subarray sum + Please enter the decimal values only and use spaces as seperator +""" +def negative_exist(arr): + + """ + checks wether the max value in -ve or not + eg :- + arr = [-2,-8,-9] + so max element is negative i.e. -2 + so answer is -2 + + and if max value is positive it will return 0 + and then apply the kadane's algorithm + eg :- + arr = [2,8,-9] + positive number exist so it will return 0 + """ + max=arr[0] + for i in arr: + if(i>=0): + return 0; + elif(max<=i): + max=i + return max + + +def kadanes_implementation(arr): + + + """ + if negative_exist return 0 than this function will execute + else it will return the value return by negative_exist function + + Eg :- + arr = [2,3,-9,8,-2] + initially the value of max_sum = 0 and max_till_element is 0 + than when max_sum less than max_till particular element it will assign that value to max_sum + and when value of max_till_sum is less than 0 it will assign 0 to i + and after whole process it will return the value + + So th output for above arr is :- 8 + """ + if(negative_exist(arr)<0): + return negative_exist(arr) + + max_sum = 0 + max_till_element=0 + + for i in arr: + max_till_element=max_till_element+i + if max_sum <= max_till_element: + max_sum = max_till_element + if max_till_element < 0: + max_till_element = 0 + + return max_sum + + + +if __name__ == "__main__": + try: + print("Enter the element of array with spaces :- ") + arr = list(map(int, input().split())) + print(kadanes_implementation(arr)) + except ValueError: + print("Please,enter decimal values") + \ No newline at end of file From 56e1222c75d4e0735b99857727e81d64a982c0fc Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 12:18:56 +0530 Subject: [PATCH 2/8] Update Kadane's_algorithm.py --- maths/Kadane's_algorithm.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/maths/Kadane's_algorithm.py b/maths/Kadane's_algorithm.py index f521242065f2..446125b8b302 100644 --- a/maths/Kadane's_algorithm.py +++ b/maths/Kadane's_algorithm.py @@ -1,11 +1,26 @@ """ Kadane's algorithm to get maximum subarray sum - Please enter the decimal values only and use spaces as seperator + Please enter the decimal values only and use spaces as separator +""" + + +""" +Doc-Test :- + +Enter the element of array with spaces :- +>>> 2 3 -9 8 -2 +Maximum subarray sum of [2, 3, -9, 8, -2] is :- 8 + + +Enter the element of array with spaces :- +>>> -9 -8 -7 -6 -2 +Maximum subarray sum of [-9, -8, -7, -6, -2] is :- -2 + """ def negative_exist(arr): """ - checks wether the max value in -ve or not + checks whether the max value in -ve or not eg :- arr = [-2,-8,-9] so max element is negative i.e. -2 @@ -40,9 +55,9 @@ def kadanes_implementation(arr): and when value of max_till_sum is less than 0 it will assign 0 to i and after whole process it will return the value - So th output for above arr is :- 8 + So the output for above arr is :- 8 """ - if(negative_exist(arr)<0): + if negative_exist(arr) < 0: return negative_exist(arr) max_sum = 0 @@ -62,8 +77,8 @@ def kadanes_implementation(arr): if __name__ == "__main__": try: print("Enter the element of array with spaces :- ") - arr = list(map(int, input().split())) - print(kadanes_implementation(arr)) + arr = [int(x) for x in input().split()] + print("Maximum subarray sum of ",arr,"is :-",kadanes_implementation(arr)) except ValueError: print("Please,enter decimal values") - \ No newline at end of file + From 244325cb46420ed29900130db92358ccc2f11427 Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 12:35:27 +0530 Subject: [PATCH 3/8] Update and rename Kadane's_algorithm.py to kadanes_algorithm.py --- maths/{Kadane's_algorithm.py => kadanes_algorithm.py} | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) rename maths/{Kadane's_algorithm.py => kadanes_algorithm.py} (87%) diff --git a/maths/Kadane's_algorithm.py b/maths/kadanes_algorithm.py similarity index 87% rename from maths/Kadane's_algorithm.py rename to maths/kadanes_algorithm.py index 446125b8b302..1207bb5990b6 100644 --- a/maths/Kadane's_algorithm.py +++ b/maths/kadanes_algorithm.py @@ -1,4 +1,6 @@ """ + Link for the explaination of this algorithm :- + https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d Kadane's algorithm to get maximum subarray sum Please enter the decimal values only and use spaces as separator """ @@ -80,5 +82,4 @@ def kadanes_implementation(arr): arr = [int(x) for x in input().split()] print("Maximum subarray sum of ",arr,"is :-",kadanes_implementation(arr)) except ValueError: - print("Please,enter decimal values") - + print("Please,enter decimal values") From 62a5b2e4ea16963eec3008f6093fb41d8d0263dc Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 12:44:51 +0530 Subject: [PATCH 4/8] Update kadanes_algorithm.py --- maths/kadanes_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/kadanes_algorithm.py b/maths/kadanes_algorithm.py index 1207bb5990b6..e23d14a368c0 100644 --- a/maths/kadanes_algorithm.py +++ b/maths/kadanes_algorithm.py @@ -1,5 +1,5 @@ """ - Link for the explaination of this algorithm :- + Link for the explanation of this algorithm :- https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d Kadane's algorithm to get maximum subarray sum Please enter the decimal values only and use spaces as separator From 5aa0304d07cf32cababc7974cc841d9ada0b1028 Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 14:10:59 +0530 Subject: [PATCH 5/8] Update kadanes_algorithm.py --- maths/kadanes_algorithm.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/maths/kadanes_algorithm.py b/maths/kadanes_algorithm.py index e23d14a368c0..38272f3eae3b 100644 --- a/maths/kadanes_algorithm.py +++ b/maths/kadanes_algorithm.py @@ -4,27 +4,17 @@ Kadane's algorithm to get maximum subarray sum Please enter the decimal values only and use spaces as separator """ +import doctest - -""" -Doc-Test :- - -Enter the element of array with spaces :- ->>> 2 3 -9 8 -2 -Maximum subarray sum of [2, 3, -9, 8, -2] is :- 8 - - -Enter the element of array with spaces :- ->>> -9 -8 -7 -6 -2 -Maximum subarray sum of [-9, -8, -7, -6, -2] is :- -2 - -""" def negative_exist(arr): """ checks whether the max value in -ve or not eg :- - arr = [-2,-8,-9] + >>> negative_exist([-2,-8,-9]) + -2 + >>> negative_exist([-2,1,5,9]) + 0 so max element is negative i.e. -2 so answer is -2 @@ -58,6 +48,12 @@ def kadanes_implementation(arr): and after whole process it will return the value So the output for above arr is :- 8 + + >>> kadanes_implementation([2,3,-9,8,-2]) + 8 + + >>> kadanes_implementation([-1 ,-2 ,-3 ,-8 ]) + -1 """ if negative_exist(arr) < 0: return negative_exist(arr) @@ -71,15 +67,12 @@ def kadanes_implementation(arr): max_sum = max_till_element if max_till_element < 0: max_till_element = 0 - return max_sum - - if __name__ == "__main__": try: print("Enter the element of array with spaces :- ") arr = [int(x) for x in input().split()] - print("Maximum subarray sum of ",arr,"is :-",kadanes_implementation(arr)) + print(f"Maximum subarray sum of {arr} is {kadanes_implementation(arr)}") except ValueError: - print("Please,enter decimal values") + print("Please,enter decimal values") From 8c9c8daae0aa23b9e39d6f02139c8194651b800f Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 14:49:39 +0530 Subject: [PATCH 6/8] Update kadanes_algorithm.py --- maths/kadanes_algorithm.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/maths/kadanes_algorithm.py b/maths/kadanes_algorithm.py index 38272f3eae3b..af1c7bfff7f4 100644 --- a/maths/kadanes_algorithm.py +++ b/maths/kadanes_algorithm.py @@ -4,8 +4,6 @@ Kadane's algorithm to get maximum subarray sum Please enter the decimal values only and use spaces as separator """ -import doctest - def negative_exist(arr): """ @@ -33,7 +31,7 @@ def negative_exist(arr): return max -def kadanes_implementation(arr): +def kadanes(arr): """ @@ -49,10 +47,10 @@ def kadanes_implementation(arr): So the output for above arr is :- 8 - >>> kadanes_implementation([2,3,-9,8,-2]) + >>> kadanes([2,3,-9,8,-2]) 8 - >>> kadanes_implementation([-1 ,-2 ,-3 ,-8 ]) + >>> kadanes([-1 ,-2 ,-3 ,-8 ]) -1 """ if negative_exist(arr) < 0: @@ -73,6 +71,6 @@ def kadanes_implementation(arr): try: print("Enter the element of array with spaces :- ") arr = [int(x) for x in input().split()] - print(f"Maximum subarray sum of {arr} is {kadanes_implementation(arr)}") + print(f"Maximum subarray sum of {arr} is {kadanes(arr)}") except ValueError: print("Please,enter decimal values") From 7b0d50c302e25d041887d2cfe5fee7bffb5a6a2a Mon Sep 17 00:00:00 2001 From: Raj-Parekh24 <54325945+Raj-Parekh24@users.noreply.github.com> Date: Fri, 8 May 2020 15:14:35 +0530 Subject: [PATCH 7/8] Update kadanes_algorithm.py --- maths/kadanes_algorithm.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/maths/kadanes_algorithm.py b/maths/kadanes_algorithm.py index af1c7bfff7f4..2d2c90065554 100644 --- a/maths/kadanes_algorithm.py +++ b/maths/kadanes_algorithm.py @@ -7,20 +7,8 @@ def negative_exist(arr): """ - checks whether the max value in -ve or not - eg :- >>> negative_exist([-2,-8,-9]) -2 - >>> negative_exist([-2,1,5,9]) - 0 - so max element is negative i.e. -2 - so answer is -2 - - and if max value is positive it will return 0 - and then apply the kadane's algorithm - eg :- - arr = [2,8,-9] - positive number exist so it will return 0 """ max=arr[0] for i in arr: @@ -49,9 +37,6 @@ def kadanes(arr): >>> kadanes([2,3,-9,8,-2]) 8 - - >>> kadanes([-1 ,-2 ,-3 ,-8 ]) - -1 """ if negative_exist(arr) < 0: return negative_exist(arr) From ca2a480da97b71b748a308187daa0bcf20763a51 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 9 May 2020 21:00:40 +0200 Subject: [PATCH 8/8] Update kadanes_algorithm.py --- maths/kadanes_algorithm.py | 76 ++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/maths/kadanes_algorithm.py b/maths/kadanes_algorithm.py index 2d2c90065554..d02f238a0dc9 100644 --- a/maths/kadanes_algorithm.py +++ b/maths/kadanes_algorithm.py @@ -1,61 +1,65 @@ """ - Link for the explanation of this algorithm :- - https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d - Kadane's algorithm to get maximum subarray sum - Please enter the decimal values only and use spaces as separator +Kadane's algorithm to get maximum subarray sum +https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d +https://en.wikipedia.org/wiki/Maximum_subarray_problem """ -def negative_exist(arr): +test_data = ([-2, -8, -9], [2, 8, 9], [-1, 0, 1], [0, 0], []) + +def negative_exist(arr: list) -> int: """ - >>> negative_exist([-2,-8,-9]) - -2 + >>> negative_exist([-2,-8,-9]) + -2 + >>> [negative_exist(arr) for arr in test_data] + [-2, 0, 0, 0, 0] """ - max=arr[0] + arr = arr or [0] + max = arr[0] for i in arr: - if(i>=0): - return 0; - elif(max<=i): - max=i - return max + if i >= 0: + return 0 + elif max <= i: + max = i + return max -def kadanes(arr): +def kadanes(arr: list) -> int: + """ + If negative_exist() returns 0 than this function will execute + else it will return the value return by negative_exist function + For example: arr = [2, 3, -9, 8, -2] + Initially we set value of max_sum to 0 and max_till_element to 0 than when + max_sum is less than max_till particular element it will assign that value to + max_sum and when value of max_till_sum is less than 0 it will assign 0 to i + and after that whole process, return the max_sum + So the output for above arr is 8 + >>> kadanes([2, 3, -9, 8, -2]) + 8 + >>> [kadanes(arr) for arr in test_data] + [-2, 19, 1, 0, 0] """ - if negative_exist return 0 than this function will execute - else it will return the value return by negative_exist function - - Eg :- - arr = [2,3,-9,8,-2] - initially the value of max_sum = 0 and max_till_element is 0 - than when max_sum less than max_till particular element it will assign that value to max_sum - and when value of max_till_sum is less than 0 it will assign 0 to i - and after whole process it will return the value - - So the output for above arr is :- 8 - - >>> kadanes([2,3,-9,8,-2]) - 8 - """ - if negative_exist(arr) < 0: - return negative_exist(arr) + max_sum = negative_exist(arr) + if max_sum < 0: + return max_sum max_sum = 0 - max_till_element=0 - + max_till_element = 0 + for i in arr: - max_till_element=max_till_element+i + max_till_element += i if max_sum <= max_till_element: max_sum = max_till_element if max_till_element < 0: max_till_element = 0 return max_sum + if __name__ == "__main__": try: - print("Enter the element of array with spaces :- ") + print("Enter integer values sepatated by spaces") arr = [int(x) for x in input().split()] print(f"Maximum subarray sum of {arr} is {kadanes(arr)}") except ValueError: - print("Please,enter decimal values") + print("Please enter integer values.")