forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_04.clj
29 lines (25 loc) · 957 Bytes
/
01_04.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
; Clojure solution to question 1.4 from Cracking the Coding Interview, 5th Edition.
; Nitin Punjabi
; github.com/nitinpunjabi
(ns chapter01.01-04
(:require [clojure.test :as t]))
; method using line strip and reduce
(defn replace-with-char
"Solution using trim and reduce. Assumes heading and trailing whitespaces should be removed."
[s target replacement]
(->> (clojure.string/trim s)
(reduce #(if (= target %2) (conj % replacement) (conj % %2)) [])
(clojure.string/join)))
(defn test-helper
[s]
(replace-with-char s \space "%20"))
(t/deftest replace-with-char-test
(t/is (= (test-helper "") ""))
(t/is (= (test-helper "a") "a"))
(t/is (= (test-helper " a") "a"))
(t/is (= (test-helper "a ") "a"))
(t/is (= (test-helper " a ") "a"))
(t/is (= (test-helper " a b ") "a%20b"))
(t/is (= (test-helper " a b ") "a%20%20b"))
(t/is (= (test-helper "Mr John Smith ") "Mr%20John%20Smith")))