0
votes

I have a list numbers say,

[1,2,3,6,8,9,10,11]

First, I want to get the sum of the differences (step size) between the numbers (n, n+1) in the list.

Second, if a set of consecutive numbers having a difference of 1 between them, put them in a list, i.e. there are two such lists in this example,

[1,2,3]

[8,9,10,11]

and then put the rest numbers in another list, i.e. there is only one such list in the example,

[6].

Third, get the lists with the max/min sizes from the sequential lists, i.e. [1,2,3], [8,9,10,11] in this example, the max list is,

[8,9,10,11]

min list is

[1,2,3].

What's the best way to implement this?

2
Considering all your requirements, you should at least have tried somethingMoses Koledoye
What have you tried ? Also why is 4 missing from the [1, 2, 3] list?Ma0
Take a look at itertools.groupby.PM 2Ring
@PM2Ring groupby seems can only return consecutive keys, which doesn't really fit in heredaiyue
@daiyue Should the last min result be [1,2,3]?Moses Koledoye

2 Answers

4
votes

First, I want to get the sum of the differences (step size) between the numbers (n, n+1) in the list.

Use sum on the successive differences of elements in the list:

>>> sum(lst[i] - x for i, x in enumerate(lst[:-1], start=1))
10

Second, if a set of consecutive numbers having a difference of 1 between them, put them in a list, i.e. there are two such lists in this example, and then put the rest numbers in another list, i.e. there is only one such list in the example,

itertools.groupby does this by grouping on the difference of each element on a reference itertools.count object:

>>> from itertools import groupby, count
>>> c = count()    
>>> result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))]
>>> result
[[1, 2, 3, 4], [6], [8, 9, 10, 11]]

Third, get the lists with the max/min sizes from above

max and min with the key function as sum:

>>> max(result, key=sum)
[8, 9, 10, 11]
>>> min(result, key=sum)
[6] #??? shouldn't this be [6]
1
votes

I wonder if you've already got the answer to this (given the missing 4 from your answers) as the first thing I naively tried produced that answer. (That and/or it reads like a homework question)

>>> a=[1,2,3,4,6,8,9,10,11]
>>> sum([a[x+1] - a[x] for x in range(len(a)-1)])
10
>>> [a[x] for x in range(len(a)-1) if abs(a[x] - a[x+1]) ==1]
[1, 2, 3, 8, 9, 10]

Alternatively, try :

a=[1,2,3,6,8,9,10,11]
sets = []
cur_set = set()
total_diff = 0
for index in range(len(a)-1):
    total_diff += a[index +1] - a[index]
    if a[index +1] - a[index] == 1:
        cur_set = cur_set | set([ a[index +1], a[index]])
    else:
        if len(cur_set) > 0:
            sets.append(cur_set)
        cur_set = set()

if len(cur_set) > 0:
    sets.append(cur_set)

all_seq_nos = set()
for seq_set in sets:
    all_seq_nos = all_seq_nos | seq_set
non_seq_set = set(a) - all_seq_nos

print("Sum of differences is {0:d}".format(total_diff))

print("sets of sequential numbers are :")
for seq_set in sets:
    print(sorted(list(seq_set)))

print("set of non-sequential numbers is :")
print(sorted(list(non_seq_set)))

big_set=max(sets, key=sum)
sml_set=min(sets, key=sum)
print ("Biggest set of sequential numbers is :")
print (sorted(list(big_set)))
print ("Smallest set of sequential numbers is :")
print (sorted(list(sml_set)))

Which will produce the output :

Sum of differences is 10
sets of sequential numbers are :
[1, 2, 3]
[8, 9, 10, 11]
set of non-sequential numbers is :
[6]
Biggest set of sequential numbers is :
[8, 9, 10, 11]
Smallest set of sequential numbers is :
[1, 2, 3]

Hopefully that all helps ;-)