(SOLVED) Python Coding Project: Longest word | Sololearn: Learn to code for FREE!
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

7th Jul 2021, 9:56 AM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
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.
7th Jul 2021, 10:48 AM
Lothar
Lothar - avatar
+ 12
Calvin Thomas Thank you, didn't knew about max() key parameter.
8th Jul 2021, 11:42 AM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
+ 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.
9th Jul 2021, 12:45 AM
Chuks AJ
Chuks AJ - avatar
+ 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 šŸ˜ƒ
7th Jul 2021, 12:53 PM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
+ 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.
8th Jan 2022, 11:03 AM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
+ 9
Abhay Almost nothing
7th Jul 2021, 12:54 PM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
+ 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)
17th Jul 2022, 3:48 PM
Nicolas Leonetti
Nicolas Leonetti - avatar
+ 5
What code you have written so far ?
7th Jul 2021, 10:06 AM
Abhay
Abhay - avatar
+ 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
17th Jul 2022, 4:13 PM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
+ 4
Nikolaas Brems actually pretty interesting šŸ¤”
13th Oct 2022, 7:41 PM
šŸ‡ŗšŸ‡¦ GUMBALL šŸ‡ŗšŸ‡¦ [Dead account]
+ 3
txt = input('Yor text: ') #your code goes here w = txt.split(' ') w.sort(key=len) print(w[-1])
19th Sep 2022, 2:08 AM
Abdulfattah Alhazmi
Abdulfattah Alhazmi - avatar
+ 2
Indeed ! Thanks for the advice !
17th Jul 2022, 4:32 PM
Nicolas Leonetti
Nicolas Leonetti - avatar
+ 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
13th Oct 2022, 7:40 PM
Nikolaas Brems
Nikolaas Brems - avatar
+ 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))
5th Sep 2022, 8:55 AM
Dukeson Ehigboria O
Dukeson Ehigboria O - avatar
+ 1
I solved this problem in this way: txt = input() result = sorted(txt.split(' '), key=len) print(result[-1])
23rd Jan 2023, 1:15 PM
ŠžŠ»ŠµŠ³ Š¤ŠøŠ½ŃŽŃ‚ŠøŠ½
ŠžŠ»ŠµŠ³ Š¤ŠøŠ½ŃŽŃ‚ŠøŠ½ - avatar
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))])
2nd Apr 2024, 9:13 AM
Mohammed Mahdy