Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Steps
#11
(Mar-27-2024, 06:14 PM)deanhystad Wrote: @Pedroski55, the goal is not to sort the frogs. The goal is to move the frogs into sorted order using the fewest moves. There is also a restriction that a frog can only move to an open spot, the zero spot. That is my understanding of the problem from thr OP and some web searching for frog puzzle.

@MoreMoney, the main problem with your code is it makes moves that don't get you any closer to the solution. Pedroski pointed out that your solution moved frogs that were in the correct position. That's a waste compounded by a move that gets you further from the solution.

I suggest you pull out a notepad and a pencil and solve some of these puzzles by hand. I would start by using a very methodical approach. Move the correct frog to position 1, then correctly fill position 2 and so on until you fill al N positions.
Output:
[0, 5, 1, 3, 4, 2] Move 5 to position 1, Already there, no move required. [0, 5, 1, 3, 4, 2] Move 4 to position 2. 2 is not empty, so move frog from position 2 to the open position [1, 5, 0, 3, 4, 2] Now move 4 to position 2 [1, 5, 4, 3, 0, 2] Move 3 to position 3. Already there, no move required. [1, 5, 4, 3, 0, 2] Move 2 to position 4 [1, 5, 4, 3, 2, 0] Move 1 to position 5 [0, 5, 4, 3, 2, 1]
This is easily converted to pseudo code
for position from 1 to 5
    if frog[position] is not correct
        if frog[position] is not open
            move frog[position] to open the spot
       move correct frog into frog[position]
It is easy to see that the maximum possible number of moves for N frogs is 2N moves, two moves for each frog, but I have yet to test a puzzle that takes more than N+1 moves unless I allow repeating a number.

Another solition I tired is something I called "Find a hole, fill a hole". In this solution I look for a frog that is in the wrong space and I swap that frog with 0 Now I loop, moving the correct frog to the open position until the open position is frogs[0]. Repeat until I can no longer find any frogs in the wrong position.
func find_a_hole()
    for frog in frogs
        if frog is in wrong position
            move frog to open spot
            return True
    return False

while find_a_hole()
    while frogs[0] is not open:
        move correct frog to open position(
You are right thank you, im keep trying but can't implemented the logic yet

import random
import time
leapfrog = [0]
n = int(input('Masukkan jumlah angka pertama yang ingin anda sortir : '))
for i in range(1, n + 1):
    leapfrog_number = random.randint(1,100)
    leapfrog.append(leapfrog_number)
start = time.time()

print(leapfrog)

def even_leapfrog(leapfrog):
    shift_count = 0
    for i in range(0, n, 2):
        while leapfrog[i] < leapfrog[i + 2]:
            leapfrog[i], leapfrog[i + 2] = leapfrog[i + 2], leapfrog[i]
            shift_count += 1
            print(leapfrog)
    leapfrog[n], leapfrog[n - 1] = leapfrog[n - 1], leapfrog[n]
    shift_count += 1
    print(leapfrog)
    for i in range(n - 1, 1, -2):
        leapfrog[i], leapfrog[i - 2] = leapfrog[i - 2], leapfrog[i]
        shift_count += 1
        print(leapfrog)
    leapfrog[0], leapfrog[1] = leapfrog[1], leapfrog[0]
    shift_count += 1
    print(leapfrog)
    return shift_count

def odd_leapfrog(leapfrog):
    shift_count = 0
    for i in range(0, n, 2):
        try:
            while leapfrog[i] < leapfrog[i + 2]:
                leapfrog[i], leapfrog[i + 2] = leapfrog[i + 2], leapfrog[i]
                shift_count += 1
                print(leapfrog)
        except IndexError:
            leapfrog[n], leapfrog[n - 1] = leapfrog[n - 1], leapfrog[n]
            shift_count += 1
            print(leapfrog)
    for i in range(n, 1, -2):
        leapfrog[i], leapfrog[i - 2] = leapfrog[i - 2], leapfrog[i]
        shift_count += 1
        print(leapfrog)
    leapfrog[0], leapfrog[1] = leapfrog[1], leapfrog[0]
    shift_count += 1
    print(leapfrog)
    return shift_count

shift_count = 0
while leapfrog[1] < leapfrog[2]:
    try:
        if n % 2 == 1:
            shift_count += odd_leapfrog(leapfrog)
            end = time.time()
            period = end - start
        else:
            shift_count += even_leapfrog(leapfrog)
            end = time.time()
            period = end - start
    except (IndexError, ValueError):
        break

print("Jumlah pergeseran:", shift_count)
print("Waktu yang dibutuhkan adalah:", period, " ms")
Would you like to give me a code to replace my code logic on bottom?

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)   
Thank you for helping me

Thanks for trying to help me, sorry for making you confused, would you like to help me in this one? @Pedroski55
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,156 May-23-2019, 10:21 AM
Last Post: heiner55
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,234 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