Python Forum
Unable to understand the meaning of the line of code. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unable to understand the meaning of the line of code. (/thread-41482.html)



Unable to understand the meaning of the line of code. - jahuja73 - Jan-23-2024

Am unable to understand the meaning of the code on the r.h.s. of the below line of code, as given here.

lambda A: sum(2*(A[a]==a)-(A[A[a]]==a) for a in A)
The array 'A' here is: [0, 1, 2, 3], with 'a' taking any of the four values in 'A'.
So,
A[a]==a 
implies any of the four below situations, all of which are satisfied for the identity permutation.
1. 0123 : identity permutation,
2. 0213 : here A[0]=0, A[3]=3,
3. 0132 : here A[0]=0, A[1]=1,
4. 1023 : here A[2]=2, A[3]=3,
5. 1203 : here A[3]=3,
6. 1320 : here A[2]=2,
7. 2103 : here A[1]=1, A[3]=3,
8. 2013 : here A[3]=3,
9. 3120 : here A[1]=1, A[2]=2,
10. 3102 : here A[1]=1,

In fact, don't know how to calculate using combinatorics, how many such combinations are possible where at least for one value of 'a', have: A[a]==a.

But, that is not the subject here.

The main concern is finding out the meaning of the given code line.