Reverse string by word is a very popular interview question. In python you can solve it easily with code like below.
|
|
We can see above implementation is good but not enough, in 2nd string we are expecting the !
symbol should be reversed as well, and keep original blank spaces between words. (multiple spaces between Hello
and World
in the example)
|
|
To improve the solution, a better choice should be re
module. You might want to take a look at re.split()
method.
|
|
If you would like to increase the readability a little bit, replacing list slicing to reversed()
is a choice.
|
|
Bingo, so easy!
近期评论