- 1
Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome
Please help me What is wrong? txt = input() #your code goes here list=f"{txt}".split(" ") print (max(list))
8 Respostas
+ 17
max() can find the longest word and is working properly with strings <<< (Slick)
max can take a key argument, that has to be a function. iterable (list of strings) are parsed and comparison is based on its return value from the function that is used. in our case it has to be 'len':
a = input().split()
print(max(a, key = len))
+ 3
max() doesn't work like that for characters. You'd need to get the length of each word and compare them. Then print the word with the largest length
Lothar
https://code.sololearn.com/c1jh6DDa5Ug6/?ref=app
+ 3
dic={len(w):w for w in txt.split()}
#find Max key and get its item
# problems: u only last longest word.
+ 3
txt = input();
#tu código va aquí
print(max(txt.split(), key = len));
+ 3
txt = input()
#your code goes here
list= txt.split(" ")
longest= max(list,key=len)
print (longest)
+ 2
Undertandable thanks
+ 1
My code is:
txt = input()
#List of words
string = txt.split(' ')
# print(string)
#Qty of strings
# print(range(len(string)))
#List of each word length
q = [len(string[i]) for i in range(len(string))]
# print(q)
#Index of the longest word
# print(q.index(max(q)))
ilw = q.index(max(q))
#The longest word
print(string[ilw])
+ 1
My code is:
txt=input()
i=" "
for w in txt.split(): #Here it is checked each word in the splitted word from txt-(input)
if (len(w)>len(i)): #It's checked if the length of the word is bigger than the previous one
i=w #If the value is bigger in letters, the value is replaced by the highest one in the variable i
print (i) #The highest value word is returned
Hope it helps up!