Created
April 11, 2019 19:33
-
-
Save daniel-woods/8c7659f57c17e104a1c07fa5f3ce46d8 to your computer and use it in GitHub Desktop.
Calculate the sum of the largest continuous integers in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ 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