From 43364c529e9565d8ae0b15f4c90c50bb196ca838 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Thu, 5 Oct 2023 19:25:03 +0530 Subject: [PATCH 1/6] Changing the directory of sigmoid_linear_unit.py --- neural_network/activation_functions/__init__.py | 0 .../activation_functions}/sigmoid_linear_unit.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 neural_network/activation_functions/__init__.py rename {maths => neural_network/activation_functions}/sigmoid_linear_unit.py (100%) diff --git a/neural_network/activation_functions/__init__.py b/neural_network/activation_functions/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/maths/sigmoid_linear_unit.py b/neural_network/activation_functions/sigmoid_linear_unit.py similarity index 100% rename from maths/sigmoid_linear_unit.py rename to neural_network/activation_functions/sigmoid_linear_unit.py From 2c3896be3cc1ec291cf6e374b79dc6f88374929d Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Thu, 5 Oct 2023 10:02:48 -0400 Subject: [PATCH 2/6] Delete neural_network/activation_functions/__init__.py --- neural_network/activation_functions/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 neural_network/activation_functions/__init__.py diff --git a/neural_network/activation_functions/__init__.py b/neural_network/activation_functions/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 From 370140b667b356e358c5a04bd022f014dedaf3f4 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 11:48:26 +0530 Subject: [PATCH 3/6] Fixing the Code to take input as a list and Adding doctest in it. --- dynamic_programming/longest_sub_array.py | 68 +++++++++++++++++------- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index b477acf61e66..0b0f0a4d2fea 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -10,24 +10,56 @@ """ -class SubArray: - def __init__(self, arr): - # we need a list not a string, so do something to change the type - self.array = arr.split(",") - - def solve_sub_array(self): - rear = [int(self.array[0])] * len(self.array) - sum_value = [int(self.array[0])] * len(self.array) - for i in range(1, len(self.array)): - sum_value[i] = max( - int(self.array[i]) + sum_value[i - 1], int(self.array[i]) - ) - rear[i] = max(sum_value[i], rear[i - 1]) - return rear[len(self.array) - 1] +def longest_sub_array(arr:list): + """ + Find the longest continuous subarray with the maximum sum within a given list of integers. + + Args: + arr (list): A list of integers. + + Returns: + A Integer which is the max subarray sum in the whole array. + + Examples: + >>> longest_sub_array([1, 2, 3, 2, 5]) + 13 + + >>> longest_sub_array([5, -4, 3, -2, 1]) + 5 + + >>> longest_sub_array([1, 2, 3, -2, 5]) + 9 + + >>> longest_sub_array([10, 20, -30, 40, 50]) + 90 + + >>> longest_sub_array([]) + 0 + """ + + max_so_far = arr[0] + max_ending_here = arr[0] + max_len = 1 + curr_len = 1 + + for i in range(1, len(arr)): + if max_ending_here < 0: + max_ending_here = arr[i] + curr_len = 1 + else: + max_ending_here += arr[i] + curr_len += 1 + if max_ending_here > max_so_far: + max_so_far = max_ending_here + max_len = curr_len + elif max_ending_here == max_so_far: + max_len = max(max_len, curr_len) + + return max_len + if __name__ == "__main__": - whole_array = input("please input some numbers:") - array = SubArray(whole_array) - re = array.solve_sub_array() - print(("the results is:", re)) + import doctest + + doctest.testmod() \ No newline at end of file From 7338a9c06fadce8552e8b7de7c961782528a80b3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 06:23:49 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_sub_array.py | 39 ++++++++++++------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index 0b0f0a4d2fea..7b1f166705d6 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -10,32 +10,32 @@ """ -def longest_sub_array(arr:list): +def longest_sub_array(arr: list): """ - Find the longest continuous subarray with the maximum sum within a given list of integers. + Find the longest continuous subarray with the maximum sum within a given list of integers. - Args: - arr (list): A list of integers. + Args: + arr (list): A list of integers. - Returns: - A Integer which is the max subarray sum in the whole array. + Returns: + A Integer which is the max subarray sum in the whole array. - Examples: - >>> longest_sub_array([1, 2, 3, 2, 5]) - 13 + Examples: + >>> longest_sub_array([1, 2, 3, 2, 5]) + 13 - >>> longest_sub_array([5, -4, 3, -2, 1]) - 5 + >>> longest_sub_array([5, -4, 3, -2, 1]) + 5 - >>> longest_sub_array([1, 2, 3, -2, 5]) - 9 + >>> longest_sub_array([1, 2, 3, -2, 5]) + 9 - >>> longest_sub_array([10, 20, -30, 40, 50]) - 90 + >>> longest_sub_array([10, 20, -30, 40, 50]) + 90 - >>> longest_sub_array([]) - 0 - """ + >>> longest_sub_array([]) + 0 + """ max_so_far = arr[0] max_ending_here = arr[0] @@ -58,8 +58,7 @@ def longest_sub_array(arr:list): return max_len - if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 9dd01704aeafa45a87536963dbfd392a19f197c4 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 11:55:06 +0530 Subject: [PATCH 5/6] Fixing the long line --- dynamic_programming/longest_sub_array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index 0b0f0a4d2fea..88f9173a6621 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -12,7 +12,7 @@ def longest_sub_array(arr:list): """ - Find the longest continuous subarray with the maximum sum within a given list of integers. + Find the longest continuous subarray with the maximum sum. Args: arr (list): A list of integers. @@ -62,4 +62,4 @@ def longest_sub_array(arr:list): if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 4cb3a55a8943d4e18d12cc9cd61442c4ab3dde28 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 06:26:35 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_sub_array.py | 37 ++++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index 88f9173a6621..0f4c691af117 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -10,32 +10,32 @@ """ -def longest_sub_array(arr:list): +def longest_sub_array(arr: list): """ - Find the longest continuous subarray with the maximum sum. + Find the longest continuous subarray with the maximum sum. - Args: - arr (list): A list of integers. + Args: + arr (list): A list of integers. - Returns: - A Integer which is the max subarray sum in the whole array. + Returns: + A Integer which is the max subarray sum in the whole array. - Examples: - >>> longest_sub_array([1, 2, 3, 2, 5]) - 13 + Examples: + >>> longest_sub_array([1, 2, 3, 2, 5]) + 13 - >>> longest_sub_array([5, -4, 3, -2, 1]) - 5 + >>> longest_sub_array([5, -4, 3, -2, 1]) + 5 - >>> longest_sub_array([1, 2, 3, -2, 5]) - 9 + >>> longest_sub_array([1, 2, 3, -2, 5]) + 9 - >>> longest_sub_array([10, 20, -30, 40, 50]) - 90 + >>> longest_sub_array([10, 20, -30, 40, 50]) + 90 - >>> longest_sub_array([]) - 0 - """ + >>> longest_sub_array([]) + 0 + """ max_so_far = arr[0] max_ending_here = arr[0] @@ -58,7 +58,6 @@ def longest_sub_array(arr:list): return max_len - if __name__ == "__main__": import doctest