Python Forum
Frog codility leap sort variant
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frog codility leap sort variant
#6
I reviewed the OP and it does limit hops to 1 or 2 frogs. This is the code cleaned up a little with some comments.
def frog_sort(frogs):
    """Sort frogs in decreasing order.  Frogs move by hopping to
    the empty pad.  A frog cannot hop over more htan 1 frog
    """
    def hop(count):
        """Hop frogs[x+count] to frogs[x]."""
        nonlocal x
        frog = x + count
        frogs[frog], frogs[x], x = frogs[x], frogs[frog], frog
        moves.append(str(frogs))

    x = 0  # Index of the empty lily pad.
    count = len(frogs)
    direction = 1
    frogs = [0] + frogs  # Add empty lily pad.
    moves = [str(frogs)]  # Record of moves.

    # Uses an odd bubble sort variant, weaving the empty pad left and
    # right though the list of frogs, moving higher numbered frogs to
    # the left and lower numbered frogs to the right.
    while x or any(a < b for a, b in zip(frogs[1:], frogs[2:])):
        if direction == 1:
            # Moving through frogs left to right
            if x == count:
                direction = -1  # Hit end of list.  Reverse direction
            elif x == count-1:
                hop(1)
            elif frogs[x+2] > frogs[x+1]:
                hop(2)
            else:
                hop(1)
        else:
            # Moving through frogs right to left
            if x == 0:
                direction = 1  # Hit end of list.  Reverse direction
            elif x == 1:
                hop(-1)
            elif frogs[x-2] < frogs[x-1]:
                hop(-2)
            else:
                hop(-1)
    return frogs[1:], moves


frogs, moves = frog_sort([int(x) for x in input("Enter Frogs: ").split()])
print(*moves, len(moves)-1, sep="\n")
This sorts the frogs, but like a bubble sort it takes a lot of moves to do so.
Output:
Enter Frogs: 5 4 3 1 2 [0, 5, 4, 3, 1, 2] [5, 0, 4, 3, 1, 2] [5, 4, 0, 3, 1, 2] [5, 4, 3, 0, 1, 2] [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] 9
This is a better solution, but it took my exhaustive search algorithm 5 seconds to find it. Longer lists will take a really long time to solve.
Output:
Enter Frogs: 5 4 3 1 2 [0, 5, 4, 3, 1, 2] [4, 5, 0, 3, 1, 2] [4, 5, 3, 0, 1, 2] [4, 5, 3, 2, 1, 0] [4, 5, 3, 2, 0, 1] [4, 5, 3, 0, 2, 1] [4, 5, 0, 3, 2, 1] [0, 5, 4, 3, 2, 1] 7
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,511 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,389 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  Problem with code to calculate weekday for leap years! Event 2 2,928 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