Guest User

Untitled

a guest
Feb 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. def better_max_sum(array):
  2. max_sum = 0
  3. max_ending_at_current_index = 0
  4. for value in array:
  5. max_ending_at_current_index += value
  6. max_sum = max(max_sum, max_ending_at_current_index)
  7. max_ending_at_current_index = max(max_ending_at_current_index, 0)
  8.  
  9. return max_sum
  10.  
  11. def max_sum(array):
  12. array_of_arrays = []
  13. for i in range(len(array)):
  14. for j in range(i, len(array)):
  15. array_of_arrays.append(array[i:j+1])
  16.  
  17. return max(list(map(lambda x: sum(x), array_of_arrays)))
  18.  
  19. if __name__ == '__main__':
  20.  
  21. tests = [
  22. [1, 3, -2, 3, 6],
  23. [1, -1, -2, 5],
  24. [1, 1, -1, 5],
  25. [2, 1, -3, 4, -1, 2, 1, -5, 4],
  26. [-100000, 1, -3, 4, 2, 1, -5, 4],
  27. [2, 1, -3, 4, -1, 2, 1, -5, -10000]
  28. ]
  29.  
  30. correct_answers = [11, 5, 6, 6, 7, 6]
  31. answers = list(map(better_max_sum, tests))
  32. assert answers == correct_answers
  33. print(answers)
Add Comment
Please, Sign In to add comment