πΉStep 1 — Understand range(len(arr))
len(arr) = 4, so:
range(4) → 0, 1, 2, 3So the loop runs 4 times with i = 0, 1, 2, 3.
πΉ Step 2 — Understand negative indexing
In Python:
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Value | 1 | 2 | 3 | 4 |
| Negative index | -4 | -3 | -2 | -1 |
πΉ Step 3 — Iterate and modify the list
Iteration 1: i = 0
arr[0] = arr[-0]But -0 == 0, so:
arr[0] = arr[0] = 1Array remains:
π [1, 2, 3, 4]
Iteration 2: i = 1
arr[1] = arr[-1]arr[-1] = 4
So:
arr[1] = 4Array becomes:
π [1, 4, 3, 4]
Iteration 3: i = 2
arr[2] = arr[-2]arr[-2] = 3
So:
arr[2] = 3Array becomes:
π [1, 4, 3, 4] (no change here)
Iteration 4: i = 3
arr[3] = arr[-3]arr[-3] = 4 (because arr is now [1,4,3,4])
So:
arr[3] = 4Final array:
π [1, 4, 3, 4]


0 Comments:
Post a Comment