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

20th Nov 2020, 1:40 PM
Nadirhan Şahin
Nadirhan Şahin - avatar
10 Respostas
+ 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
22nd Nov 2020, 2:42 PM
Михаил Иванов
Михаил Иванов - avatar
+ 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 😀
20th Nov 2020, 2:49 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 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.
20th Nov 2020, 1:45 PM
Roderick Davis
Roderick Davis - avatar
+ 1
arr[0]=arr[arr[0]-1]=arr[4-1]=2
20th Nov 2020, 1:43 PM
Hima
Hima - avatar
+ 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?
20th Nov 2020, 2:24 PM
Nadirhan Şahin
Nadirhan Şahin - avatar
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.
20th Nov 2020, 7:40 PM
Nadirhan Şahin
Nadirhan Şahin - avatar
0
Up
21st Nov 2020, 11:59 AM
Nadirhan Şahin
Nadirhan Şahin - avatar
0
Михаил Иванов this question is from our challenge with you. And i got it thanks.
22nd Nov 2020, 5:16 PM
Nadirhan Şahin
Nadirhan Şahin - avatar
0
Impervious I am not sure that I am wright. But for myself I understood it this way. Sorry for my awful English.
23rd Nov 2020, 10:02 AM
Михаил Иванов
Михаил Иванов - avatar
0
Михаил Иванов Even if it's wrong, it make sense to me. I am not native and my english isnt very good either.
23rd Nov 2020, 1:35 PM
Nadirhan Şahin
Nadirhan Şahin - avatar