Unable to run given code in Python.
by ajiten from LinuxQuestions.org on (#6J51Z)
Have code here (with its code here) that have difficulty in not only understanding, but if try to put at pythontutor.com, then cannot run too, as shown in the attachment.
So, obviously cannot run it too.
Am also going through the 3rd edition of the book titled: Learn_Python_Programming_An_in_depth_introduction_to_the_fundamentals, by: Fabrizio Romano, Heinrich Kruger; in order to understand where am making mistake in putting up the iterator.
But, seems have stuck in starting from the start, as till now have reached page #65, with silly doubts there too.
Didn't attract any response here.
So, came here, though have problem both in code running and understanding.
My understanding of the code is that the
Code:lambda A: sum(2*(A[a]==a)-(A[A[a]]==a) for a in A)For the array 'A', the index variable 'a' takes sequentially all values in it.
The condition 'A[a]==a' is met when the element at i-th position is the same as the i-th element.
Say, for the below cases:
Code: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,Though am confused on the interpretation, as well as the need for the condition:
Code:A[A[a]]==a
Attached Thumbnails
So, obviously cannot run it too.
Am also going through the 3rd edition of the book titled: Learn_Python_Programming_An_in_depth_introduction_to_the_fundamentals, by: Fabrizio Romano, Heinrich Kruger; in order to understand where am making mistake in putting up the iterator.
But, seems have stuck in starting from the start, as till now have reached page #65, with silly doubts there too.
Didn't attract any response here.
So, came here, though have problem both in code running and understanding.
My understanding of the code is that the
Code:lambda A: sum(2*(A[a]==a)-(A[A[a]]==a) for a in A)For the array 'A', the index variable 'a' takes sequentially all values in it.
The condition 'A[a]==a' is met when the element at i-th position is the same as the i-th element.
Say, for the below cases:
Code: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,Though am confused on the interpretation, as well as the need for the condition:
Code:A[A[a]]==a
Attached Thumbnails