DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
johncip profile image
jmc • Edited

Clojure:

(defn row [max i]
  (apply str (concat (repeat (- max i) \space)
                     (repeat (inc (* 2 i)) \*))))

(defn diamond [max]
  (let [is (range 0 max)]
    (->> (concat is (rest (reverse is)))
         (map (partial row max))
         (clojure.string/join \newline))))

as pseudocode:

row = fn(max, i) =>
    pad = " " * (max - i)
    stars = "*" * (2 * i + 1)
    stars + pad

diamond = fn(max) =>
    top_nums = up_to(0, max)
    btm_nums = drop_first(reverse(top_nums))
    rows = row(max, i) for each i in (top_nums + btm_nums) 
    join(rows, "\n")