Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Steps
#20
You could try this, it seems to work. This doesn't have any if logic that I do not understand.

from random import randint

# see if a list is in descending order
# shortcut out of the loop if True
def checkOrder(alist):
    for i in range(len(alist) - 1):
        if not alist[i] >= alist[i+1]:
            return False        
    return True

# zero is at position 0 at the start
def swap(alist):
    if not alist[0] == 0:
        print('This is if not alist[0] == 0 ... ')
        temp = alist[0]
        #index0 = alist.index(0)
        index_max = alist.index(max(alist))
        final.append(alist[index_max])
        alist[0] = alist[index_max]
        alist[index_max] = temp
        print(f'current list = {alist}, final = {final}')
    else:
        print('This is else: alist[0] == 0 ... ')
        index_max = alist.index(max(alist))
        temp = alist[index_max]
        alist[index_max] = 0
        alist[0] = temp
        final.append(temp)
        print(f'current list = {alist}, final = {final}')        
    return alist[1:len(alist)]
    

def loop(nl, moves):
    while len(nl) >= 2:
        print(f'nl before swap = {nl}')
        nl = swap(nl)
        if checkOrder(nl):
            for n in range(len(nl)-1):
                final.append(nl[n])
            moves +=1
            print(f'nl after swap and checkOrder() = {nl}, final = {final}, moves = {moves}')
            return
        moves +=1
        print(f'nl after swap = {nl}, final = {final}, moves = {moves}')
    return

for j in range(3):
    print(f'\nloop {j+1}\n')
    #frogs = [randint(1, 10) for i in range(6)]
    frogs = [randint(1, 15) for i in range(15)]
    frogs.insert(0, 0)
    nl = frogs.copy()
    final = [0]
    moves = 0
    loop(nl, moves)
Reply


Messages In This Thread
Sorting Steps - by MoreMoney - Mar-26-2024, 02:16 PM
RE: Sorting Steps - by deanhystad - Mar-26-2024, 02:27 PM
RE: Sorting Steps - by MoreMoney - Mar-26-2024, 03:22 PM
RE: Sorting Steps - by deanhystad - Mar-26-2024, 03:24 PM
RE: Sorting Steps - by MoreMoney - Mar-26-2024, 03:40 PM
RE: Sorting Steps - by deanhystad - Mar-26-2024, 04:09 PM
RE: Sorting Steps - by MoreMoney - Mar-26-2024, 04:42 PM
RE: Sorting Steps - by deanhystad - Mar-27-2024, 02:39 AM
RE: Sorting Steps - by Pedroski55 - Mar-27-2024, 09:34 AM
RE: Sorting Steps - by deanhystad - Mar-27-2024, 06:14 PM
RE: Sorting Steps - by MoreMoney - Mar-28-2024, 07:30 AM
RE: Sorting Steps - by deanhystad - Mar-28-2024, 04:47 PM
RE: Sorting Steps - by Pedroski55 - Mar-29-2024, 09:48 AM
RE: Sorting Steps - by deanhystad - Mar-29-2024, 06:20 PM
RE: Sorting Steps - by Pedroski55 - Mar-29-2024, 09:10 PM
RE: Sorting Steps - by deanhystad - Mar-29-2024, 10:55 PM
RE: Sorting Steps - by MoreMoney - Mar-30-2024, 03:15 PM
RE: Sorting Steps - by Pedroski55 - Mar-30-2024, 07:54 AM
RE: Sorting Steps - by MoreMoney - Mar-30-2024, 03:10 PM
RE: Sorting Steps - by Pedroski55 - Mar-31-2024, 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Next steps for using API data Rebster 6 2,736 Oct-10-2020, 05:35 PM
Last Post: perfringo
  Keep Toggle View through Slider Steps yourboyjoe 1 1,767 Aug-10-2020, 07:32 PM
Last Post: Yoriz
  Files to store configuration and steps for a industrial process control application Danieru 1 1,880 Aug-03-2020, 05:43 PM
Last Post: buran
  Pexpect baby steps slouw 9 8,245 May-23-2019, 10:21 AM
Last Post: heiner55
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,310 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  first steps with python3 hunnimonstr 5 4,754 Jul-03-2017, 08:49 PM
Last Post: hunnimonstr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020