🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # phamduc's solution

## to Diamond in the Scala Track

Published at Jun 04 2019 · 1 comment
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.

## 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··
·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····
``````

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Submitting Incomplete Solutions

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

### DiamondTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class DiamondTest extends FunSuite with Matchers {

test("Degenerate case with a single 'A' row") {
Diamond.rows('A') should be(List("A"))
}

test("Degenerate case with no row containing 3 distinct groups of spaces") {
pending
Diamond.rows('B') should be(List(" A ", "B B", " A "))
}

test("Smallest non-degenerate case with odd diamond side length") {
pending
Diamond.rows('C') should be(
List("  A  ", " B B ", "C   C", " B B ", "  A  "))
}

test("Smallest non-degenerate case with even diamond side length") {
pending
Diamond.rows('D') should be(
List("   A   ",
"  B B  ",
" C   C ",
"D     D",
" C   C ",
"  B B  ",
"   A   "))
}

test("Largest possible diamond") {
pending
Diamond.rows('Z') should be(
List(
"                         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                         "
))
}
}``````
``````object Diamond {
val letters = 'A' to 'Z' toList

def rows(char : Char): List[String] = {
require(char.isUpper)
val select = letters.takeWhile(_ <= char)
val line = (select.tail.reverse:::select).mkString
val picture = select.map(c => line.replaceAll("[^"+c+"]"," "))
picture:::picture.reverse.tail
}
}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more. Ingenious: use the precompiled 'line' (e.g. FEDCBABCDEF) for each step, then replace everything that is not the character for that like (e.g. 'D') with a space.

### What can you learn from this solution?

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?