(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.You are right thank you, im keep trying but can't implemented the logic yet
@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.
This is easily converted to pseudo code
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]
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(
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