Interviews: Reverse Word Order
Question
Rather than reverse the string, reverse the word order
Solution
For this question, let’s make it simple and steal one of the solutions to the question on reversing a string. We’ll need to make an alteration to allow for list explosion based on whitespace rather than individual characters:
def revWordList(input):
return ' '.join(reversed(input.split(' ')))
This function explodes (or “splits”) the string based on a space, calls the list reverse, then implodes (or “joins”) the list into a string by reintroducing the space.