python - reverse words in a sentence pythonic -
i trying write pythonic code reverse words in sentence
ex:
input: "hello there friend" output: "friend there hello"
code:
class solution: def swap(self, w): return w[::-1] def reversewords(self, s): w = self.swap(s).split() return ' '.join(for x in w: swap(x))
i having bit of trouble getting work. need on return statement
you're calling swap/split in wrong order. use instead:
w = self.swap(s.split())
then return shouldn't need comprehension:
return ' '.join(w)
Comments
Post a Comment