0

[Solved] Why can't I get the same results with Method3 in the code? (Python)

I've been having a hard time trying not to mix up the syntax and facts of generators and functional programming. Failing big time, actually. The aim of my exercise is NOT finding the best solution. It's being able to tell my apple from my orange. So, maybe, the third one sucks, but I just cannot find a way to pinpoint my problem by printing the result. Me trying to use a gen function inside lambda with a boolean? Why not, if former's the problem, why not if latter's the problem? There's something or things fundamental about it that I do not get. I know it. Please help me. Edit: Thank you a l l. https://code.sololearn.com/crS314jT8YUK/?ref=app

6th Jun 2022, 9:01 PM
Korkunç el Gato
Korkunç el Gato - avatar
28 Réponses
+ 5
Based on your original code I think you have some misconceptions about the purpose of generators and how they work. Here is a good introduction: https://pythongeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-generators-with-examples/ Note that generators create generator objects. That's why your inequality was true for all values: you compared generator objects to a string. Here's how you can do what you're trying using a generator function. I've also added some further examples of how to use generators. https://code.sololearn.com/cVJbGa0wPlqc/?ref=app
6th Jun 2022, 10:45 PM
Simon Sauter
Simon Sauter - avatar
+ 4
Not the generator *function*, the generator *object*. When you call a normal function you get whichever object you used in the return statement. But when you call a generator function you always get a generator object. This generator object can then be used to generate objects and what kind of objects those are is determined by the yield statement in the definition of the generator function. To generate single objects using a generator object you can use the next() function or the .__next__() method, alternatively you can create a list of objects or create multiple objects by iterating over the object. You can create objects until the generator is exhausted (similar to reaching the end of a list when iterating except that you can't move back) or without limit if you use an infinite loop in the generator function definition to yield objects.
7th Jun 2022, 12:07 AM
Simon Sauter
Simon Sauter - avatar
+ 3
Simon Sauter Hell yes. So, it doesn't matter what is yielded in type, the generator func is the object compared, even if used with argument. (writing to let you see if I'm using the wrong words, if I'm inadvertently still not understanding it) Thank you. Thank you for reading my questions. Thank you for your effort to understand all these questions and misconceptions. You always read. I am grateful. I only feel gratitude when people try to help but I guess I feel extra grateful right now. Thank you.
6th Jun 2022, 11:05 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
Korkunç the Terrible Frusciante 😳 How could I forget about you? I'm sorry ☺️ More precisely: all(int(i) % 2 == 0 for i in str(num)) will always be false until num reaches 2000 print([int(i) % 2 == 0 for i in str(1000)]) #[False, True, True, True] print(all(int(i) % 2 == 0 for i in str(1000))) #False print([int(i) % 2 == 0 for i in str(2000)]) #[True, True, True, True] print(all(int(i) % 2 == 0 for i in str(2000))) #True
7th Jun 2022, 12:56 AM
Solo
Solo - avatar
+ 3
Korkunç the Terrible Frusciante I know this has been resolved, but I think I know how I gave you the wrong answer. I was rushing & did not check my work -> mortal sin. This is the code I meant to post this morning print('\n\n' + '#Method3# '*3, "\n") def fnc(num): if all(int(i) % 2 == 0 for i in str(num)): yield str(num) #lsst = list(range(1000, 3001)) m3 = list(filter(lambda x: ([i for i in fnc(x)]), range(1000, 3001))) [print(i) for i in m3]
7th Jun 2022, 8:19 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Korkunç the Terrible Frusciante I am still very much a learner, but thanks for your vote of confidence. 😁👍
7th Jun 2022, 9:48 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Korkunç the Terrible Frusciante See if this helps print('\n\n' + '#Method3# '*3, "\n") def fnc(num): if all(int(i) % 2 == 0 for i in str(num)): yield str(num) else: yield "" #lsst = list(range(1000, 3001)) m3 = list(filter(lambda x: (fnc(x) != ""), range(1000, 3001))) [print(i) for i in m3]
6th Jun 2022, 9:24 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
#Method3: print('\n\n' + '#Method3# '*3, "\n") def fnc(num): if all(int(i) % 2 == 0 for i in str(num)): pass else: return "" for i in list(filter(lambda x: (fnc(x) != ""), range(1000, 3001))): print(i) 👇 def fnc(num): if all(int(i) % 2 == 0 for i in str(num)): return 1 for i in list(filter(lambda x: fnc(x), range(1000, 3001))): print(i)
6th Jun 2022, 9:33 PM
Solo
Solo - avatar
+ 2
Korkunç the Terrible Frusciante , I do not understand English well and did not quite understand your question, if you want to receive my explanations, then subscribe to me and write to me in a personal 😎.
6th Jun 2022, 10:12 PM
Solo
Solo - avatar
+ 2
Solo You mean follow? You are among the first group of people that I have been following :D So I will then use Google translate and ask you about it with DM. I will then share your answer here, unless you tell me not to. Thank you.
6th Jun 2022, 10:16 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
From Solo: "So the line: all (int(i) % 2 == 0 for i in row(num)) will always be false, so I put pass, I thought you could guess ☺️" (Thank you Solo)
6th Jun 2022, 11:25 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
(to Solo: one of my many misconceptions was that I was getting rid of the false cases with yield "") Preconceptions that are also misconceptions. So dangerously sneaky, and needs unlearning. This question is an example. And I couldn't thank enough for all the help I got.
7th Jun 2022, 1:05 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Thanks for your kindness to explain that there was a bit of rushing. Somebody who's lev 16 knows their generators. Still, acknowledgement is good, that's what newbies cannot do when questions don't get resolved, so yeah, it's cool to let learners know :-)
7th Jun 2022, 9:37 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
To be precise, the solution should be like this 😎: values = [] for i in range(1000, 3001): s = str(i) if all(int(i)%2==0 and i!='0' for i in s): values.append(s) print(",".join(values)) 👇 print(",".join(str(i) for i in range(1000,3001) if all(int(i)%2==0 and i!='0' for i in str(i)))) 👇 print(*[i for i in range(1000,3001) if all(not int(i)%2 and i!='0' for i in str(i))], sep=',')
7th Jun 2022, 10:39 AM
Solo
Solo - avatar
+ 2
Oh here we go again. Question's back but the tick is removed. I don't understand. Why does my question stay as downvoted, and don't for a sec think I care about trolls who do that, and yet what I chose as best twice gets unticked. Yet again!!!! Driving me crazy, I swear. I must tell you this is like gaslighting to me because I have a neurological condition and it feels so bad to be constantly doubting oneself. I know no one in SL would like to have this happen, but I'm like paranoid android over here! Seriously :/ And the actual spams are still around. Yeah, SL must be going through a tough time but a notif explaining what's what would have gone a long way. Whatever the problem with the update is, it's been wreaking all kinds of havoc. edit: I sent an SS of the tickless best answer, waiting for a response.
10th Jun 2022, 5:54 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Rik Wittkopp 😓you serious? I'm about to unleash a flood of tears. I will try it now but ofc I know it will work (edit: soz, I was kinda excited after intense frustration) oh my... *facepalm* the time I spent to understand... Thaaaaaaank you in advance, million thanks!
6th Jun 2022, 9:27 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Rik Wittkopp It didn't work. Still though you had a reason to suggest doing that. May I ask why? I just want to learn more.
6th Jun 2022, 9:34 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Solo Oh, thank you too, a million times! (It did work) Do you have time for questions? If yes: why can't I yield the fnc at that number to use it below. I didn't know we could use pass like that and yes what I did like looks redundant now. But it's not just redundant, it's wrong. Why, if I may? (Sorry for addressing you with a translated version of my question a second time, I realize you don't need it at all)
6th Jun 2022, 9:40 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Don't worry. That's perfectly fine.
7th Jun 2022, 12:43 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Korkunç the Terrible Frusciante, another example using yield: def gen(): yield [int(i) % 2 == 0 for i in str(1000)] print(next(gen())) #[False, True, True, True]
7th Jun 2022, 1:36 AM
Solo
Solo - avatar