# Scala algorithm: Check Sudoku board

Published

## Algorithm goal

The game of Sudoku is composed of a 9x9 grid that is valid when each of the rows, columns, and every of the 9 3-by-3 squares contains only a unique combination of numbers 1 to 9 (or no number). The game is complete when all the squares are filled. In this challenge, create a function to check whether a given board is valid, as it is easy for the players to miss something.

## Test cases in Scala

``````assert(SudokuBoard.blank.isValid)
assert(SudokuBoard.fromStrings("1").isValid)
assert(SudokuBoard.fromStrings("12").isValid)
assert(SudokuBoard.fromStrings("1", "2").isValid)
assert(SudokuBoard.fromStrings("1", " 2").isValid)
assert(!SudokuBoard.fromStrings("1", "1").isValid)
assert(!SudokuBoard.fromStrings("11").isValid)
assert(!SudokuBoard.fromStrings("1", " 1").isValid)
assert(!SudokuBoard.fromStrings("1       1").isValid)
assert(
!SudokuBoard.fromStrings("1", " ", " ", " ", " ", " ", " ", " ", "1").isValid
)
assert(
SudokuBoard.fromStrings("1", " ", " ", " ", " ", " ", " ", " ", "9").isValid
)
``````

## Algorithm in Scala

45 lines of Scala (compatible versions 2.13 & 3.0).

## Explanation

The trickiest part of this challenge is actually the representation of the grid. While it may be tempting to go for an Array/Vector-based solution, such a board is actually far simpler to represent as a Map, and its 'PositionGroup's as Sets of positions sa well.

Initially, we generate SudokuBoard.Groups by: (this is © from www.scala-algorithms.com)

• Taking a row, then shifting it down by 1 8 times
• Taking a row, turning it into a column, then shifting it right by 1 for 8 times
• Taking a 3x3 box, then doing a combination of shifting it by 1 and 2 times in both x and y directions.

This then gives us a set of Groups that we can validate the SudokuBoard with.

## Scala concepts & Hints

1. ### For-comprehension

The for-comprehension is highly important syntatic enhancement in functional programming languages.

``````val Multiplier = 10

val result: List[Int] = for {
num <- List(1, 2, 3)
anotherNum <-
List(num * Multiplier - 1, num * Multiplier, num * Multiplier + 1)
} yield anotherNum + 1

assert(result == List(10, 11, 12, 20, 21, 22, 30, 31, 32))
``````
2. ### Option Type

The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!

``````assert(Option(1).flatMap(x => Option(x + 2)) == Option(3))

assert(Option(1).flatMap(x => None) == None)
``````
3. ### Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

``````assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")
``````
4. ### 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))
``````

# 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.