Mar-30-2024, 03:10 PM
(Mar-30-2024, 07:54 AM)Pedroski55 Wrote: Thanks for that!
The old clock finally ticked, I remember vaguely having heard of this frog puzzle in school!
Frog may only land on empty lilypad!
Got it at last!
Thanks, i got the idea but i can't find the right code to do that, do you have code that can do my sort?
The best i can do is this
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)