Consider list(range(0, 5)), for example. I want the function to do this mapping:
(1, 4), (2, 1), (3, 2), (4, 3)
That is, it should return the previous value on the list and, if it can't do that (if it reaches index 0), it will "loop around" and output the list's last value.
I made this ugly solution:
def come_back(x, highest, head = 1):
if x == head:
newx = highest
else:
newx = x - 1
return newx
But it feels like there should be a more elegant one, probably based on Modular Arithmetic. Any clues?