【今日湯底】
Complete the solution so that it reverses all of the words within the string passed in.
Example(Input --> Output):
"The greatest victory is that which requires no battle" --> "battle no requires which that is victory greatest The"
(必須通過以下測試)
(ns kata.test
(:require [clojure.test :refer :all]
[kata :refer [reverse-words]]))
(deftest basic-tests
(is (= (reverse-words "hello world!") , "world! hello"))
(is (= (reverse-words "yoda doesn't speak like this"), "this like speak doesn't yoda"))
(is (= (reverse-words "foobar") , "foobar"))
(is (= (reverse-words "kata editor") , "editor kata"))
(is (= (reverse-words "row row row your boat") , "boat your row row row")))
【我的答案】
(ns kata)
(defn reverse-words [words]
(->> (clojure.string/split words #" ")
reverse
(clojure.string/join #" "))
)
(ns kata)
(defn reverse-words [words]
(->> (clojure.string/split words #" ")
reverse
(reduce #(str %1 " " %2) " ")
(clojure.string/trim))
)
【其他人的答案】
(ns kata
(require [clojure.string :as str]))
(defn reverse-words [words]
(->> (str/split words #" ")
(reverse)
(str/join " ")))