diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index b477acf61e66..072fcee7f5bc 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -10,24 +10,51 @@ """ -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_subarray(arr: list): + """ + Find the longest continuous subarray with the maximum sum. + + Args: + arr (list): A list of integers. + + Returns: + A Integer which is the max subarray sum in the whole array. + + Examples: + >>> longest_subarray([1, 2, 3, 2, 5]) + 13 + + >>> longest_subarray([5, -4, 3, -2, 1]) + 5 + + >>> longest_subarray([1, 2, 3, -2, 5]) + 9 + + >>> longest_subarray([10, 20, -30, 40, 50]) + 90 + + >>> longest_subarray([]) + 0 + """ + + if not arr: + return 0 + + max_sum = arr[0] + current_sum = arr[0] + + for i in range(1, len(arr)): + if arr[i] > (current_sum + arr[i]): + current_sum = arr[i] + else: + current_sum += arr[i] + + max_sum = max(max_sum, current_sum) + + return max_sum 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() diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 819fd8106def..9b48ea201062 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -3,42 +3,50 @@ 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()