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.
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] isOutput:[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