[Solved] Create list of list of numbers. If sorting is wrong a new sublist should be created


Algorithm

input = [2,5,1,4,7,3,1,2,3]

output = [[]]
for idx,val in enumerate(input):
    if idx > 0 and input[idx-1] > input[idx]:
        output.append([val])
    else:
        output[-1].append(val)

print output

Output is

[[2, 5], [1, 4, 7], [3], [1, 2, 3]]

Explanation of the algorithm in words:

Create an output list with an empty sublist. Enumerate over the input list. If the previous element (if one exists) is not bigger than the actual element add the element to the last sublist of the output. If it’s bigger create a new sublist in output and add it to this new sublist.

4

solved Create list of list of numbers. If sorting is wrong a new sublist should be created