Python Forum
Frog codility leap sort variant
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frog codility leap sort variant
#5
@Pedroski55, your code is an insertion sort. You find the max value and insert it in [1], the second largest value goes in [2] and so on. Insertions are a little odd because you use [0] as a temporary landing spot for the frogs swapping places. It could be written like this:
def frog_sort(frogs):
    """Sort frogs in decreasing order."""
    def swap(a, b):
        """Move frog from from pad a to pad b using pad 0 for the exchange."""
        frogs[0], frogs[a] = frogs[a], frogs[0]
        moves.append(str(frogs))
        frogs[b], frogs[a] = frogs[a], frogs[b]
        moves.append(str(frogs))
        frogs[0], frogs[b] = frogs[b], frogs[0]
        moves.append(str(frogs))

    frogs = [0] + frogs
    moves = [str(frogs)]
    for i in range(1, len(frogs)):
        max_index = frogs.index(max(frogs[i:]))
        if max_index != i:
            swap(i, max_index)
    return moves

print(*frog_sort([int(x) for x in input("Enter frogs: ").split()]), sep="\n")
Output:
Enter frogs: 1 2 3 4 5 [0, 1, 2, 3, 4, 5] [1, 0, 2, 3, 4, 5] [1, 5, 2, 3, 4, 0] [0, 5, 2, 3, 4, 1] [2, 5, 0, 3, 4, 1] [2, 5, 4, 3, 0, 1] [0, 5, 4, 3, 2, 1]
For this particular arrangement of frogs, this is the least number of moves to sort the frogs.

Sorting this arrangment of frogs wastes several moves retuning the empty lily pad back to zero.
Output:
Enter frogs: 1 5 2 4 3 [0, 1, 5, 2, 4, 3] [1, 0, 5, 2, 4, 3] [1, 5, 0, 2, 4, 3] [0, 5, 1, 2, 4, 3] [1, 5, 0, 2, 4, 3] [1, 5, 4, 2, 0, 3] [0, 5, 4, 2, 1, 3] [2, 5, 4, 0, 1, 3] [2, 5, 4, 3, 1, 0] [0, 5, 4, 3, 1, 2] [1, 5, 4, 3, 0, 2] [1, 5, 4, 3, 2, 0] [0, 5, 4, 3, 2, 1]
Changing the code to not return the empty lily pad to [0] each time reduces the number of moves, but we can no longer use max(frogs[i:] ) to find the frog to move to frogs[i]. The frog that belons in frogs[i] might be in frogs[0]. That is where having a sorted list comes in handy.
def frog_sort(frogs):
    """Sort frogs in decreasing order."""
    def swap(index):
        """Move frog from index to the empty pad."""
        nonlocal empty
        frogs[empty], frogs[index], empty = frogs[index], frogs[empty], index
        moves.append(str(frogs))

    empty = 0
    order = [0] + sorted(frogs, reverse=True)
    frogs = [0] + frogs
    moves = [str(frogs)]
    for dst in range(1, len(frogs)):
        src = frogs.index(order[dst])
        if src != dst:
            if dst != empty:
                swap(dst)
            swap(src)
    return moves

print(*frog_sort([int(x) for x in input("Enter frogs: ").split()]), sep="\n")
Output:
Enter frogs: 1 5 2 4 3 [0, 1, 5, 2, 4, 3] [1, 0, 5, 2, 4, 3] [1, 5, 0, 2, 4, 3] [1, 5, 4, 2, 0, 3] [1, 5, 4, 0, 2, 3] [1, 5, 4, 3, 2, 0] [0, 5, 4, 3, 2, 1]
Neither of these use the "limited leap" that's been discussed recently. That adds an interesting twist to the problem and I can see that an "out and back" solution where you bubble the smallest value to the right, then bubble back larger values to the left might work. That could be what the OP was doing the "direction" flag.
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 4,074 Oct-14-2022, 03:20 PM
Last Post: betinajessen
  how to sort a list without .sort() function letmecode 3 3,549 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,268 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 49,587 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  Problem with code to calculate weekday for leap years! Event 2 2,963 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