0
Can Someone Explain This? 3
Can Someone Explain This? I don't understand how this code works. Could you explain this? https://code.sololearn.com/c9n0k8F300bV/?ref=app
10 Respuestas
+ 1
arr[0], arr[arr[0]-1]=arr[arr[0]-1], arr[0]
arr[0], arr[arr[0]-1]=2,4
arr[0]=2
arr[arr[0]-1]=arr[2-1]=arr[1]=4
+ 5
When making assignments like this, the value doesn't change until they have been assigned to the different variable or indexes as this case is
Simpler example:
a = 5
b = 6
a, b = a-3, a-5
print(a,b)
Output: 2 0
We are not working with the immediate variable here but the previous one still. Anything after this assignment, then our a == 2, b == 0.
Happy Coding 😀
+ 2
this is basically switching the first and last valise of the array. Indexing arr[0] gives you the first value which is 4, then arr[arr[0]-1] would be arr[4-1], so arr[3] is your last element in the array. When making statements in the form of a, b = b, a for example, you switch the values of the two variables. In this case its the first and last of your array.
+ 1
arr[0]=arr[arr[0]-1]=arr[4-1]=2
+ 1
Roderick Davis it must return [2,3,1,4] as you say, i think so too. But the output is [2,4,1,2]. Am i wrong?
0
Tomiwa Joseph is arr[arr[0]-1] equal to ar[3] until execution is over? Am i wrong? Then write as ar[0],ar[3]=2,4 since output must [2,3,1,4]. But its not.
0
Up
0
Михаил Иванов this question is from our challenge with you. And i got it thanks.
0
Impervious I am not sure that I am wright. But for myself I understood it this way. Sorry for my awful English.
0
Михаил Иванов Even if it's wrong, it make sense to me. I am not native and my english isnt very good either.