Complete the solution so that it splits the string into pairs of two characters.
If the string contains an odd number of characters then it should replace the missing
second character of the final pair with an underscore ('_').Examples:
'abc' => ['ab', 'c_']
'abcdef' => ['ab', 'cd', 'ef']
Keywords:
count
: gets length of stringodd?
: checks if number is oddstr
: concatenates strings (adds underscore if needed)partition
: splits collection into groups of specified sizemap
: applies function to each elementapply
str: joins characters in each pair back into strings->>
: threading macro for cleaner data transformation; implement
(defn solution [s]
(let [padded-string (if (odd? (count s))
(str s "_")
s)]
(->> padded-string
(partition 2)
(map #(apply str %)))))
; test
; execute implement function
(defn tester [arg exp]
(= (solution arg) exp))
; args & exception
(comment
(tester "abc" ["ab" "c_"])
(tester "abcdef" ["ab" "cd" "ef"])
(tester "" [])
(tester "x" ["x_"])
(tester "Banana" ["Ba" "na" "na"])
(tester "CodeWars" ["Co" "de" "Wa" "rs"]))