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 where
import Prelude
import Data.Array (mapWithIndex, (..))
import Data.Char (fromCharCode, toCharCode)
import Data.String (joinWith, singleton)
newtype MyType a = MyType a
instance functorMyType :: Functor MyType where
map f (MyType x) = MyType $ f x
start :: Int
start = toCharCode 'A'
rows :: Char -> Array String
rows ch = let
size = (toCharCode ch) - start
f i n = formatRow $
fromCharCode (start + delta)
where
delta | i <= size = i
| otherwise = 2*size - i
formatRow c = joinWith "" $ do
j <- 0..(2*size)
pure $ singleton $ case j of
_ | j + delta == size -> c
_ | j - delta == size -> c
_ -> ' '
in
mapWithIndex f $ 0..(2*size)
```

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?

Level up your programming skills with 3,047 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments