Skip to content

Instantly share code, notes, and snippets.

@daniel-woods
Created April 11, 2019 19:33
Show Gist options
  • Save daniel-woods/8c7659f57c17e104a1c07fa5f3ce46d8 to your computer and use it in GitHub Desktop.
Save daniel-woods/8c7659f57c17e104a1c07fa5f3ce46d8 to your computer and use it in GitHub Desktop.
Calculate the sum of the largest continuous integers in an array
def calculate_subs(_list):
totals = []
index = 0
first_val = True
for i in _list:
if first_val:
totals.append(i)
first_val = False
else:
if i - prev == 1:
totals[index] += i
else:
index += 1
totals.append(i)
prev = i
return totals
def main():
_input = [5, 4, 6, 7, 8, 6, 7, 6, 8, 9, -10, -9, 2, 3, 4, 5, 6]
totals = calculate_subs(_input)
biggest = totals[0]
for i in totals:
if i > biggest:
biggest = i
print(_input)
print(totals)
print(biggest)
if __name__ == '__main__':
main()
@daniel-woods
Copy link
Author

$ python calculate_subset.py
[5, 4, 6, 7, 8, 6, 7, 6, 8, 9, -10, -9, 2, 3, 4, 5, 6]
[5, 4, 21, 13, 6, 17, -19, 20]
21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment