0

"Longest Word" - Problem

***PROBLEM TEXT*** Longest Word Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome ------------------------------------------------------------- ***MY SOLUTION*** txt = input() words = txt.split() char = [] for i in words: y=len(i) char.append(y) highervalue = max(char) position = char.index(highervalue) print(words[position]) ----------------------------------------------------------------- Any better/faster code advice? I'm looking for comparison because I want to improve my skills with people with better coding ability, or just different solutions :) ty u all!

9th Sep 2021, 11:48 PM
Alvise Fortin
Alvise Fortin - avatar
3 odpowiedzi
+ 2
uh, sorry ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ , I'm just new in this place so I didn't know it was wrong to post something like this. Thank you for advices, both on the main topic and the post section :)
10th Sep 2021, 8:57 AM
Alvise Fortin
Alvise Fortin - avatar
+ 1
Here's a straightforward example: txt = input() words = txt.split() longlen, longest = 0, None for i in words: if longlen < len(i): longlen = len(i) longest = i print(longest) And here's a one-liner: print(sorted((input() or 'None').split(), key = len, reverse = True)[0])
10th Sep 2021, 1:56 AM
Steve
Steve - avatar