iT邦幫忙

2022 iThome 鐵人賽

DAY 15
0
Modern Web

clojure 刷刷鍋系列 第 15

Clojure 肉片 -第 15 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Write a function that converts any sentence into a V A P O R W A V E sentence. a V A P O R W A V E sentence converts all the letters into uppercase, and adds 2 spaces between each letter (or special character) to create this V A P O R W A V E effect.

Note that spaces should be ignored in this case.

(必須通過以下測試)

(ns kata.test
  (:require [clojure.test :refer :all]
            [kata         :refer :all]))
(deftest basic-tests
  (defn tester [s exp]
    (testing (str "Testing for \"" s "\"")
      (is (= (vaporcode s) exp))))
  (tester "Lets go to the movies" "L  E  T  S  G  O  T  O  T  H  E  M  O  V  I  E  S")
  (tester "Why isn't my code working?" "W  H  Y  I  S  N  '  T  M  Y  C  O  D  E  W  O  R  K  I  N  G  ?"))

【我的答案】

(ns kata)
(defn vaporcode [s]
  (clojure.string/join "  " (clojure.string/split (clojure.string/upper-case (.replaceAll s " " "")) #""))
)

思路:

  1. 拿掉空白 -> 轉大寫
  2. 想到 join 可以在合併時多插入答案需要的空白,所以先做了 split

【其他人的答案】

(ns kata
  (:require [clojure.string :as str]))

(defn vaporcode
  [s]
  (->> s
       (remove (partial = \space))
       (str/join "  ")
       (str/upper-case)))
(ns kata (:require [clojure.string :refer [join replace upper-case]]))
(defn vaporcode [s] (join "  " (vec (replace (upper-case s) " " ""))))

上一篇
Clojure 肉片 -第 14 塊
下一篇
Clojure 肉片 -第 16 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言