-1

I am trying to write a code to take all the ascending order sequence from a list and print the sublist. How should I match the elements of the list?

I have tried iterating over the list and compare the elements but not working the way I want it to work.

def ascending_order_sequence(list1):
    sublist = []

    for i in range(len(no_list)):
        for j in range(i + 1, len(no_list) + 1):

Dont know how to compare and then fetch the sublists

TEST CASE 1: [4,5,9,7,1,4,3,8,10] OUTPUT: [4,5,9] [1,4] [3,8,10]

TEST CASE 2: [1,2,3,4,5,6] OUTPUT : [1,2,3,4,5,6]

TEST CASE 3: [8,6,2,1,0] OUTPUT: []

Nordle
  • 2,915
  • 3
  • 16
  • 34
Prithvi Singh
  • 97
  • 1
  • 10
  • Related question (not the same but could give a hint to the solution): https://stackoverflow.com/questions/51571422/split-sorted-array-into-list-with-sublists – Risadinha Jul 15 '19 at 06:50

1 Answers1

0
def func(d):
    res =[]
    tmp =[]
    val =d[0]
    for i in range(1,len(d)):
        if d[i]>=val:
            tmp.append(val)
            val =d[i]
        else:
            tmp.append(val)
            res.append(tmp)
            tmp =[]
            val = d[i]
    tmp.append(val)
    res.append(tmp)
    return res




l = [4,5,9,7,1,4,3,8,10,1]

print(func(l))  

output

 [[4, 5, 9], [7], [1, 4], [3, 8, 10], [1]]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44