Python: La palabra más larga / Longest Word | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python: La palabra más larga / Longest Word

Hola!! Soy muy nuevo en esto de la programación y necesito ayuda con el desafío "La palabra más larga" por favor. Mi código es el siguiente pero solo cumple en el caso 4. ¿Qué me faltaría? Gracias de antemano!! :) txt = input().split() for x in txt: longitud = [len(x)] mayor = max(longitud) buscamayor = longitud.index(mayor) print (txt[buscamayor])

22nd Feb 2021, 10:35 PM
Rafa RafaR
Rafa RafaR - avatar
8 Answers
+ 3
okay, well, since you passed, i will tell you that you do not need to convert the list of words to a list of lengths to use the max function. although using the max function directly on the word waste by default will return the first word in alphabetical order, you can change the key to the len function, like this: buscamayor=max(txt, key=len)
23rd Feb 2021, 5:18 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 6
t=input().split() for i in t: b = max(t, key=len) print(b)
10th Oct 2021, 1:50 PM
Samuel Duque
Samuel Duque - avatar
+ 2
you can do code analysis yourself, working line by line, and pretending that you are the python interpreter. what answer would you come up with? but for now, I’ll do it one more time, if thats what you need?
23rd Feb 2021, 5:21 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 2
Wow!! How easy! :O I have just learnt 'len' as a key function in max(). Thank you so much!!!!! :)
23rd Feb 2021, 9:11 PM
Rafa RafaR
Rafa RafaR - avatar
+ 1
my translator is failing, but i think i understand. you seem to have mixed up programming language concepts. longitud = [len(x)] will detect the length of whatever word the For loop is on, place it inside a List, and then assign that to longitud. the List will only have one element. then on the next line Max finds the largest element in Longitud (there is only one element, the length of X, so it will return that), only to assign it back to Mayor. the next line then finds where Mayor is in Longitud (it will be the first element since it is the only one, so that will return 0), and assigns it to Buscamayor. when the for loop finally does this to the last word in the List Txt, the final line prints whatever word in Txt is at index buscamayor, and since buscamayor will always turn out to be 0, this code will always return the first word entered. I am really tempted to give you the answer to this, but I’ll just say that you are thinking of it as much trickier than it actually is.
22nd Feb 2021, 11:38 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Thank you so much for ur help Wilbur!! I have finally passed the exercise this way: txt = input().split() longitud = [len(x) for x in txt] mayor = max(longitud) buscamayor = longitud.index(mayor) print (txt[buscamayor]) Although, I cannot get the reason why it does work that way :S
23rd Feb 2021, 5:08 PM
Rafa RafaR
Rafa RafaR - avatar
+ 1
txt = input().split(" ") #hacemos el texto un arreglo longitud = [] #ago un arreglo bacio para poner las len() de las palabras for x in txt: numero = len(x) longitud.append(numero) # obtenemos el numero de cada palabra y lo agregamos al arreglo bacio maximo = max(longitud) #buscamos el numero mas alto de las palabras indexacion = longitud.index(maximo) #buscamos la indexacion de esa palabra alta print (txt[indexacion]) #hacemos aparecer en pantalla el arreglo de palabra con su indexacion #espero aver ayudado es lo mejor explicado que pude saludos :)
14th Dec 2021, 10:08 PM
Danny Navarrete
Danny Navarrete - avatar
0
Muchas gracias Samuel Duque, estuve mucho tiempo intentando resolver éste desafío. Exelente respuesta.
16th Aug 2022, 7:09 PM
Jhozmer A. Ramírez P.
Jhozmer A. Ramírez P. - avatar