+ 2
Longest Word
"Given a text as input, find and output the largest word." Please help, to write code for this problem. 😢😭
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
+ 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.
+ 5
split(), len() and max() are probably the most useful functions for this.
+ 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))])
+ 5
txt = input()
sp = txt.split(" ")
for word in sp:
if len(word) == max(len(w) for w in sp):
print(word)
+ 3
Og'abek Rayimov
Google those functions to get examples, it is part of the learning process
+ 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.
+ 2
text = input().split()
new = sorted(text, key=len)
print(new[-1])
+ 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.
+ 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.
+ 1
Can you give me example please
+ 1
Okay thanks I will tell you if I will face any problem.
+ 1
Jack Jennings Thankyou very much 🙏🙏
+ 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)
+ 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
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.
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.
0
Your welcome
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])
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?