Posts: 17
Threads: 5
Joined: Mar 2024
Mar-23-2024, 10:39 AM
(This post was last modified: Mar-23-2024, 10:39 AM by MoreMoney.)
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.
Posts: 1,094
Threads: 143
Joined: Jul 2017
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from random import randint
frogs = [randint( 0 , 20 ) for i in range ( 10 )]
moves = 0
for j in range ( len (frogs)):
swapped = False
i = 0
while i< len (frogs) - 1 :
if frogs[i]>frogs[i + 1 ]:
frogs[i],frogs[i + 1 ] = frogs[i + 1 ],frogs[i]
swapped = True
moves + = 1
i = i + 1
print ( f 'frogs = {frogs}, i = {i}, moves = {moves}' )
if swapped = = False :
break
|
Posts: 1,094
Threads: 143
Joined: Jul 2017
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from random import randint
moves = 0
q = True
frogs = [randint( 0 , 20 ) for i in range ( 10 )]
print ( f 'Start situation frogs = {frogs}' )
while q:
q = False
for i in range ( 1 , len (frogs)):
if frogs[i - 1 ] > frogs[i]:
frogs[i - 1 ], frogs[i] = frogs[i], frogs[i - 1 ]
q = True
moves + = 1
print ( f 'frogs = {frogs}, i = {i}, moves = {moves}' )
|
MoreMoney likes this post
Posts: 6,815
Threads: 20
Joined: Feb 2020
Mar-25-2024, 09:53 PM
(This post was last modified: Mar-25-2024, 09:53 PM by deanhystad.)
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.
1 2 3 4 5 6 7 8 |
input_str = input ( "Enter Frogs: " )
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
Posts: 17
Threads: 5
Joined: Mar 2024
Mar-26-2024, 08:38 AM
(This post was last modified: Mar-26-2024, 08:39 AM by MoreMoney.)
(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.
1 2 3 4 5 6 7 8 |
input_str = input ( "Enter Frogs: " )
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
frogs = []
frogs_sorted = []
n = int ( input ( "Enter the number of frogs: " ))
print ( "Enter the numbers for the frogs, separated by commas:" )
user_input = input ()
frogs = list ( map ( int , user_input.split( ',' )))
angka_sebelum_disortir = frogs.copy()
frogs_sorted = frogs.copy()
print ( "Angka sebelum disortir:" , angka_sebelum_disortir)
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
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
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)
|
|