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

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -