Repository: exercism/clojure
Pull request #190 (2017-10-21 10:12:52)

add diamond exercise


Patch:
diff --git a/config.json b/config.json
index 7499938..a69a870 100644
--- a/config.json
+++ b/config.json
@@ -506,6 +506,14 @@
       "topics": null,
       "unlocked_by": null,
       "uuid": "622823dc-ee47-42a0-acc6-190de4541625"
+    },
+    {
+      "core": false,
+      "difficulty": 1,
+      "slug": "diamond",
+      "topics": null, 
+      "unlocked_by": null,
+      "uuid": "f3971f71-08c9-4e36-a69e-5bd7fe070b07"
     }
   ],
   "foregone": [],
diff --git a/exercises/diamond/README.md b/exercises/diamond/README.md
new file mode 100644
index 0000000..13b1446
--- /dev/null
+++ b/exercises/diamond/README.md
@@ -0,0 +1,51 @@
+The diamond kata takes as its input a letter, and outputs it in a diamond
+shape. Given a letter, it prints a diamond starting with 'A', with the
+supplied letter at the widest point.
+
+## Requirements
+
+* The first row contains one 'A'.
+* The last row contains one 'A'.
+* All rows, except the first and last, have exactly two identical letters.
+* All rows have as many trailing spaces as leading spaces. (This might be 0).
+* The diamond is horizontally symmetric.
+* The diamond is vertically symmetric.
+* The diamond has a square shape (width equals height).
+* The letters form a diamond shape.
+* The top half has the letters in ascending order.
+* The bottom half has the letters in descending order.
+* The four corners (containing the spaces) are triangles.
+
+## Examples
+
+In the following examples, spaces are indicated by `·` characters.
+
+Diamond for letter 'A':
+
+```text
+A
+```
+
+Diamond for letter 'C':
+
+```text
+··A··
+·B·B·
+C···C
+·B·B·
+··A··
+```
+
+Diamond for letter 'E':
+
+```text
+····A····
+···B·B···
+··C···C··
+·D·····D·
+E·······E
+·D·····D·
+··C···C··
+···B·B···
+····A····
+```
diff --git a/exercises/diamond/project.clj b/exercises/diamond/project.clj
new file mode 100644
index 0000000..f1883c3
--- /dev/null
+++ b/exercises/diamond/project.clj
@@ -0,0 +1,4 @@
+(defproject diamond "0.1.0-SNAPSHOT"
+  :description "diamond exercise."
+  :url "https://github.com/exercism/xclojure/tree/master/exercises/diamond"
+  :dependencies [[org.clojure/clojure "1.8.0"]])
diff --git a/exercises/diamond/src/example.clj b/exercises/diamond/src/example.clj
new file mode 100644
index 0000000..909b2fe
--- /dev/null
+++ b/exercises/diamond/src/example.clj
@@ -0,0 +1,22 @@
+(ns diamond)
+
+(def alphabet (map char (range (int \A) (inc (int \Z)))))
+
+(defn- pad [x]
+  (apply str (repeat x " ")))
+
+(defn- one-row [letter [inner-padding outer-padding]]
+  (if (= inner-padding 0)
+    (str (pad outer-padding) letter (pad outer-padding))
+    (str (pad outer-padding) letter (pad inner-padding) letter (pad outer-padding))))
+
+(defn- row-paddings [num-letters]
+  (let [inner-padding (conj (iterate (partial + 2) 1) 0)
+        outer-padding (iterate dec (dec num-letters))]
+    (take num-letters (map vector inner-padding outer-padding))))
+
+(defn diamond [c]
+  (let [num-letters (- (int c) (dec (int \A)))
+        top-half (map one-row alphabet (row-paddings num-letters))]
+    (concat top-half (rest (reverse top-half)))))
+


Test patch:
diff --git a/exercises/diamond/test/diamond_test.clj b/exercises/diamond/test/diamond_test.clj
new file mode 100644
index 0000000..0e99eec
--- /dev/null
+++ b/exercises/diamond/test/diamond_test.clj
@@ -0,0 +1,79 @@
+(ns diamond-test
+  (:require [clojure.test :refer [deftest is are]]
+            [diamond :refer [diamond]]))
+
+(deftest single-a-row
+  (is (= (diamond \A) ["A"])))
+
+(deftest b-diamond
+  (is (= (diamond \B) [" A "
+                       "B B"
+                       " A "])))
+(deftest c-diamond
+  (is (= (diamond \C) ["  A  "
+                       " B B "
+                       "C   C"
+                       " B B "
+                       "  A  "])))
+
+(deftest d-diamond
+  (is (= (diamond \D) ["   A   "
+                       "  B B  "
+                       " C   C "
+                       "D     D"
+                       " C   C "
+                       "  B B  "
+                       "   A   "])))
+
+(deftest full-z-diamond
+  (is (= (diamond \Z) ["                         A                         "
+                       "                        B B                        "
+                       "                       C   C                       "
+                       "                      D     D                      "
+                       "                     E       E                     "
+                       "                    F         F                    "
+                       "                   G           G                   "
+                       "                  H             H                  "
+                       "                 I               I                 "
+                       "                J                 J                "
+                       "               K                   K               "
+                       "              L                     L              "
+                       "             M                       M             "
+                       "            N                         N            "
+                       "           O                           O           "
+                       "          P                             P          "
+                       "         Q                               Q         "
+                       "        R                                 R        "
+                       "       S                                   S       "
+                       "      T                                     T      "
+                       "     U                                       U     "
+                       "    V                                         V    "
+                       "   W                                           W   "
+                       "  X                                             X  "
+                       " Y                                               Y "
+                       "Z                                                 Z"
+                       " Y                                               Y "
+                       "  X                                             X  "
+                       "   W                                           W   "
+                       "    V                                         V    "
+                       "     U                                       U     "
+                       "      T                                     T      "
+                       "       S                                   S       "
+                       "        R                                 R        "
+                       "         Q                               Q         "
+                       "          P                             P          "
+                       "           O                           O           "
+                       "            N                         N            "
+                       "             M                       M             "
+                       "              L                     L              "
+                       "               K                   K               "
+                       "                J                 J                "
+                       "                 I               I                 "
+                       "                  H             H                  "
+                       "                   G           G                   "
+                       "                    F         F                    "
+                       "                     E       E                     "
+                       "                      D     D                      "
+                       "                       C   C                       "
+                       "                        B B                        "
+                       "                         A                         "])))