Python Forum
Unable to understand the meaning of the line of code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to understand the meaning of the line of code.
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 6 465 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Csv writer meaning of quoting mg24 2 1,307 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 2,234 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 7,492 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Operator meaning explanation Sherine 3 2,200 Jul-31-2021, 11:05 AM
Last Post: Sherine
  how long can a line of code be? Skaperen 2 2,340 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,894 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Don't understand example code from a textbook lil_fentanyl 1 1,933 Jan-25-2021, 07:02 PM
Last Post: nilamo
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,318 Dec-06-2020, 10:10 PM
Last Post: Aizou
  parser.parse_args() meaning vinci 2 2,781 Oct-26-2020, 04:13 PM
Last Post: vinci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020