ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
Sorry, am not conversant still with Python, and on copying the code verbatim, i.e. including header too; it works.
The code is:
Code:
f=\
lambda A:sum(2*(A[a]==a)-(A[A[a]]==a)for a in A)
from itertools import permutations
for *x, in permutations([0,1,2,3]):
print(x,f(x))
Am confused still as what does the first line of code mean even. Seems the first line of code defines a function that takes the definition of 'A',
as given on the second line. But, not know of this syntax.
Nor, am sure how the value corresponding to each permutation of the array is being computed, so unable to understand the running of the code too.
from itertools import permutations
#First
f = lambda A:sum(2*(A[a]==a)-(A[A[a]]==a)for a in A)
for *x, in permutations([0,1,2,3]):
print(x,f(x))
#Second
def f(A):
A = sum(2*(A[a]==a)-(A[A[a]]==a)for a in A)
return A
for *x, in permutations([0,1,2,3]):
print(x,f(x))
is not used here, instead the function name 'f' is followed by an '=' and a statement-continuation- character '\'.
They use whatever they want, if syntactically correct. how do you think we can tell you why they implemented a functionality this way or that way?
Otherwise if you start to learn python start it with something easier, this (what you posted) is an advanced topic.
Additionally you can [obviously] read the official documentation and the huge amount of tutorials, examples, sample programs on the net. It is extremely well documented.
For example regarding lambda: https://realpython.com/python-lambda/
You didn't get any answer on the other host, because what you posted there is incomplete.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.