Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Steps
#7
(Mar-26-2024, 04:09 PM)deanhystad Wrote: While loop should be:
    while frog_list != sorted_list:
Thank you, but it seems doesn't make any difference? Maybe there is more thing i should do?

input_str = input("Enter Frogs: ")  # User types 5 1 3 4 2 <enter>.  Do not type the zero.
frog_list = [0]
for number_str in input_str.split():
    frog_list.append(int(number_str))
sorted_frogs = [0] + sorted(frog_list[1:], reverse=True)
frog_count = len(frog_list) - 1
 
move_count = 0
position = 0
direction = 0
 
print(frog_list)
if frog_count == 1:
    move_count = 0
else:
    while frog_list != sorted_list:
        if direction == 0:
            if frog_list[position] == frog_list[-2]:
                frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
                position += 1
            elif frog_list[position] == frog_list[-1]:
                direction = 1
            elif frog_list[position + 2] > frog_list[position + 1]:
                frog_list[position], frog_list[position + 2] = frog_list[position + 2], frog_list[position]
                position += 2
            else:
                frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
                position += 1
        elif direction == 1:
            if frog_list[position] == frog_list[1]:
                frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
                position -= 1
            elif frog_list[position] == frog_list[0]:
                direction = 0
            elif frog_list[position - 2] < frog_list[position - 1]:
                frog_list[position], frog_list[position - 2] = frog_list[position - 2], frog_list[position]
                position -= 2
            else:
                frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
                position -= 1
         
        move_count += 1
        print(frog_list)
 
    move_count = move_count
 
print('Minimum number of moves: ', move_count)   
Output:
Enter Frogs: 5 1 3 4 2 [0, 5, 1, 3, 4, 2] Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module> start(fakepyfile,mainpyfile) File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start exec(open(mainpyfile).read(), __main__.__dict__) File "<string>", line 16, in <module> NameError: name 'sorted_list' is not defined [Program finished]
Or how about this, why the steps is inefficient? Line 16 sorted_frogs(?)

input_str = input("Enter Frogs: ")  # User types 5 1 3 4 2 <enter>.  Do not type the zero.
frog_list = [0]
for number_str in input_str.split():
    frog_list.append(int(number_str))
sorted_frogs = [0] + sorted(frog_list[1:], reverse=True)
frog_count = len(frog_list) - 1
 
move_count = 0
position = 0
direction = 0
 
print(frog_list)
if frog_count == 1:
    move_count = 0
else:
    while frog_list != sorted_frogs:
        if direction == 0:
            if frog_list[position] == frog_list[-2]:
                frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
                position += 1
            elif frog_list[position] == frog_list[-1]:
                direction = 1
            elif frog_list[position + 2] > frog_list[position + 1]:
                frog_list[position], frog_list[position + 2] = frog_list[position + 2], frog_list[position]
                position += 2
            else:
                frog_list[position], frog_list[position + 1] = frog_list[position + 1], frog_list[position]
                position += 1
        elif direction == 1:
            if frog_list[position] == frog_list[1]:
                frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
                position -= 1
            elif frog_list[position] == frog_list[0]:
                direction = 0
            elif frog_list[position - 2] < frog_list[position - 1]:
                frog_list[position], frog_list[position - 2] = frog_list[position - 2], frog_list[position]
                position -= 2
            else:
                frog_list[position], frog_list[position - 1] = frog_list[position - 1], frog_list[position]
                position -= 1
         
        move_count += 1
        print(frog_list)
 
    move_count = move_count
 
print('Minimum number of moves: ', move_count)   
Output:
Enter Frogs: 5 1 3 4 2 [0, 5, 1, 3, 4, 2] [5, 0, 1, 3, 4, 2] [5, 3, 1, 0, 4, 2] [5, 3, 1, 4, 0, 2] [5, 3, 1, 4, 2, 0] [5, 3, 1, 4, 2, 0] [5, 3, 1, 4, 0, 2] [5, 3, 0, 4, 1, 2] [5, 0, 3, 4, 1, 2] [0, 5, 3, 4, 1, 2] [0, 5, 3, 4, 1, 2] [5, 0, 3, 4, 1, 2] [5, 4, 3, 0, 1, 2] [5, 4, 3, 2, 1, 0] [5, 4, 3, 2, 1, 0] [5, 4, 3, 2, 0, 1] [5, 4, 3, 0, 2, 1] [5, 4, 0, 3, 2, 1] [5, 0, 4, 3, 2, 1] [0, 5, 4, 3, 2, 1] Minimum number of moves: 19 [Program finished]
My goal is to make this output instead

Output:
Output: [0, 5, 1, 3, 4, 2] [2, 5, 1, 3, 4, 0] [2, 5, 0, 3, 4, 1] [2, 5, 4, 3, 0, 1] [0, 5, 4, 3, 2, 1] Minimum number of moves: 4
Thank you
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,739 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