The aim is to find groups of increasing/monotonic numbers given a list of integers. Each item in the resulting group must be of a +1 increment from the previous item Given an input: x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5] I need to find groups of increasing numbers and achieve: increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)] And eventually also the number of increasing numbers: len(list(chain(*increasing_numbers))) And also the len of the groups: increasing_num_groups_length = [len(i) for i in increasing_numbers] I have tried the following to get the number of increasing numbers: >>> from itertools import tee, chain >>> def pairwise(iterable): ... a, b = tee(iterable) ... next(b, None) ... return zip(a, b) ... >>> x = [8, 9, 10, 11, 7, 1, 2, 3, 4, 5, 6] >>> set(list(chain(*[(i,j) for i,j in pairwise(x) if j-1==i]))) set([1, 2, 3, 4, 5, 6, 8, 9, 10, 11]) >>> len(set(list(chain(*[(i,j) for i,j in pairwise(x)...
Comments
Post a Comment