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])
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)
+ 6
t=input().split()
for i in t:
b = max(t, key=len)
print(b)
+ 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?
+ 2
Wow!! How easy! :O I have just learnt 'len' as a key function in max(). Thank you so much!!!!! :)
+ 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.
+ 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
+ 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 :)
0
Muchas gracias Samuel Duque, estuve mucho tiempo intentando resolver Ă©ste desafĂo. Exelente respuesta.