简体   繁体   中英

How to make ascending sublists in a list of integers go in descending order?

Working on some example questions, the particular one asks to make a function which would take a list and return a new one which would make every ascending sublist in the list go in descending order and leave the descending sublists as they are. For example, given the list [1,2,3,4,5] , I need the list [5,4,3,2,1] or given a list like [1,2,3,5,4,6,7,9,8] would return [5,3,2,1,9,7,6,4,8]

Here's what I have so far, but it does not do anything close to what I'd like it to do:

def example3(items):
    sublst = list()


    for i in items:

        current_element = [i]

        next_element = [i+1]

        if next_element > current_element:
            sublst = items.reverse()

        else:
            return items
    return sublst

print (example3([1,2,3,2])) #[[1, 2, 3, 2], [1, 2, 3, 2], [1, 2, 3, 2], [1, 2, 3, 2]]

EDIT: I feel like people are a little confused as to what I want to do in this case, heres a better example of what I'd like my function to do. Given a list like: [5, 7, 10, 4, 2, 7, 8, 1, 3] I would like it to return [10, 7, 5, 4, 8, 7, 2, 3, 1] . As you can see all the sublists that are in descending order such as ( [5,7,10] ) gets reversed to [10, 7, 5] .

It was a bit challenging to figure out what you need. I think you want something like as follows:

import random

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

while True:
    if len(l) == 0:
        break
    r = random.randint(0, len(l))
    bl.extend(l[r:None:-1])
    l = l[r+1:]

print(bl)

Out1:

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

Out2:

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

Out3:

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

Out4:

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

etc.

If you want a specific reverse random list:

import random

loop_number = 0
while True:
    l = [5, 7, 10, 4, 2, 7, 8, 1, 3]
    bl =[]
    while True:
        if len(l) == 0:
            break
        r = random.randint(0, len(l))
        bl.extend(l[r:None:-1])
        l = l[r+1:]
    loop_number += 1
    if bl == [10, 7, 5, 4, 8, 7, 2, 3, 1]:
        print(bl)
        print("I tried {} times".format(loop_number))
        break

Out:

[10, 7, 5, 4, 8, 7, 2, 3, 1]
I tried 336 times

The general algorithm is to keep track of the current ascending sublist you are processing using 2 pointers, perhaps a "start" and "curr" pointer. curr iterates over each element of the list. As long as the current element is greater than the previous element, you have an ascending sublist, and you move curr to the next number. If the curr number is less than the previous number, you know your ascending sublist has ended, so you collect all numbers from start to curr - 1 (because array[curr] is less than array[curr - 1] so it can't be part of the ascending sublist) and reverse them. You then set start = curr before incrementing curr .

You will have to deal with the details of the most efficient way of reversing them, as well as the edge cases with the pointers like what should the initial value of start be, as well as how to deal with the case that the current ascending sublist extends past the end of the array. But the above paragraph should be sufficient in getting you to think in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM