Python Forum
Frog Puzzle Random To Custom
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frog Puzzle Random To Custom
#1
Question 
I just want to make the input to custom number instead of randomized number ie i want my code to sort this [0,5,1,3,4,2] instead of randomized number, can anyone revise my whole code? Thank you so much

I already try putting and changing the random part but it wont work

https://pastebin.com/xhnEUwf9

note : My code is sorting algorithm that sorts a list of numbers (representing frogs) in descending order. The sorting is done through a series of swaps, and the algorithm counts the number of moves required to achieve the sorted order.
Here's a breakdown of the sorting rule applied in my code:

**Initialization**: A list of frogs is created with random numbers between the lower_bound and upper_bound, and a 0 is appended at the beginning. The same list is copied to frogs_sorted.

**Sorting**: The frogs_sorted list is sorted in reverse order (descending).

**Swapping Mechanism**:
- The algorithm performs a loop until the sublist of frogs from index 1 to n matches the sorted list frogs_sorted.
- It uses two pointers, p and q, to traverse the list from left to right (q == 0) and right to left (q == 1).
- Swaps are made between adjacent elements or by skipping one element if it results in a larger number moving towards its correct position.
- The pointer p is incremented or decremented accordingly, and q is toggled when the end or the beginning of the list is reached.

**Counting Moves**: Each swap counts as a move, and the total number of moves is recorded in total_moves.
This algorithm is not a standard sorting algorithm like Bubble Sort, Selection Sort, or Quick Sort. It's a custom algorithm that seems to be designed for a specific problem, possibly a variation of the "frog puzzle" where the goal is to sort the frogs in a particular order with the least number of moves.
Frog sorting program. The task is Prepare a list/place to input frog numbers during the presentation later instead of random number. The frogs at the start are arranged randomly, with the empty/0 spot on the far left.
Reply
#2
I think what you want to do is a bubble sort. Or maybe you want to know how sorted() works?

Your code doesn't work and seems complicated.

This implementation of the bubble sort may help you visualise the way bubble sort works.

from random import randint

frogs = [randint(0, 20) for i in range(10)]
#repeating loop len(frogs)(number of elements) number of times
moves = 0
for j in range(len(frogs)):
    #initially swapped is false
    swapped = False
    i = 0
    while i<len(frogs)-1:
        #comparing the adjacent elements
        if frogs[i]>frogs[i+1]:
            #swapping
            frogs[i],frogs[i+1] = frogs[i+1],frogs[i]
            #Changing the value of swapped
            swapped = True
            moves +=1
        i = i+1
        print (f'frogs = {frogs}, i = {i}, moves = {moves}')
    #if swapped is false then the list is sorted
    #we can stop the loop
    if swapped == False:
        break
Reply
#3
Following your private message:

I think perhaps you are concentrating on a list with only 4 elements, but that is not helpful.

You need to be able to sort any list.

Try this way to bubble sort:

from random import randint

# Helper variables
moves = 0
# set q = True or the while loop will not start
q = True
# make a random list
# you can change the numbers here: randint(0, 20) and here: range(10)
frogs = [randint(0, 20) for i in range(10)]
print(f'Start situation frogs = {frogs}')
# the while loop will keep going until there is nothing to swap around
# then q = False and the while loop will stop
# don't set: while True because True is always True, the while loop will not stop
while q:
    q = False  # Reset q for this pass
    # keep looping through the list until there is nothing left to swap
    for i in range(1, len(frogs)):
        if frogs[i - 1] > frogs[i]:
            # Swap the elements
            frogs[i - 1], frogs[i] = frogs[i], frogs[i - 1]
            q = True  # Set q to True if a swap occurs
            moves +=1
        print (f'frogs = {frogs}, i = {i}, moves = {moves}') 
MoreMoney likes this post
Reply
#4
Quote:I just want to make the input to custom number instead of randomized number ie i want my code to sort this [0,5,1,3,4,2] instead of randomized number,
It is easy to convert a string like "5 1 3 4 2" to a list of ints using split(). First split the string into a list of numeric strings using split(), then use int() to convert the strings to numbers.
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

print(frog_list, sorted_frogs, frog_count)
I give you this code because I think you need to test your frog sort. When I run your code with 58 66 97 51 8, it prints this:
Output:
[0, 58, 66, 97, 51, 8] [66, 58, 0, 97, 51, 8] [66, 58, 97, 0, 51, 8] [66, 58, 97, 51, 0, 8] [66, 58, 97, 51, 8, 0] [66, 58, 97, 51, 8, 0] [66, 58, 97, 51, 0, 8] [66, 58, 97, 0, 51, 8] [66, 0, 97, 58, 51, 8] [0, 66, 97, 58, 51, 8] [0, 66, 97, 58, 51, 8] [97, 66, 0, 58, 51, 8] [97, 66, 58, 0, 51, 8] [97, 66, 58, 51, 0, 8] [97, 66, 58, 51, 8, 0] [97, 66, 58, 51, 8, 0] [97, 66, 58, 51, 0, 8] [97, 66, 58, 0, 51, 8] [97, 66, 0, 58, 51, 8] [97, 0, 66, 58, 51, 8] [0, 97, 66, 58, 51, 8] Minimum number of moves: 20
This is the correct order (assuming 0 should end in the leftmost slot), but the solution is not optimal. Looking at your approach to the problem, you'll only find the optimal solution by luck of it being the only solution you try.

I think the output should be
Output:
[0, 58, 66, 97, 51, 8] [58, 0, 66, 97, 51, 8] [58, 97, 66, 0, 51, 8] [0, 97, 66, 58, 51, 8] Minimum number of moves: 3
And the solution for [0, 5, 1, 3, 4, 2] is
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
Reply
#5
(Mar-25-2024, 09:53 PM)deanhystad Wrote:
Quote:I just want to make the input to custom number instead of randomized number ie i want my code to sort this [0,5,1,3,4,2] instead of randomized number,
It is easy to convert a string like "5 1 3 4 2" to a list of ints using split(). First split the string into a list of numeric strings using split(), then use int() to convert the strings to numbers.
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

print(frog_list, sorted_frogs, frog_count)
I give you this code because I think you need to test your frog sort. When I run your code with 58 66 97 51 8, it prints this:
Output:
[0, 58, 66, 97, 51, 8] [66, 58, 0, 97, 51, 8] [66, 58, 97, 0, 51, 8] [66, 58, 97, 51, 0, 8] [66, 58, 97, 51, 8, 0] [66, 58, 97, 51, 8, 0] [66, 58, 97, 51, 0, 8] [66, 58, 97, 0, 51, 8] [66, 0, 97, 58, 51, 8] [0, 66, 97, 58, 51, 8] [0, 66, 97, 58, 51, 8] [97, 66, 0, 58, 51, 8] [97, 66, 58, 0, 51, 8] [97, 66, 58, 51, 0, 8] [97, 66, 58, 51, 8, 0] [97, 66, 58, 51, 8, 0] [97, 66, 58, 51, 0, 8] [97, 66, 58, 0, 51, 8] [97, 66, 0, 58, 51, 8] [97, 0, 66, 58, 51, 8] [0, 97, 66, 58, 51, 8] Minimum number of moves: 20
This is the correct order (assuming 0 should end in the leftmost slot), but the solution is not optimal. Looking at your approach to the problem, you'll only find the optimal solution by luck of it being the only solution you try.

I think the output should be
Output:
[0, 58, 66, 97, 51, 8] [58, 0, 66, 97, 51, 8] [58, 97, 66, 0, 51, 8] [0, 97, 66, 58, 51, 8] Minimum number of moves: 3
And the solution for [0, 5, 1, 3, 4, 2] is
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
Thanks, you're right, thank you for correcting my code, but how can i make it show the steps, thank you
Output:
Enter Frogs: 5 1 3 4 2 [0, 5, 1, 3, 4, 2] [0, 5, 4, 3, 2, 1] 5 [Program finished]
Also here my attempt

# Removed the random import as it's no longer needed

frogs = []
frogs_sorted = []
n = int(input("Enter the number of frogs: "))

# Prompt the user to input the numbers
print("Enter the numbers for the frogs, separated by commas:")
user_input = input()

# Convert the user input into a list of integers
frogs = list(map(int, user_input.split(',')))
angka_sebelum_disortir = frogs.copy() # Copy the list to display later
frogs_sorted = frogs.copy() # Copy the list for sorting

# Menampilkan angka sebelum disortir
print("Angka sebelum disortir:", angka_sebelum_disortir)

# Helper variables
moves = 0
p = 0
q = 0

frogs_sorted = sorted(frogs_sorted, reverse=True)
if n == 1:
    total_moves = 0
else:
    while True:
        if frogs[1:] == frogs_sorted[:n]:
            break
        # Move from left to right
        if q == 0:
            if frogs[p] == frogs[-2]:
                frogs[p], frogs[p+1] = frogs[p+1], frogs[p]
                p += 1
            elif frogs[p] == frogs[-1]:
                q = 1
            elif frogs[p+2] > frogs[p+1]:
                frogs[p], frogs[p+2] = frogs[p+2], frogs[p]
                p += 2
            else:
                frogs[p], frogs[p+1] = frogs[p+1], frogs[p]
                p += 1
        # Move from right to left
        if q == 1:
            if frogs[p] == frogs[1]:
                frogs[p], frogs[p-1] = frogs[p-1], frogs[p]
                p -= 1
            elif frogs[p] == frogs[0]:
                q = 0
            elif frogs[p-2] < frogs[p-1]:
                frogs[p], frogs[p-2] = frogs[p-2], frogs[p]
                p -= 2
            else:
                frogs[p], frogs[p-1] = frogs[p-1], frogs[p]
                p -= 1
        moves += 1
        print(frogs)
    total_moves = moves
print("Total moves: ", total_moves)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Codility Frog River One Westerner 2 2,415 Jan-09-2021, 06:35 PM
Last Post: deanhystad
  Slider puzzle captcha resolver, how do this? dw0rd1337 0 3,492 Jan-04-2021, 11:55 PM
Last Post: dw0rd1337
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,767 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  how to program robot to pass wise man puzzle steven12341234 0 1,949 Dec-02-2018, 08:31 AM
Last Post: steven12341234
  Need help with this coding puzzle torchtopher 1 2,104 Jun-22-2018, 01:14 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020