Created
February 2, 2016 20:27
-
-
Save werand/1447e2de5c2dbe606bb4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.core) | |
;; | |
;; Diamond kata | |
;; | |
(def alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
(def blanks-d (iterate #(str %1 "-") "")) | |
(defn blanks [n] (nth blanks-d n)) | |
(defn base-line [width n letter] | |
(let [outer-blanks (blanks (- width n))] | |
(str outer-blanks letter outer-blanks))) | |
(defn diamond-line [width n letter] | |
(let [inner-blanks (blanks (dec (* 2 n))) | |
outer-blanks (blanks (- width n))] | |
(str outer-blanks letter inner-blanks letter outer-blanks))) | |
(defn diamond [c] | |
(when-not (or (nil? c) (.equals c "")) | |
(let [width (.indexOf alphabet c) | |
half-diamond (for [n (range 0 (inc width) 1)] | |
((if (= 0 n) base-line diamond-line) width n (nth alphabet n))) | |
diamond (concat half-diamond (rest (reverse half-diamond)))] | |
(clojure.string/join "\n" diamond)))) | |
(println (diamond "Z")) | |
(diamond "") | |
(diamond "A") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment