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>
You can use the tuple swap idiom a, b = b, a
to swap the variable noting that for edge cases you need to wrap around the index index % len(seq)
Implementation
def swap_cards(seq, index):
indexes = (index, (index + 1)% len(seq))
seq[indexes[0]], seq[indexes[1]] = seq[indexes[1]], seq[indexes[0]]
return seq
Example
>>> swap_cards([3, 2, 1, 4, 5, 6, 0], 6)
[0, 2, 1, 4, 5, 6, 3]
>>> swap_cards([3, 2, 1, 4, 5, 6, 0], 5)
[3, 2, 1, 4, 5, 0, 6]