Skip to content

Instantly share code, notes, and snippets.

@werand
Created February 2, 2016 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save werand/1447e2de5c2dbe606bb4 to your computer and use it in GitHub Desktop.
Save werand/1447e2de5c2dbe606bb4 to your computer and use it in GitHub Desktop.
(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