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
  Csv writer meaning of quoting mg24 2 1,233 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 2,103 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 7,194 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Operator meaning explanation Sherine 3 2,129 Jul-31-2021, 11:05 AM
Last Post: Sherine
  how long can a line of code be? Skaperen 2 2,287 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,820 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Don't understand example code from a textbook lil_fentanyl 1 1,899 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,214 Dec-06-2020, 10:10 PM
Last Post: Aizou
  parser.parse_args() meaning vinci 2 2,702 Oct-26-2020, 04:13 PM
Last Post: vinci
  What is the meaning of k in this function? giladal 3 2,837 Aug-15-2020, 12:32 PM
Last Post: buran

Forum Jump:

User Panel Messages

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