Skip to content

Adding the doctests and fixing the code in dynamic_programming/longest_sub_array #10154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions dynamic_programming/longest_sub_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
48 changes: 28 additions & 20 deletions dynamic_programming/subset_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()