Published at Jul 13 2018
·
0 comments

Instructions

Test suite

Solution

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.

- 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.

In the following examples, spaces are indicated by `·`

characters.

Diamond for letter 'A':

```
A
```

Diamond for letter 'C':

```
··A··
·B·B·
C···C
·B·B·
··A··
```

Diamond for letter 'E':

```
····A····
···B·B···
··C···C··
·D·····D·
E·······E
·D·····D·
··C···C··
···B·B···
····A····
```

Seb Rose http://claysnow.co.uk/recycling-tests-in-tdd/

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
module Test.Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.AVar (AVAR)
import Control.Monad.Eff.Console (CONSOLE)
import Test.Unit.Assert as Assert
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)
import Diamond (rows)
main :: forall eff
. Eff ( avar :: AVAR
, console :: CONSOLE
, testOutput :: TESTOUTPUT
| eff
)
Unit
main = runTest suites
suites :: forall e. TestSuite e
suites = do
suite "Diamond.rows" do
test "Degenerate case with a single 'A' row" $
Assert.equal [ "A"
]
(rows 'A')
test "Degenerate case with no row containing 3 distinct groups of spaces" $
Assert.equal [ " A "
, "B B"
, " A "
]
(rows 'B')
test "Smallest non-degenerate case with odd diamond side length" $
Assert.equal [ " A "
, " B B "
, "C C"
, " B B "
, " A "
]
(rows 'C')
test "Smallest non-degenerate case with even diamond side length" $
Assert.equal [ " A "
, " B B "
, " C C "
, "D D"
, " C C "
, " B B "
, " A "
]
(rows 'D')
test "Largest possible diamond" $
Assert.equal [ " 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 "
]
(rows 'Z')
```

```
module Diamond
(rows
) where
import Prelude
import Data.Array (reverse, drop, (..))
import Data.Char (toCharCode)
import Data.Enum (enumFromTo)
import Data.String (fromCharArray)
rows ∷ Char → Array String
rows c =
map (row size) $ forward <> backward
where
size ∷ Int -- size of the diamond, depending on the char
size = 2*((toCharCode c) - 65)+1
forward ∷ Array Char
forward = enumFromTo 'A' c
backward ∷ Array Char
backward = drop 1 $ reverse forward
row ∷ Int → Char → String
row s x =
fromCharArray $ map (\i →
if i == middle-charNum || i == middle+charNum then
x
else
' ') $ 0..(s-1)
where
charNum = (toCharCode x) - 65
middle = s/2
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

## Community comments