[Solved] How can we test if a list contains n consecutive numbers with a difference of 1? [closed]
Try reducing your list. from functools import reduce def get_all_consecutives(iterable, consecutive_limit): consecutives = [] def consecutive_reducer(accumulator, next_item): if not accumulator: accumulator.append(next_item) else: if next_item – accumulator[-1] == 1: accumulator.append(next_item) else: accumulator = [next_item] if len(accumulator) == consecutive_limit: consecutives.append(accumulator) return accumulator reduce(consecutive_reducer, iterable, []) if not consecutives: return False elif len(consecutives) == 1: return consecutives[0] else: … Read more