Can anybody explain why i can use in expression and not jn normal way | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anybody explain why i can use in expression and not jn normal way

I am super confuse https://code.sololearn.com/ccl4ctqPmM60/?ref=app

15th Oct 2022, 5:36 AM
Aisy Danish Mohd Nazry
Aisy Danish Mohd Nazry - avatar
2 Answers
+ 2
# Hi, Aisy Danish Mohd Nazry ! # In your first code example you created a generator expression inside min(), that the min-function works over. # So in your second example, you have to do this work by your self. # To to illustrate this, I put all word lengths in a list: def find_short(s): res = [] for x in s.split(): res.append(len(x)) return min(res) print(find_short("hshshsnsjsjsj js jsjs")) # Your first code example is acctually a short way to doing this (but # this of course nobody does, because the min-function handle it for you): def find_short(s): # Create a generator. def wd_lengths(s): for x in s.split(): yield len(x) gen = wd_lengths(s) return min(gen) txt = "hshshsnsjsjsj js jsjs" print(find_short(txt))
15th Oct 2022, 5:57 AM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Thank you bro 💪💪🫶🏻
15th Oct 2022, 10:14 AM
Aisy Danish Mohd Nazry
Aisy Danish Mohd Nazry - avatar