馃帀 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 馃帀

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路路路路
```

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

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.

```
local diamond = require 'diamond'
describe('diamond', function()
it('should generate the diamond for A', function()
assert.are.equal('A\n', diamond('A'))
end)
it('should generate the diamond for B', function()
local expected =
' A \n' ..
'B B\n' ..
' A \n'
assert.are.equal(expected, diamond('B'))
end)
it('should generate the diamond for C', function()
local expected =
' A \n' ..
' B B \n' ..
'C C\n' ..
' B B \n' ..
' A \n'
assert.are.equal(expected, diamond('C'))
end)
it('should generate the diamond for E', function()
local expected =
' A \n' ..
' B B \n' ..
' C C \n' ..
' D D \n' ..
'E E\n' ..
' D D \n' ..
' C C \n' ..
' B B \n' ..
' A \n'
assert.are.equal(expected, diamond('E'))
end)
it('should generate the diamond for H', function()
local expected =
' A \n' ..
' B B \n' ..
' C C \n' ..
' D D \n' ..
' E E \n' ..
' F F \n' ..
' G G \n' ..
'H H\n' ..
' G G \n' ..
' F F \n' ..
' E E \n' ..
' D D \n' ..
' C C \n' ..
' B B \n' ..
' A \n'
assert.are.equal(expected, diamond('H'))
end)
end)
```

```
return function(seed)
local start = 'A'
local max = seed:byte()
local min = start:byte()
local diff = max - min
if diff == 0 then
return 'A\n'
end
local diamond = seed .. (' '):rep(diff - 1).. ' ' .. (' '):rep(diff - 1) .. seed .. '\n'
for index=max - 1, min + 1, -1 do
local c = string.char(index)
local line = (' '):rep(max - index) .. c .. (' '):rep(index - min)
line = line .. string.rep(' ', index - min - 1) .. c .. string.rep(' ', max - index) .. '\n'
diamond = line .. diamond .. line
end
local first_line = (' '):rep(diff) .. start .. (' '):rep(diff) .. '\n'
diamond = first_line .. diamond .. first_line
return diamond
end
```

A huge amount can be learned from reading other people鈥檚 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,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments