Python Forum
Frog codility leap sort variant
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frog codility leap sort variant
#1
Question 
Frog codility leap sort variant

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)
My attempt :
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)   
Current output:
Output:
Enter Frogs: 5 1 3 4 2 [0, 5, 1, 3, 4, 2] [0, 5, 4, 3, 2, 1] 5 [Program finished]
Can anyone make the code show the steps
Desired output :
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
You can help me improve my code or you can share a whole code that do this better since i'm not really good at changing small part of code if you give me one, sorry for that, thank you so much
Reply


Messages In This Thread
Frog codility leap sort variant - by MoreMoney - Mar-31-2024, 03:11 AM
RE: Frog codility leap sort variant - by Pedroski55 - Apr-04-2024, 09:36 AM
RE: Frog codility leap sort variant - by deanhystad - Apr-04-2024, 06:01 PM
RE: Frog codility leap sort variant - by Pedroski55 - Apr-05-2024, 08:14 AM
RE: Frog codility leap sort variant - by deanhystad - Apr-05-2024, 07:31 PM
RE: Frog codility leap sort variant - by deanhystad - Apr-06-2024, 08:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tracking leap.py years for gregorian calendar (Exercism org) Drone4four 11 3,965 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  how to sort a list without .sort() function letmecode 3 3,512 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,227 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 49,391 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  Problem with code to calculate weekday for leap years! Event 2 2,929 Dec-15-2018, 05:13 PM
Last Post: Event

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020