# Scala algorithm: Print Alphabet Diamond

Published

## Algorithm goal

Given a letter such as C, print a diamond using letters A to C, for example:

```  A
B B
C   C
B B
A  ```

## Test cases in Scala

``````assert(fillingLength(forLetter = 'B', toLetter = 'B') == 1)
assert(fillingLength(forLetter = 'B', toLetter = 'C') == 1)
assert(diamond('A') == "A")
assert(diamond('B') == " A \nB B\n A ")
assert(diamond('C') == "  A  \n B B \nC   C\n B B \n  A  ")
``````

## Algorithm in Scala

20 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!

## Explanation

First thing to notice that a Diamond is symmetric vertically: we can then reduce the algorithm to taking the top half, and putting the bottom half by re-using the top through reversing and dropping the last element.

We can in fact represent a range of letters using Scala's Range concept, and from that, compute the length of the padding and the filling around the diamond. (this is © from www.scala-algorithms.com)

Scala provides a simple way to repeat a character multiple times, and String interpolation enables straight-forward concatenation like you would have in languages such as PHP.

## Scala concepts & Hints

1. ### Drop, Take, dropRight, takeRight

Scala's `drop` and `take` methods typically remove or select `n` items from a collection.

``````assert(List(1, 2, 3).drop(2) == List(3))

assert(List(1, 2, 3).take(2) == List(1, 2))

assert(List(1, 2, 3).dropRight(2) == List(1))

assert(List(1, 2, 3).takeRight(2) == List(2, 3))

assert((1 to 5).take(2) == (1 to 2))
``````
2. ### Range

The `(1 to n)` syntax produces a "Range" which is a representation of a sequence of numbers.

``````assert((1 to 5).toString == "Range 1 to 5")

assert((1 to 5).reverse.toString() == "Range 5 to 1 by -1")

assert((1 to 5).toList == List(1, 2, 3, 4, 5))
``````
3. ### View

The `.view` syntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.

# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

## How our 100 algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.

### Study our 100 Scala Algorithms: 6 fully free, 100 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 22 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.