+ 2

# Longest Word

"Given a text as input, find and output the largest word." Please help, to write code for this problem. 😢😭

15th Apr 2021, 8:24 AM
Og'abek Rayimov
29 ответов
+ 30
txt = input() #your code goes here x = max(txt.split(), key = len) print(x) #i honestly dont blame u for looking, sololearn didnt teach us how to usebthe key function in any lesson
16th Apr 2021, 1:30 PM
Jack Jennings
+ 12
mIcHyAmRaNe , that's a very neat solution. You could make it far more pythonic like this : words = txt.split() word_lengths = [] for word in words : word_lengths.append(len(word)) longest_word = words[word_lengths.index(max(word_lengths))] print(longest_word) That is functionally exactly the same, but someone reading it will find it instantly comprehensible, even if they aren't familiar with Python. This is how you make code that is easily maintainable.
17th Jun 2021, 10:01 PM
Myk Dowling
+ 5
split(), len() and max() are probably the most useful functions for this.
15th Apr 2021, 8:29 AM
Myk Dowling
+ 5
txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua" txt= txt.split() p=[] for i in txt: p.append(len(i)) print(txt[p.index(max(p))])
17th Jun 2021, 8:10 AM
RAHMANI AMRANE
+ 5
txt = input() sp = txt.split(" ") for word in sp: if len(word) == max(len(w) for w in sp): print(word)
25th Jan 2022, 7:15 PM
or haim perets
+ 3
Og'abek Rayimov Google those functions to get examples, it is part of the learning process
15th Apr 2021, 9:47 AM
Rik Wittkopp
+ 2
Og'abek Rayimov , what is your goal here? Do you want to learn how to code in Python? If so, you need to read through the lessons and learn how to do something. We're happy to help, but it seems pointless to just give you the answers. Start with reading in the text using input(), then split it, get the length of each word from the list that split gives you, then get the max of those lengths.
15th Apr 2021, 9:17 AM
Myk Dowling
+ 2
text = input().split() new = sorted(text, key=len) print(new[-1])
24th May 2021, 5:57 AM
Xcalibur
+ 2
Frano Piskulić , You're confusing things by changing the type of your txt variable from a string to a list. You should initialise your max variable to 0, rather than the length of the first word. Your code would read cleaner if you turned around your if test. It's generally better linguistically to say if (thing I'm testing in this loop) (has relationship to) (thing I'm measuring against). Using i for your loop variable is usually only for integers, it would be much better to use a meaningful name. Get your head out of the short variable name space, and you'll find debugging much easier.
15th Jun 2021, 9:09 PM
Myk Dowling
+ 2
Hmm, I searched the internet and solved this using "key", but the version using the .index method uses only code that had previously been introduced - in lesson 25. It hasn't been a part of any of the code coaches or of any example code since then. From a didactical standpoint this is just sloppy and does not translate to a good learning experience: having to remember something anecdotal from 37 lessons ago, that has not been used since, just sucks.
28th Dec 2021, 2:21 PM
Roman-Maria Höritzsch
+ 1
Can you give me example please
15th Apr 2021, 8:37 AM
Og'abek Rayimov
+ 1
Okay thanks I will tell you if I will face any problem.
15th Apr 2021, 8:39 AM
Og'abek Rayimov
+ 1
Jack Jennings Thankyou very much 🙏🙏
19th Apr 2021, 7:40 AM
Og'abek Rayimov
+ 1
txt = input() #your code goes here list_txt = txt.split() list_1 = [] for i in range(len(list_txt)): list_1.append(len(list_txt[i])) a = max(list_1) for item in list_txt: if len(item) == a: print (item)
26th Jun 2022, 11:53 AM
HE Miaomiao
+ 1
txt = input() #your code goes here b = [len(x) for x in txt.split(" ")] # generates a list with the len of the txt inside it print(txt.split(" ")[b.index(max(b))]) # this is like printing txt in a list mode with the index of the largest value in b
30th Oct 2022, 11:48 PM
CodeEater
0
15th Apr 2021, 8:38 AM
The future is now thanks to science
0
The future is now thanks to science I cannot do anything please, just write a simple code with exactly using split(), len() and max() in which place. Please help me.
15th Apr 2021, 8:54 AM
Og'abek Rayimov
0