Mar-29-2024, 09:48 AM
(This post was last modified: Mar-29-2024, 09:48 AM by Pedroski55.)
To continue the saga: Can you explain why you chose the ifs and elifs you chose? I can't understand why you made the choices you made.
These 2 functions let you run things as many times as you want, more or less using your code.
Try it out, see what you get!
Your solution definitely works, it just seems a bit random in the choices you make!
This might take a long time on a large list!
These 2 functions let you run things as many times as you want, more or less using your code.
Try it out, see what you get!
from random import randint def makeList(): move_count = position = direction = 0 frog_list = [randint(0, 20) for i in range(6)] sorted_frogs = sorted(frog_list, reverse=True) return (move_count, frog_list, sorted_frogs) def loop(): start = makeList() move_count = position = direction = start[0] frog_list = start[1] sorted_frogs = start[2] while frog_list != sorted_frogs: if move_count == 0: print(f'Original frog_list = {frog_list}, position = {position}, direction = {direction}, move_count = {move_count}') if direction == 0: # direction starts as 0 if frog_list[position] == frog_list[-2]: # why -2? print('This is direction = 0, if 1') print(f'frog_list[position] = {frog_list[position]} , frog_list[-2] = {frog_list[-2]}') # simple bubble sort technique but how is that related to the above: if frog_list[position] == frog_list[-2]:?? print(f'frog_list[position] = {frog_list[position]}, frog_list[position + 1] = {frog_list[position + 1]}') frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position] print(f'Swapped around: frog_list[position] = {frog_list[position]}, frog_list[position + 1] = {frog_list[position + 1]}') position += 1 elif frog_list[position] == frog_list[-1]: # why -1? print('This is elif 1') direction = 1 elif frog_list[position + 2] > frog_list[position + 1]: # why +2 and plus +1? print('This is direction = 0, elif 2') print(f'frog_list[position + 2] = {frog_list[position + 2]}, frog_list[position + 1] = {frog_list[position + 1]}') frog_list[position], frog_list[position + 2] = frog_list[position + 2], frog_list[position] print(f'Swapped around: frog_list[position + 2] = {frog_list[position + 2]}, frog_list[position + 1] = {frog_list[position + 1]}') position += 2 else: # same bubble sort condition as above in first if of direction == 0 print('This is direction = 0, else') print(f'frog_list[position] = {frog_list[position]}, frog_list[position + 1] = {frog_list[position + 1]}') frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position] print(f'Swapped around: frog_list[position] = {frog_list[position]}, frog_list[position + 1] = {frog_list[position + 1]}') position += 1 elif direction == 1: if frog_list[position] == frog_list[1]: # why 1?? print('This is direction = 1, if 1') print(f'frog_list[position] = {frog_list[position]}, frog_list[position - 1] = {frog_list[position - 1]}') frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position] print(f'Swapped around: frog_list[position] = {frog_list[position]}, frog_list[position - 1] = {frog_list[position - 1]}') position -= 1 elif frog_list[position] == frog_list[0]: # why 0? print('This is direction = 1, elif 1') direction = 0 elif frog_list[position - 2] < frog_list[position - 1]: # why -2 and -1? print('This is direction = 1, elif 2') print(f'frog_list[position] = {frog_list[position]}, frog_list[position - 2] = {frog_list[position - 2]}') # reversed bubble sort condition frog_list[position], frog_list[position - 2] = frog_list[position - 2], frog_list[position] print(f'Swapped around: frog_list[position] = {frog_list[position]}, frog_list[position -2] = {frog_list[position - 2]}') position -= 2 else: print('This is direction = 1, else') print(f'frog_list[position] = {frog_list[position]}, frog_list[position - 1] = {frog_list[position - 1]}') # reversed bubble sort condition frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position] print(f'Swapped around: frog_list[position] = {frog_list[position]}, frog_list[position - 1] = {frog_list[position - 1]}') position -= 1 move_count += 1 print(f'frog_list = {frog_list}, position = {position}, direction = {direction}, move_count = {move_count}')Now just run loop() in your IDE.
Your solution definitely works, it just seems a bit random in the choices you make!
This might take a long time on a large list!