samedi 26 janvier 2019

Python stop increment a list in certain condition

I am trying to implement a simple left rotation in a list. Please review the following code:def left_rotation(n,d,arr): temp = arr for i in range(n): print(temp[i]) # here is the problem if (i-d < 0): arr[i+n-d] = temp[i] else: arr[i-d] = temp[i] return arr print(left_rotation(5, 4,[1,2,3,4,5]))

When I put the if-else structure it fails to access the variable temp but when I remove the if-else, it works. How do you explain that ?

Aucun commentaire:

Enregistrer un commentaire