Published at Aug 03 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 Effect (Effect)
import Test.Unit.Assert as Assert
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Main (runTest)
import Diamond (rows)
main :: Effect Unit
main = runTest suites
suites :: TestSuite
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 (replicate, reverse, (..))
import Data.Enum (enumFromTo)
import Data.Foldable (foldMap)
import Data.FunctorWithIndex (mapWithIndex)
import Data.Map as Map
import Data.Maybe (Maybe, fromMaybe)
import Data.String (CodePoint, codePointFromChar, joinWith, singleton)
import Data.Tuple (Tuple(..))
codePoints :: Array CodePoint
codePoints =
enumFromTo
(codePointFromChar 'A')
(codePointFromChar 'Z')
codePointIndex :: CodePoint -> Maybe Int
codePointIndex = \i -> Map.lookup i mapping
where
mapping = Map.fromFoldable (mapWithIndex (flip Tuple) codePoints)
indexCodePoint :: Int -> Maybe CodePoint
indexCodePoint = \i -> Map.lookup i mapping
where
mapping = Map.fromFoldable (mapWithIndex Tuple codePoints)
blank :: Int -> String
blank size = joinWith "" (replicate size " ")
newtype Size = Size Int
newtype Index = Index Int
row :: Size -> Index -> String
row (Size size) (Index 0) =
space <> "A" <> space
where
space = blank size
row (Size size) (Index index) =
outer <> inner <> outer
where
outer = blank (size - index)
char = foldMap singleton (indexCodePoint index)
inner = char <> blank ((2 * index) - 1) <> char
rows :: Char -> Array String
rows char =
row (Size size) <<< Index <$> (indices <> [size] <> reverse indices)
where
size = fromMaybe (-1) (codePointIndex (codePointFromChar char))
indices = if size <= 0 then [] else (0 .. (size - 1))
```

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,284 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments