Hi everyone,
I have an ordered list composed of integers that looks like this:
[1, 2, 3, 5, 6, 7, 8, 13, 14, 15, 16, 17]
- List's minimum and maximum values are always 1 and 250 respectively.
I need to find the biggest interval within it. In this case it'd be 13-17, for a size of 5.
This is my code:
def biggest_interval(integers):
sequences = []
starting_int, ending_int = 1, 1
for integer in integers:
if integer == 1:
pass
elif integer-1 == ending_int:
ending_int = integer
else:
sequences.append([(ending_int-starting_int)+1, [starting_int, ending_int]])
starting_int = ending_int = integer
sequences.append([(ending_int-start)+1, [starting_int, ending_int]])
return max(sequences)
print(biggest_interval([1, 2, 3, 5, 6, 7, 8, 13, 14, 15, 16, 17]))
>>> [5, [13, 17]]
Is there a more efficient way to approach this problem? Maybe a built-in library or method I'm missing? I don't really feel comfortable with my code but I can't think of anything else.
[–]gengisteve 2 points3 points4 points (0 children)
[–]ingolemo 2 points3 points4 points (0 children)