From 43364c529e9565d8ae0b15f4c90c50bb196ca838 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Thu, 5 Oct 2023 19:25:03 +0530 Subject: [PATCH 1/9] 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/9] 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/9] 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/9] [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/9] 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/9] [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 From 8c612ac027e58acf54677907990096b5503ca24e Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 12:16:36 +0530 Subject: [PATCH 7/9] Fixing the Code --- dynamic_programming/longest_sub_array.py | 40 +++++++++++------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index 0f4c691af117..072fcee7f5bc 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -10,7 +10,7 @@ """ -def longest_sub_array(arr: list): +def longest_subarray(arr: list): """ Find the longest continuous subarray with the maximum sum. @@ -21,41 +21,37 @@ def longest_sub_array(arr: list): A Integer which is the max subarray sum in the whole array. Examples: - >>> longest_sub_array([1, 2, 3, 2, 5]) + >>> longest_subarray([1, 2, 3, 2, 5]) 13 - >>> longest_sub_array([5, -4, 3, -2, 1]) + >>> longest_subarray([5, -4, 3, -2, 1]) 5 - >>> longest_sub_array([1, 2, 3, -2, 5]) + >>> longest_subarray([1, 2, 3, -2, 5]) 9 - >>> longest_sub_array([10, 20, -30, 40, 50]) + >>> longest_subarray([10, 20, -30, 40, 50]) 90 - >>> longest_sub_array([]) + >>> longest_subarray([]) 0 """ - max_so_far = arr[0] - max_ending_here = arr[0] - max_len = 1 - curr_len = 1 + if not arr: + return 0 + + max_sum = arr[0] + current_sum = arr[0] for i in range(1, len(arr)): - if max_ending_here < 0: - max_ending_here = arr[i] - curr_len = 1 + if arr[i] > (current_sum + arr[i]): + current_sum = arr[i] 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 + current_sum += arr[i] + + max_sum = max(max_sum, current_sum) + + return max_sum if __name__ == "__main__": From ebf2a6c16cb786329944e6583b3a76e15b8e598c Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 19:39:18 +0530 Subject: [PATCH 8/9] Adding doctests in subset_generation.py --- dynamic_programming/subset_generation.py | 49 ++++++++++++++---------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 819fd8106def..aa4c6e30bfa6 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -3,42 +3,51 @@ def combination_util(arr, n, r, index, data, i): """ - Current combination is ready to be printed, print it - arr[] ---> Input Array - data[] ---> Temporary array to store current combination - start & end ---> Staring and Ending indexes in arr[] - index ---> Current index in data[] - r ---> Size of a combination to be printed + Generate and print all combinations of 'r' elements from the input list 'arr'. + + Args: + arr (list): The input list from which combinations are generated. + n (int): The total number of elements in the input list 'arr'. + r (int): The size of the combinations to be generated. + index (int): The current index in the 'data' array. + data (list): Temporary array to store the current combination being generated. + i (int): The current index in the input list 'arr'. + + Returns: + None: This function prints the combinations but does not return a value. + + Examples: + >>> arr = [1, 2, 3, 4] + >>> n = len(arr) + >>> r = 2 + >>> data = [0] * r + >>> combination_util(arr, n, r, 0, data, 0) + 1 2 + 1 3 + 1 4 + 2 3 + 2 4 + 3 4 """ if index == r: for j in range(r): print(data[j], end=" ") print(" ") return - # When no more elements are there to put in data[] if i >= n: return - # current is included, put next at next location data[index] = arr[i] combination_util(arr, n, r, index + 1, data, i + 1) - # current is excluded, replace it with - # next (Note that i+1 is passed, but - # index is not changed) combination_util(arr, n, r, index, data, i + 1) - # The main function that prints all combinations - # of size r in arr[] of size n. This function - # mainly uses combinationUtil() + def print_combination(arr, n, r): - # A temporary array to store all combination one by one data = [0] * r - # Print all combination using temporary array 'data[]' combination_util(arr, n, r, 0, data, 0) if __name__ == "__main__": - # Driver code to check the function above - arr = [10, 20, 30, 40, 50] - print_combination(arr, len(arr), 3) - # This code is contributed by Ambuj sahu + import doctest + + doctest.testmod() From 0bede821c9edd05634962a5d8ff7ff53e33cbae1 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 14:11:25 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/subset_generation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index aa4c6e30bfa6..9b48ea201062 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -41,7 +41,6 @@ def combination_util(arr, n, r, index, data, i): combination_util(arr, n, r, index, data, i + 1) - def print_combination(arr, n, r): data = [0] * r combination_util(arr, n, r, 0, data, 0)