Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Steps
#13
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!

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!
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,668 Oct-10-2020, 05:35 PM
Last Post: perfringo
  Keep Toggle View through Slider Steps yourboyjoe 1 1,740 Aug-10-2020, 07:32 PM
Last Post: Yoriz
  Files to store configuration and steps for a industrial process control application Danieru 1 1,865 Aug-03-2020, 05:43 PM
Last Post: buran
  Pexpect baby steps slouw 9 8,157 May-23-2019, 10:21 AM
Last Post: heiner55
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,236 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  first steps with python3 hunnimonstr 5 4,681 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