Hướng dẫn find the maximum of subsequence in an integer list python - tìm tối đa dãy con trong python danh sách số nguyên

Tôi đã được đưa ra một câu hỏi trong khi phỏng vấn, và tôi quyết định mã hóa nó và tìm hiểu cách khác biệt để thực hiện vấn đề này. Tìm tổng tối đa của một chuỗi tiếp giáp trong một danh sách. Tôi đã tự hỏi nếu bạn có thể viết mã xem lại các cách khác nhau để giải quyết vấn đề này.

Đưa ra một danh sách bao gồm cả số nguyên dương và âm, hãy tìm tổng tối đa trong số tất cả các chuỗi tiếp giáp của danh sách đầu vào. Viết một hàm có trong một danh sách các số nguyên và trả về số tiền tối đa.

#  Example:   input = [6, -1, 3, 5, -10]
#             output = 13 [6 + -1 + 3 + 5 = 13]

một vi dụ khac.

#maxSubArraySum[[-1,-2,3,4,5]] ==> 12

#maxSubArraySum[[1,2,3,-2,5]] ==> 9

Giải pháp đầu tiên của tôi

def maxSubArraySum[arr]:

    max_so_far =arr[0]
    curr_max = arr[0]

    for i in range[1,len[arr]]:
        curr_max = max[arr[i], curr_max + arr[i]]
        max_so_far = max[max_so_far,curr_max]

    return max_so_far

# Driver function to check the above function 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum[a]

Giải pháp giải pháp thứ hai của tôi giải pháp lập trình động

def maxSubArraySum[nums]:
  if not nums: return 0
  n = len[nums]
  s = [0] * n
  res, s, s_pre = nums[0], nums[0], nums[0]
  for i in xrange[1, n]:
      s = max[nums[i], s_pre + nums[i]]
      s_pre = s
      res = max[res, s]
  return res

nó vượt qua tất cả các bài kiểm tra

# input: count {List} - keeps track out how many tests pass and how many total
#        in the form of a two item array i.e., [0, 0]
# input: name {String} - describes the test
# input: test {Function} - performs a set of operations and returns a boolean
#        indicating if test passed
# output: {None}
def expect[count, name, test]:
    if [count is None or not isinstance[count, list] or len[count] != 2]:
        count = [0, 0]
    else:
        count[1] += 1

    result = 'false'
    error_msg = None
    try:
        if test[]:
            result = ' true'
            count[0] += 1
    except Exception as err:
        error_msg = str[err]

    print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
    if error_msg is not None:
        print['       ' + error_msg + '\n']


print['max_consecutive_sum Tests']
test_count = [0, 0]


def test[]:
    example = max_consecutive_sum[[6, -1, 3, 5, -10]]
    return example == 13


expect[test_count, 'should work on example input', test]


def test[]:
    example = max_consecutive_sum[[5]]
    return example == 5


expect[test_count, 'should work on single-element input', test]


def test[]:
    example = max_consecutive_sum[[]]
    return example == 0


expect[test_count, 'should return 0 for empty input', test]


def test[]:
    example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
    return example == 6


expect[test_count, 'should work on longer input', test]

print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
max_consecutive_sum Tests
  1]    true : should work on example input
  2]    true : should work on single-element input
  3]    true : should return 0 for empty input
  4]    true : should work on longer input
PASSED: 4 / 4

#maxSubArraySum[[-1,-2,3,4,5]] ==> 12

#maxSubArraySum[[1,2,3,-2,5]] ==> 9
7
def maxSubArraySum[nums]:
  if not nums: return 0
  n = len[nums]
  s = [0] * n
  res, s, s_pre = nums[0], nums[0], nums[0]
  for i in xrange[1, n]:
      s = max[nums[i], s_pre + nums[i]]
      s_pre = s
      res = max[res, s]
  return res
2
def maxSubArraySum[nums]:
  if not nums: return 0
  n = len[nums]
  s = [0] * n
  res, s, s_pre = nums[0], nums[0], nums[0]
  for i in xrange[1, n]:
      s = max[nums[i], s_pre + nums[i]]
      s_pre = s
      res = max[res, s]
  return res
3
#maxSubArraySum[[-1,-2,3,4,5]] ==> 12

#maxSubArraySum[[1,2,3,-2,5]] ==> 9
0
def maxSubArraySum[arr]:

    max_so_far =arr[0]
    curr_max = arr[0]

    for i in range[1,len[arr]]:
        curr_max = max[arr[i], curr_max + arr[i]]
        max_so_far = max[max_so_far,curr_max]

    return max_so_far

# Driver function to check the above function 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum[a]
77

def maxSubArraySum[arr]:

    max_so_far =arr[0]
    curr_max = arr[0]

    for i in range[1,len[arr]]:
        curr_max = max[arr[i], curr_max + arr[i]]
        max_so_far = max[max_so_far,curr_max]

    return max_so_far

# Driver function to check the above function 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum[a]
6
def maxSubArraySum[arr]:

    max_so_far =arr[0]
    curr_max = arr[0]

    for i in range[1,len[arr]]:
        curr_max = max[arr[i], curr_max + arr[i]]
        max_so_far = max[max_so_far,curr_max]

    return max_so_far

# Driver function to check the above function 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum[a]
4
#maxSubArraySum[[-1,-2,3,4,5]] ==> 12

#maxSubArraySum[[1,2,3,-2,5]] ==> 9
04

#maxSubArraySum[[-1,-2,3,4,5]] ==> 12

#maxSubArraySum[[1,2,3,-2,5]] ==> 9
7
def maxSubArraySum[arr]:

    max_so_far =arr[0]
    curr_max = arr[0]

    for i in range[1,len[arr]]:
        curr_max = max[arr[i], curr_max + arr[i]]
        max_so_far = max[max_so_far,curr_max]

    return max_so_far

# Driver function to check the above function 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum[a]
4
def maxSubArraySum[arr]:

    max_so_far =arr[0]
    curr_max = arr[0]

    for i in range[1,len[arr]]:
        curr_max = max[arr[i], curr_max + arr[i]]
        max_so_far = max[max_so_far,curr_max]

    return max_so_far

# Driver function to check the above function 
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print"Maximum contiguous sum is" , maxSubArraySum[a]
93

  • #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    05
  • def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    10
  • #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    77

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    04

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    93

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    05arr[] of size N, the task is to find the maximum sum non-empty subsequence present in the given array.

    Examples:

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    10
    arr[] = { 2, 3, 7, 1, 9 } 
    Output: 22 
    Explanation: 
    Sum of the subsequence { arr[0], arr[1], arr[2], arr[3], arr[4] } is equal to 22, which is the maximum possible sum of any subsequence of the array. 
    Therefore, the required output is 22.

    Đầu vào: mảng [] = {-2, 11, -4, 2, -3, -10} & nbsp; đầu ra: 13 & nbsp; giải thích: & nbsp; tổng của phần sau đến 13, đó là tổng tối đa có thể của bất kỳ phần trăm nào của mảng. & nbsp; do đó, đầu ra cần thiết là 13. arr[] = { -2, 11, -4, 2, -3, -10 } 
    Output: 13 
    Explanation: 
    Sum of the subsequence { arr[1], arr[3] } is equal to 13, which is the maximum possible sum of any subsequence of the array. 
    Therefore, the required output is 13.

    Cách tiếp cận ngây thơ: Cách tiếp cận đơn giản nhất để giải quyết vấn đề này là tạo ra tất cả các chuỗi không trống có thể có của mảng và tính tổng của mỗi phần sau của mảng. Cuối cùng, in tổng tối đa thu được từ phần sau. The simplest approach to solve this problem is to generate all possible non-empty subsequences of the array and calculate the sum of each subsequence of the array. Finally, print the maximum sum obtained from the subsequence.

    & nbsp; độ phức tạp về thời gian: o [n * 2n] & nbsp; không gian phụ trợ: O [n]Time Complexity: O[N * 2N] 
    Auxiliary Space: O[N]

    Cách tiếp cận hiệu quả: Ý tưởng là đi qua mảng và tính tổng các phần tử dương của mảng và in tổng thu được. Thực hiện theo các bước dưới đây để giải quyết vấn đề: The idea is to traverse the array and calculate the sum of positive elements of the array and print the sum obtained. Follow the steps below to solve the problem:

    • Kiểm tra xem phần tử lớn nhất của mảng lớn hơn 0 hoặc không. Nếu được tìm thấy là đúng, sau đó đi qua mảng và in tổng của tất cả các phần tử dương của mảng.0 or not. If found to be true, then traverse the array and print the sum of all positive elements of the array.
    • Nếu không, in phần tử lớn nhất có trong mảng.

    Dưới đây là việc thực hiện phương pháp trên:

    C++

    #include

    using namespace std;

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    1
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    5

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    9

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    2

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    5

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    8

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    5

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    8

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    7

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    4

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    7
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    8
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    9
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    8#include 1

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7#include 3

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7 #include 6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    Java

    #include 8 #include 9

    using0 using1

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    using3using4

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    1
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    3__

    using3

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 namespace5namespace6namespace7

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 std;0namespace6std;2

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 std;7std;8std;9

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    04

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    08

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    15namespace6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    17

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    19

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    8

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 std;7namespace6std;9

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    36namespace6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    17

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    7

    using3

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    0

    using3

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    4

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    7
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    8
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    9
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    8#include 1

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    78

    using3

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    Python3

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7 #include 6

    Java

    #include 8 #include 9

    using0 using1

    using3using4

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    1
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    3__

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 namespace5namespace6namespace7

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 std;0namespace6std;2

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 std;7std;8std;9

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    15namespace6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    17

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    8

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0 std;7namespace6std;9

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    53
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    54

    C#

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    02
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    36namespace6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    17

    using3

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    53 using4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    55
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    56

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    61
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    62
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    63
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    64
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    65
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    66
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    63
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    62

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    9

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    2

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    5

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    8

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    08

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    5

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    8

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    3
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    5

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    8

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    7

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    29

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    0
    max_consecutive_sum Tests
      1]    true : should work on example input
      2]    true : should work on single-element input
      3]    true : should return 0 for empty input
      4]    true : should work on longer input
    PASSED: 4 / 4
    
    4

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    34

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    JavaScript

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    37

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    39
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    40

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    44

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    46

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    49

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    04

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    57
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    08

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    93

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    8

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    2
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    75

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    4
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    10

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    6

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    57
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    0

    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    9
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    6
    def maxSubArraySum[arr]:
    
        max_so_far =arr[0]
        curr_max = arr[0]
    
        for i in range[1,len[arr]]:
            curr_max = max[arr[i], curr_max + arr[i]]
            max_so_far = max[max_so_far,curr_max]
    
        return max_so_far
    
    # Driver function to check the above function 
    a = [-2, -3, 4, -1, -2, 1, 5, -3]
    print"Maximum contiguous sum is" , maxSubArraySum[a]
    
    7
    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    7

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    0

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    95

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    97

    #maxSubArraySum[[-1,-2,3,4,5]] ==> 12
    
    #maxSubArraySum[[1,2,3,-2,5]] ==> 9
    
    7
    def maxSubArraySum[nums]:
      if not nums: return 0
      n = len[nums]
      s = [0] * n
      res, s, s_pre = nums[0], nums[0], nums[0]
      for i in xrange[1, n]:
          s = max[nums[i], s_pre + nums[i]]
          s_pre = s
          res = max[res, s]
      return res
    
    99

    # input: count {List} - keeps track out how many tests pass and how many total
    #        in the form of a two item array i.e., [0, 0]
    # input: name {String} - describes the test
    # input: test {Function} - performs a set of operations and returns a boolean
    #        indicating if test passed
    # output: {None}
    def expect[count, name, test]:
        if [count is None or not isinstance[count, list] or len[count] != 2]:
            count = [0, 0]
        else:
            count[1] += 1
    
        result = 'false'
        error_msg = None
        try:
            if test[]:
                result = ' true'
                count[0] += 1
        except Exception as err:
            error_msg = str[err]
    
        print['  ' + [str[count[1]] + ']   '] + result + ' : ' + name]
        if error_msg is not None:
            print['       ' + error_msg + '\n']
    
    
    print['max_consecutive_sum Tests']
    test_count = [0, 0]
    
    
    def test[]:
        example = max_consecutive_sum[[6, -1, 3, 5, -10]]
        return example == 13
    
    
    expect[test_count, 'should work on example input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[5]]
        return example == 5
    
    
    expect[test_count, 'should work on single-element input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[]]
        return example == 0
    
    
    expect[test_count, 'should return 0 for empty input', test]
    
    
    def test[]:
        example = max_consecutive_sum[[-1, 1, -3, 4, -1, 2, 1, -5, 4]]
        return example == 6
    
    
    expect[test_count, 'should work on longer input', test]
    
    print['PASSED: ' + str[test_count[0]] + ' / ' + str[test_count[1]] + '\n\n']
    
    00

    Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [1] O[N] 
    Auxiliary Space: O[1]


    Chủ Đề