Mar-31-2024, 10:59 AM
(This post was last modified: Mar-31-2024, 10:59 AM by Pedroski55.)
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)