Avatar of MohamedHajr

MohamedHajr's solution

to Diamond in the Scala Track

Published at Aug 12 2019 · 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.

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.

Source

Seb Rose http://claysnow.co.uk/recycling-tests-in-tdd/

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 {
  
  def drawRow(char: Char, innerSpaces: String, outerSpaces: String): String = char match {
    case 'A'  => s"$outerSpaces$char$outerSpaces"
    case  _ => s"$outerSpaces$char$innerSpaces$char$outerSpaces"
  }

  def createRow(row: (Char, Int), totalLength: Int): String = {
    val (char, index) = row
    val outerSpaces = " " * (totalLength - index - 1)
    val innerSpaces = if (index == 0) "" else " " * (index * 2 - 1)

    drawRow(char, innerSpaces, outerSpaces)
  }
  
  def rows(char: Char): List[String] = {
    val upperChars = ('A' to char).zipWithIndex
    val bottomChars = upperChars.reverse.tail

    val upper = upperChars map { createRow(_, upperChars.length) } toList 
    val bottom = bottomChars.map{ createRow(_, upperChars.length) }.toList

    upper ::: bottom
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?