Python/dynamic_programming/longest_sub_array.py
Tianyi Zheng cc10b20beb
Remove some print statements within algorithmic functions ()
* Remove commented-out print statements in algorithmic functions

* Encapsulate non-algorithmic code in __main__

* Remove unused print_matrix function

* Remove print statement in __init__

* Remove print statement from doctest

* Encapsulate non-algorithmic code in __main__

* Modify algorithm to return instead of print

* Encapsulate non-algorithmic code in __main__

* Refactor data_safety_checker to return instead of print

* updating DIRECTORY.md

* updating DIRECTORY.md

* Apply suggestions from code review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-10-22 13:33:51 +02:00

34 lines
1019 B
Python

"""
Author : Yvonne
This is a pure Python implementation of Dynamic Programming solution to the
longest_sub_array problem.
The problem is :
Given an array, to find the longest and continuous sub array and get the max sum of the
sub array in the given array.
"""
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]
if __name__ == "__main__":
whole_array = input("please input some numbers:")
array = SubArray(whole_array)
re = array.solve_sub_array()
print(("the results is:", re))