I am trying to left rotate an array in python3. The code that I am using is:
def rotLeft(a, d):
b = a
lengthOfArray = len(a)
shift = d
for i in range (0,lengthOfArray):
print(a)
newLocation = (i + (lengthOfArray - shift)) % lengthOfArray
b[newLocation] = a[i]
return b
if __name__ == '__main__':
nd = input().split()
n = int(nd[0])
d = int(nd[1])
a = list(map(int, input().rstrip().split()))
result = rotLeft(a, d)
print(result)
It takes three inputs from the user which are the length of the array of the elements, the number of rotations to perform and the array itself. But, instead of the elements rotating, the array is getting filled with the first element in the same position. For example:
Inputs:
5 4
12 76 1 09 13
Output:
[12, 12, 12, 12, 12]
What is wrong with my algorithm and how can I fix it?