Python Swap Function

前端 未结 3 892
执念已碎
执念已碎 2021-01-26 12:30

I\'m having a hard time expressing this in Python.

This is the description of what needs to be done.

swap_cards: (list of int, int) -> NoneType

3条回答
  •  醉梦人生
    2021-01-26 13:18

    def swap_cards(deck, index):
        if index in range(0, len(deck)):
            factor = (index + 1) % len(deck)
            aux = deck[factor]
            deck[factor] = deck[index]
            deck[index] = aux
            return deck
        else:
            return None
    
    deck = [3, 2, 1, 4, 5, 6, 0]
    
    new_deck = swap_cards(deck, 6)
    
    print new_deck
    

    Output:

    [0, 2, 1, 4, 5, 6, 3]
    

提交回复
热议问题