ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Aug 21 2019
·
0 comments

Instructions

Test suite

Solution

Given the size, return a square matrix of numbers in spiral order.

The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples:

```
1 2 3
8 9 4
7 6 5
```

```
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
```

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.

Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/

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

```
import org.scalatest.{Matchers, FunSuite}
/** @version 1.1.0 */
class SpiralMatrixTest extends FunSuite with Matchers {
test("empty spiral") {
SpiralMatrix.spiralMatrix(0) should be(List())
}
test("trivial spiral") {
pending
SpiralMatrix.spiralMatrix(1) should be(List(List(1)))
}
test("spiral of size 2") {
pending
SpiralMatrix.spiralMatrix(2) should be(List(List(1, 2),
List(4, 3)))
}
test("spiral of size 3") {
pending
SpiralMatrix.spiralMatrix(3) should be(
List(List(1, 2, 3),
List(8, 9, 4),
List(7, 6, 5)))
}
test("spiral of size 4") {
pending
SpiralMatrix.spiralMatrix(4) should be(
List(List(1, 2, 3, 4),
List(12, 13, 14, 5),
List(11, 16, 15, 6),
List(10, 9, 8, 7)))
}
test("spiral of size 5") {
pending
SpiralMatrix.spiralMatrix(5) should be(
List(List(1, 2, 3, 4, 5),
List(16, 17, 18, 19, 6),
List(15, 24, 25, 20, 7),
List(14, 23, 22, 21, 8),
List(13, 12, 11, 10, 9)))
}
}
```

```
object SpiralMatrix {
def spiralMatrix(size: Int): List[List[Int]] = {
def spiralMatrixValue(size: Int, startValue: Int, row: Int, col: Int): Int = {
(row, col) match {
case (0, _) => startValue + col
case _ if col == size - 1 => startValue + size + row - 1
case _ if row == size - 1 => startValue + size * 3 - 3 - col
case (_, 0) => startValue + size * 4 - 4 - row
case _ => spiralMatrixValue(size - 2, startValue + size * 4 - 4, row - 1, col - 1)
}
}
// List.range(0, size).map(row =>
// List.range(0, size).map(col => spiralMatrixValue(size, 1, row, col)))
List.tabulate(size, size)((x: Int, y: Int) => spiralMatrixValue(size, 1, x, y))
}
}
```

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?

Level up your programming skills with 3,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments