(Mar-26-2024, 04:09 PM)deanhystad Wrote: [ -> ]While loop should be:
while frog_list != sorted_list:
Thank you, but it seems doesn't make any difference? Maybe there is more thing i should do?
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_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:
Enter Frogs: 5 1 3 4 2
[0, 5, 1, 3, 4, 2]
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 16, in <module>
NameError: name 'sorted_list' is not defined
[Program finished]
Or how about this, why the steps is inefficient? Line 16 sorted_frogs(?)
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)
Output:
Enter Frogs: 5 1 3 4 2
[0, 5, 1, 3, 4, 2]
[5, 0, 1, 3, 4, 2]
[5, 3, 1, 0, 4, 2]
[5, 3, 1, 4, 0, 2]
[5, 3, 1, 4, 2, 0]
[5, 3, 1, 4, 2, 0]
[5, 3, 1, 4, 0, 2]
[5, 3, 0, 4, 1, 2]
[5, 0, 3, 4, 1, 2]
[0, 5, 3, 4, 1, 2]
[0, 5, 3, 4, 1, 2]
[5, 0, 3, 4, 1, 2]
[5, 4, 3, 0, 1, 2]
[5, 4, 3, 2, 1, 0]
[5, 4, 3, 2, 1, 0]
[5, 4, 3, 2, 0, 1]
[5, 4, 3, 0, 2, 1]
[5, 4, 0, 3, 2, 1]
[5, 0, 4, 3, 2, 1]
[0, 5, 4, 3, 2, 1]
Minimum number of moves: 19
[Program finished]
My goal is to make this output instead
Output:
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
Thank you