Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 6
(SOLVED) Python Coding Project: Longest word
Hello! Can somebody help me with this task? I cannot get the solution ā¹ļø Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome
16 Respostas
+ 21
Gįį°į·į©įŖįŖ ,
you have already completed nearly 60% of the python core tutorial. so you have learned everything you need to solve this task by yourself.
here some hints:
āŖļø take an input string and store it in a variable
āŖļøsplit the string to individual words, this will result in a list
āŖļøcreate variables that can hold a string and the corresponding length of the string and initialize them (1)
āŖļøuse a for loop to iterate through the list of words
āŖļøget length of each word that is given in the loop variable
āŖļøif length of current word is longer than the ones stored in the variable put new longest word and length in variables
āŖļøafter loop is finished you can output the variable that contains the longest word
(1) instead of using 2 variables you can use a list to store the word and the corresponding length.
+ 12
Calvin Thomas Thank you, didn't knew about max() key parameter.
+ 12
I'm not sure why people enjoy using complex codes to explain concepts to beginners. No disrespect to you, Calvin, but #2 up there only complicates issues for a newbie. Pythonic principles state that SIMPLE IS ALWAYS BETTER THAN COMPLEX!! š
word = "This is an awesome text"
longest_word = ""
splitted = word.split()
for word in splitted:
if (len(word)) > len(longest_word):
longest_word = word
print(longest_word)
If you're getting input from a user, then simply save it into a variable like 'word' above and use it.
+ 11
Lothar Thank you so much, I'm so glad you helped me š
Yes, I know I've completed 60% of course and know everything I need, I just tried to write normal code for, like, 20 minutes, I tried to remember all kind of things I know, but no result. Maybe, I was just bored, tired or lazy. Anyways, thanks for you feedback š
+ 10
Chuks AJ I don't know why I'm answering after 6 months, but late is better than never.
I'm not beginner š
And I was not beginner back then too š
š
I just didn't practice for a VERY long time.
+ 9
Abhay Almost nothing
+ 6
Here is the code that I came up with intuitively.
But, is putting a for loop inside an if statement "pythonic", or does it have problems ?
txt = input()
text = txt.split(" ")
for w in text :
if len(w) == max(len(w) for w in text) :
print (w)
+ 5
What code you have written so far ?
+ 4
Nicolas Leonetti I think everything is ok with your code, just put a "break" after "print(w)", so it doesn't do necessary comparations
+ 4
Nikolaas Brems actually pretty interesting š¤
+ 3
txt = input('Yor text: ')
#your code goes here
w = txt.split(' ')
w.sort(key=len)
print(w[-1])
+ 2
Indeed ! Thanks for the advice !
+ 2
I'm very proud i solved this one on my own, but after reading this page, I felt stupid. Seems I have a tendency to make stuff needlessly complicated...
Here's what I came up with:
txt = input()
WordLengthDict = {}
WordLength = []
words = txt.split()
for word in words:
WordLengthDict [len(word)] = word
WordLength.append(len(word))
Longest = max(WordLength)
LongestWord = WordLengthDict.get(int(Longest))
print(LongestWord)
It works, but it's far from elegant xD
+ 1
This is what I did
txt = input()
splitted = txt.split()
longest_word = [word for word in splitted if (len(word) == max(len(word) for word in splitted)) ]
print("".join(longest_word))
+ 1
I solved this problem in this way:
txt = input()
result = sorted(txt.split(' '), key=len)
print(result[-1])
0
My way: for whom has basic knowledge
txt = input()
splitted = txt.split(' ')
txt_len=[]
for word in splitted :
txt_len.append(len(word))
print(splitted[txt_len.index(max(txt_len))])