LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2024, 10:12 PM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
Unable to run given code in Python.


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
Click image for larger version

Name:	Screenshot (105).png
Views:	8
Size:	144.4 KB
ID:	42472  

Last edited by ajiten; 01-25-2024 at 10:14 PM.
 
Old 01-25-2024, 10:41 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,311
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Please post the code in question. Be sure to use [code] [/code] tags to preserve indentation.
 
Old 01-26-2024, 12:25 AM   #3
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by Turbocapitalist View Post
Please post the code in question. Be sure to use [code] [/code] tags to preserve indentation.
Am following the code as given in my first message, i.e. here.

That states:
Code:
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))
which am confused as the array A is not specified.

Also, the above code doesn't run.

=================================================
P.S. : modification

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.

The output is:
Code:
[0, 1, 2, 3] 4
[0, 1, 3, 2] 0
[0, 2, 1, 3] 0
[0, 2, 3, 1] 1
[0, 3, 1, 2] 1
[0, 3, 2, 1] 0
[1, 0, 2, 3] 0
[1, 0, 3, 2] -4
[1, 2, 0, 3] 1
[1, 2, 3, 0] 0
[1, 3, 0, 2] 0
[1, 3, 2, 0] 1
[2, 0, 1, 3] 1
[2, 0, 3, 1] 0
[2, 1, 0, 3] 0
[2, 1, 3, 0] 1
[2, 3, 0, 1] -4
[2, 3, 1, 0] 0
[3, 0, 1, 2] 0
[3, 0, 2, 1] 1
[3, 1, 0, 2] 1
[3, 1, 2, 0] 0
[3, 2, 0, 1] 0
[3, 2, 1, 0] -4
Attached Thumbnails
Click image for larger version

Name:	Screenshot (107).png
Views:	4
Size:	143.2 KB
ID:	42473  

Last edited by ajiten; 01-26-2024 at 12:36 AM.
 
Old 01-26-2024, 01:10 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start, forget lambdas, and do some basic practices, e.g.
Code:
list=[1,2,4,8]
for a in list:
    print(a)

list=[1,2,4,8]
for i in range(len(list)):
    print(i,list[i])
 
Old 01-26-2024, 01:34 AM   #5
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
As a start, forget lambdas, and do some basic practices, e.g.
Code:
list=[1,2,4,8]
for a in list:
    print(a)

list=[1,2,4,8]
for i in range(len(list)):
    print(i,list[i])
Request why the earlier syntax of
Code:
def function_name(parameters):
is not used here, instead the function name 'f' is followed by an '=' and a statement-continuation- character '\'.

-----

How that links up with the lambda function on the next line, is not an issue; at least for now.

Last edited by ajiten; 01-26-2024 at 01:35 AM.
 
Old 01-26-2024, 02:40 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Look carefully: it is not a working example, it is a counterexample: a piece of code, that doesn't work. The error message is below:
Code:
NameError: name 'f' is not defined
 
Old 01-26-2024, 03:37 PM   #7
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Look carefully: it is not a working example, it is a counterexample: a piece of code, that doesn't work. The error message is below:
Code:
NameError: name 'f' is not defined
Please see my second message's modification at the end, as it works, on adding the header in the stated code.

Last edited by ajiten; 01-26-2024 at 03:39 PM.
 
Old 01-26-2024, 04:14 PM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Code:
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))
Look at:
Code:
>>> help(sum)
>>> help(permutations)
>>> help(print)

Last edited by teckk; 01-26-2024 at 04:20 PM.
 
1 members found this post helpful.
Old 01-27-2024, 02:35 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by ajiten View Post
Request why the earlier syntax of
Code:
def function_name(parameters):
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to understand why the given Lex program fails to recognize the given input string. ajiten Programming 11 07-11-2023 12:28 AM
i tried using this code for deleting a user given character from a user given string mecrazyme1234 Linux - Newbie 2 06-04-2011 04:59 PM
i tried using this code for deleting a user given character from a user given string mecrazyme1234 Programming 7 06-04-2011 11:47 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration