Created
December 9, 2014 04:12
-
-
Save leifp/ae37c3b6f1b497f13f1e to your computer and use it in GitHub Desktop.
Solution to diamond kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns diamond | |
(:require [clojure.string :as s] | |
[clojure.test :refer [deftest is]])) | |
(defn palindrome | |
[coll] | |
(concat coll (reverse (butlast coll)))) | |
(defn char-range | |
[start-c end-c] | |
(let [start (int start-c) | |
end (int end-c)] | |
(map char (range start (inc end))))) | |
(defn initial-coll | |
[c] | |
(map #(hash-map :chr %) (char-range \A c))) | |
(defn add-spacing | |
[coll] | |
(let [r (range (count coll))] | |
(map #(assoc %1 :prefix %2 :suffix %3) coll (reverse r) r))) | |
(defn make-row | |
[prefix-char suffix-char row-data] | |
(let [{:keys [chr prefix suffix]} row-data] | |
(->> (concat (repeat prefix prefix-char) | |
[chr] | |
(repeat suffix suffix-char)) | |
palindrome | |
(apply str)))) | |
(defn diamond | |
[c] | |
{:pre [(<= (int \A) (int c) (int \Z))]} | |
(->> (initial-coll c) | |
add-spacing | |
(map (partial make-row " " " ")) | |
palindrome | |
(s/join \newline))) | |
(deftest A | |
(is (= (diamond \A) "A"))) | |
(deftest B | |
(is (= (diamond \B) | |
" A | |
B B | |
A"))) | |
(deftest C | |
(is (= (diamond \C) | |
" A | |
B B | |
C C | |
B B | |
A"))) | |
(deftest E | |
(is (= (diamond \E) | |
" A | |
B B | |
C C | |
D D | |
E E | |
D D | |
C C | |
B B | |
A"))) | |
(deftest input-char-not-allowed | |
(is (thrown? AssertionError (diamond \a)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment