Reverse words
Write a function that takes a string containing words (one or more sentences) and returns a string containing the words in reverse order.
Examples
(reverse-words "my name is Eric.") ;;=> "Eric. is my name"
(reverse-words "hello") ;;=> "hello"
(reverse-words "I love you") ;;=> "you love I"
Note: Words are characters separated by whitespace.
Thanks to this site for the problem idea, where it is rated Very Hard in Java. The problem has been modified.
Please submit your solutions as comments on this gist.
To subscribe: https://ericnormand.me/newsletter
Minor stylistic variant of @miner's answer just for fun, as my original answer was just the same (only difference being the usage of
reverse
instead ofrseq
).str/join
I like that the core of the function reads just the same as the function name
(reverse words)
. I find it also interesting to consider whether long-form representations like\space
or#"\p{Space}+"
(that aren't idiomatic in Clojure) instead of" "
or#"\s+"
can aid in legibility or not. Of course here it is but a tiny little function where it doesn't really matter much.