minimize frog steps as possible, trying to minimize and efficient the frog step.
The rule is given the last step 0 must be at most left, then big number to smaller number, its seems there is some inefficient on my coding
I also want to make it able to input my custom random number like i want to put [0,55,12,74,12,39] and make it work
Note : Frog sorting program. Can input frog numbers randomly. Prepare a list/place to input frog numbers during the presentation later. The frogs at the start are arranged randomly, with the empty/0 spot on the far left.
Here is my try:
#homework
The rule is given the last step 0 must be at most left, then big number to smaller number, its seems there is some inefficient on my coding
I also want to make it able to input my custom random number like i want to put [0,55,12,74,12,39] and make it work
Note : Frog sorting program. Can input frog numbers randomly. Prepare a list/place to input frog numbers during the presentation later. The frogs at the start are arranged randomly, with the empty/0 spot on the far left.
Here is my try:
frog_count = int(input('Enter the number of frogs: ')) print() min_interval = int(input('Enter the minimum interval: ')) max_interval = int(input('Enter the maximum interval: ')) print() from random import randint frog_list = [0] if frog_count >= 1 else [] sorted_list = [] for index in range(1, frog_count + 1): random_value = randint(min_interval, max_interval) frog_list.append(random_value) sorted_list.append(random_value) print(frog_list) sorted_list.sort(reverse=True) move_count = 0 position = 0 direction = 0 print(frog_list) if frog_count == 1: move_count = 0 else: while frog_list[1:] != sorted_list: 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)........................output
Output:Enter the number of frogs: 5
Enter the minimum interval: 1
Enter the maximum interval: 100
[0, 58, 66, 97, 51, 82]
[0, 58, 66, 97, 51, 82]
[66, 58, 0, 97, 51, 82]
[66, 58, 97, 0, 51, 82]
[66, 58, 97, 82, 51, 0]
[66, 58, 97, 82, 51, 0]
[66, 58, 97, 82, 0, 51]
[66, 58, 97, 0, 82, 51]
[66, 0, 97, 58, 82, 51]
[0, 66, 97, 58, 82, 51]
[0, 66, 97, 58, 82, 51]
[97, 66, 0, 58, 82, 51]
[97, 66, 82, 58, 0, 51]
[97, 66, 82, 58, 51, 0]
[97, 66, 82, 58, 51, 0]
[97, 66, 82, 58, 0, 51]
[97, 66, 82, 0, 58, 51]
[97, 0, 82, 66, 58, 51]
[0, 97, 82, 66, 58, 51]
Minimum number of moves: 17
[Program finished]#homework
deanhystad write Mar-22-2024, 03:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please don't double post.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please don't double post.