+ 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
Og'abek Rayimov - avatar
29 Answers
+ 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
Jack Jennings - avatar
+ 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
Myk Dowling - avatar
+ 5
split(), len() and max() are probably the most useful functions for this.
15th Apr 2021, 8:29 AM
Myk Dowling
Myk Dowling - avatar
+ 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
or haim perets - avatar
+ 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
Rik Wittkopp - avatar
+ 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
Myk Dowling - avatar
+ 2
text = input().split() new = sorted(text, key=len) print(new[-1])
24th May 2021, 5:57 AM
Albert
Albert - avatar
+ 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
Myk Dowling - avatar
+ 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
Og'abek Rayimov - avatar
+ 1
Okay thanks I will tell you if I will face any problem.
15th Apr 2021, 8:39 AM
Og'abek Rayimov
Og'abek Rayimov - avatar
+ 1
Jack Jennings Thankyou very much 🙏🙏
19th Apr 2021, 7:40 AM
Og'abek Rayimov
Og'abek Rayimov - avatar
+ 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
CodeEater - avatar
0
Myk Dowling already gave you hint .Now try to make your code.Then if you face any problem ,show your attempt and we will help you.
15th Apr 2021, 8:38 AM
The future is now thanks to science
The future is now thanks to science - avatar
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
Og'abek Rayimov - avatar
0
Your welcome
19th Apr 2021, 2:44 PM
Jack Jennings
Jack Jennings - avatar
0
txt = input() split_of_text = txt.split(" ") num_of_word = [] for i in range(len(split_of_text)): num_of_word.append(len(split_of_text[i])) #print(num_of_text) #print(num_of_word) longest_word_index = num_of_word.index(max(num_of_word)) print (split_of_text[longest_word_index])
15th Jun 2021, 6:07 AM
Avid_Coder
Avid_Coder - avatar
0
I used this code: --- txt = input() #your code goes here txt=txt.split() #print(txt) max=len(txt[0]) word='' for i in txt: if max<len(i): max=len(i) word=i print(word) ---- But I pass only 3 out 4 case tests, any suggestions?
15th Jun 2021, 11:28 AM
Frano Piskulić
Frano Piskulić - avatar