Reverse Words Function
Python
Medium
3 views
Problem Description
One line sentence is provided. Write function rev_words(s) that reverses word order. Output output.
Input Format
One line sentence.
Output Format
One line reversed words.
Official Solution
import sys
s=sys.stdin.read()
if s is None: sys.exit(0)
if s.endswith('\
'):
s=s[:-1]
def rev_words(t):
words=[w for w in t.split(' ') if w!='']
words.reverse()
return ' '.join(words)
sys.stdout.write(rev_words(s))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!