# angelikatyborska's solution

## to Diamond in the Erlang Track

Published at Dec 15 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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':

``````A
``````

Diamond for letter 'C':

``````路路A路路

C路路路C

``````

Diamond for letter 'E':

``````路路路路A路路路路

E路路路路路路路E

``````

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Submitting Incomplete Solutions

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

### diamond_tests.erl

``````%% Based on canonical data version 1.1.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/diamond/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(diamond_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_degenerate_case_with_a_single_a_row_test'() ->
Expected=[
"A"
],
?assertEqual(Expected, diamond:rows("A")).

'2_degenerate_case_with_no_row_containing_3_distinct_groups_of_spaces_test'() ->
Expected=[
" A ",
"B B",
" A "
],
?assertEqual(Expected, diamond:rows("B")).

'3_smallest_non_degenerate_case_with_odd_diamond_side_length_test'() ->
Expected=[
"  A  ",
" B B ",
"C   C",
" B B ",
"  A  "
],
?assertEqual(Expected, diamond:rows("C")).

'4_smallest_non_degenerate_case_with_even_diamond_side_length_test'() ->
Expected=[
"   A   ",
"  B B  ",
" C   C ",
"D     D",
" C   C ",
"  B B  ",
"   A   "
],
?assertEqual(Expected, diamond:rows("D")).

'5_largest_possible_diamond_test'() ->
Expected=[
"                         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                         "
],
?assertEqual(Expected, diamond:rows("Z")).``````
``````-module(diamond).

-export([rows/1]).

-define(Space, 32).

rows([LastLetter]) ->
RowLetters = lists:seq(\$A, LastLetter) ++ lists:seq(LastLetter - 1, \$A, -1),
lists:map(fun(L) -> row(L, LastLetter) end, RowLetters).

row(\$A, LastLetter) ->
OuterSpaces = lists:duplicate(LastLetter - \$A, ?Space),
OuterSpaces ++ [\$A] ++ OuterSpaces;
row(Letter, LastLetter) ->
OuterSpaces = lists:duplicate(LastLetter - Letter, ?Space),
InnerSpaces = lists:duplicate((Letter - \$A) * 2 - 1, ?Space),
OuterSpaces ++ [Letter] ++ InnerSpaces ++ [Letter] ++ OuterSpaces.``````