[Solved] Print the length of the longest continuous sequence (Python)


Try this instead:

sequence = []
stop = False
while not stop:
    num = int(input())
    if num == -1:
        stop = True
    else:
        sequence.append(num)

longest = 0
length = 0
for i in range(2, len(sequence)):
    if (sequence[i-2] + sequence[i-1]) == sequence[i]:
        length += 1
    else:
        longest = max(longest, length)
        length = 0

First, I read in the sequence of integers until -1 is entered. Then, I loop through all of the numbers in the sequence apart from the first two. For each one, I check if it’s the sum of the previous two numbers. If it is, I increment the variable which is keeping track of the length of the current continuous sequence (length). Once this sequence is finished, I recalculate longest as the maximum of itself and the current subsequence. This is repeated until all of the numbers in the sequence have been checked.

7

solved Print the length of the longest continuous sequence (Python)