-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest_Sum.py
More file actions
29 lines (22 loc) · 834 Bytes
/
Largest_Sum.py
File metadata and controls
29 lines (22 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 8 09:44:23 2019
@author: William Keilsohn
"""
'''
Given an array of itegers (positive and negative) write a program that can find the
largest continuous sum.
'''
test = [10, -1, 11, 23, 4, -3]
# Solution based on course solution from:
# https://www.udemy.com/data-science-career-guide-interview-preparation/learn/v4/content
def findSum(arr): # This is the major function the course walks through.
if len(arr) == 0:
return 0
val1 = arr[0]
val2 = arr[0]
for i in arr[1:]:
val2 = max(val2 + i, i) # Checking to see if we get a larger number if we add the next in the list/array.
val1 = max(val2, val1) # Checking to see if this new variable is larger than our current maximum
return val1
print(findSum(test))