I have a list of integers from 1 to n
.
How can check if a subset of this list contains only consecutive numbers, accounting for a cyclic iteration (i.e. after n
the iteration continues with 1
)
Assuming my function is called foo
and n=36
, this is what I need:
# subsets could be bigger, using 3 elements as example
foo([1, 2, 3]) # True
foo([8, 9, 10]) # True
foo([35, 36, 1]) # True
foo([36, 1, 2]) # True
foo([1, 3, 4]) # False
foo([15, 17, 20]) # False
foo([3, 2, 1]) # False
My current approach is to create a string template with two cycles, and then compare if the string representation of the subset is contained in the template.
I’m sure there’s a better way.
1