Python swapping and slicing method

If last index is valid, i.e. there are at least that much cards to move in the array then you can do this:

def move_cards(cards):
    if cards[-1] == 3:
        return cards[2:-1] + cards[-3:-1] + [3]
    else:
        return cards[cards[-1]:-1] + cards[0:cards[-1]] + [cards[-1]]

move_cards([1, 2, 3, 4, 27, 28, 5, 6, 7, 2])
[3, 4, 27, 28, 5, 6, 7, 1, 2, 2]

move_cards([1,2,3,4,5,6,1,2,3])
[3, 4, 5, 6, 1, 2, 1, 2, 3]

Browse More Popular Posts

Leave a Comment