So the question is pretty strange. I wrote an algorithm to move contents of any list (array) by a given number of digits to the left.
DIGS = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
# move functions
def move(arr):
this = arr
first = this[0]
for b in range(len(this) - 1):
this[b] = this[b + 1]
this[-1] = first
return this
def move_with_step(arr, step):
this_arr = arr
for a in range(step):
this_arr = move(arr)
return this_arr
And, obviously, when typing print(move_with_step(DIGS, 5)
will give us the same DIGS array but twisted. It will be something like [ 5, 6, 7... 3, 4 ]. You get the idea. And in this case it works. BUT...
The problem is: if I'd put this same call into the
for
loop like below or just one after another, it will give me wrong results which is kinda strange because it should'n modify DIGS itself and why is that happening IDK.
So this code
for a in range(1, 6):
print(move_with_step(DIGS, a))
Returns this
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
in the console. Which is crazy and totally wrong. Why is that?