0

I'm new to programming. I started doing this question where I have to make sub-lists of descending values from the given list.

input_list=[7,1,6, 17, 18, 25, 25, 21, 11, 5 ,3 ,3,26,25]

The expected output should be :

descend_lists=[[7, 1], [25, 25, 21, 11, 5, 3, 3], [26, 25]]

I've no idea where to start from. The idea I have in my mind is I check ith and ith+1 elements together if ith element is greater than ith+1 element then add both elements to descend_list. Please help me out.

13
  • 2
    What criterion was used to generate descend_lists? Commented Dec 26, 2019 at 9:37
  • @norok2 descend_lists is a list of lists and every list present in descend_lists should be sorted in descending order. Commented Dec 26, 2019 at 9:41
  • 1
    but why not e.g. [[5, 3], [7, 1], [26, 25], ...] or any other "valid" combination? Commented Dec 26, 2019 at 9:59
  • In question it's mentioned the sequence should not be disturbed and minimum number of sublists. Commented Dec 26, 2019 at 9:59
  • what does that mean? To sort a sequence you must necessarily modify the order of the element unless it is already sorted. In your very example, the order is modified. Commented Dec 26, 2019 at 10:01

1 Answer 1

1

How I would do it would be to iterate over the original list considering a temporary list consisting of the current sub-list with descending order and pop it once it stops being in descending order.

def sublists(l):
    result = [] # the list of sub-lists
    sublist = [] # temporary sub-list kept in descending order
    for i in range(len(l)):
        sublist.append(l[i]) # add the element
        if(i == len(l) - 1 or l[i] < l[i+1]):
            result.append(sublist)
            sublist = []
    return result

In the if statement, what's happening is that you're stopping either when you reach the end of the list (i == len(l) - 1) or when you reach the end of the descending order (l[i] < l[i+1]). Notice, you need to write i == len(l) - 1 or l[i] < l[i+1] instead of l[i] < l[i+1] or i == len(l) - 1 otherwise you'd get an OutOfBounds error (accessing l[i+1] is illegal at that point.)

This will preserve all the elements in the list and produce all singletons for sorted lists (with distinct elements) instead of simply throwing them away. This, and the fact that I believe the code is nicer to read for a beginner in this form, is why I add my answer here over the answer of @Ch3steR

Sign up to request clarification or add additional context in comments.

3 Comments

[[7, 1], [6], [17], [18], [25, 25, 21, 11, 5, 3, 3], [26, 25]] this is the output im getting for your code. but i dont want [6] , [17] , [18]
@Jad Issa I threw those elements because OP wanted them to be thrown. Logically they shouldn't be you are right. And your code looks elegant. Deserves upvote.
Great! Then this offers a slightly different solution; I hope this helps for reference purposes!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.