Longest Word
Python
Medium
2 views
Problem Description
A sentence is provided. Words are separated by spaces. Output the longest word. If tie, output the first one.
Input Format
One line sentence.
Sample Test Case
Input:
I love programming so much
Official Solution
import sys
s=sys.stdin.read()
if s is None: sys.exit(0)
if s.endswith('\
'):
s=s[:-1]
words=[w for w in s.split(' ') if w!='']
best=''
for w in words:
if len(w)>len(best):
best=w
sys.stdout.write(best)
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!