The code is supposed to make a copy of the original array and rotate it (turn the columns into rows) while keeping the original array the same but the original array changes without a reason.
This is most likely a deep copy issue but I have tried using copy.copy() and it has not worked
Code:
l = [[1,2,3],[4,5,6],[7,8,9]]
def rotate(funcl):
funcl = funcl[::-1]
for s1 in range(0, len(funcl)):
for s2 in range(s1, len(funcl)):
funcl[s1][s2], funcl[s2][s1] = funcl[s2][s1], funcl[s1][s2]
return funcl
print("Original Array:\n", l)
nextl = rotate(l)
print("Original Array after Function:\n", l)
print("New Rotated Array:\n", nextl)
Output:
Original Array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Original Array after Function:
[[9, 6, 3], [8, 5, 2], [7, 4, 1]]
New Rotated Array:
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
As you can see the function works fine but changes the original array
l.copy()).nextl = rotate(l.copy())