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

## to OCR Numbers in the Scala Track

Published at Sep 02 2019 · 0 comments
Instructions
Test suite
Solution

Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.

# Step One

To begin with, convert a simple binary font to a string containing 0 or 1.

The binary font uses pipes and underscores, four rows high and three columns wide.

``````     _   #
| |  # zero.
|_|  #
# the fourth row is always blank
``````

Is converted to "0"

``````         #
|  # one.
|  #
# (blank fourth row)
``````

Is converted to "1"

If the input is the correct size, but not recognizable, your program should return '?'

If the input is the incorrect size, your program should return an error.

# Step Two

Update your program to recognize multi-character binary strings, replacing garbled numbers with ?

# Step Three

Update your program to recognize all numbers 0 through 9, both individually and as part of a larger string.

`````` _
_|
|_

``````

Is converted to "2"

``````      _  _     _  _  _  _  _  _  #
| _| _||_||_ |_   ||_||_|| | # decimal numbers.
||_  _|  | _||_|  ||_| _||_| #
# fourth line is always blank
``````

Is converted to "1234567890"

# Step Four

Update your program to handle multiple numbers, one per line. When converting several lines, join the lines with commas.

``````    _  _
| _| _|
||_  _|

_  _
|_||_ |_
| _||_|

_  _  _
||_||_|
||_| _|

``````

Is converted to "123,456,789"

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

Inspired by the Bank OCR kata http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR

## Submitting Incomplete Solutions

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

### OcrNumbersTest.scala

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

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

test("Recognizes 0") {
OcrNumbers.convert(List(" _ ",
"| |",
"|_|",
"   ")) should be("0")
}

test("Recognizes 1") {
pending
OcrNumbers.convert(List("   ",
"  |",
"  |",
"   ")) should be("1")
}

test("Unreadable but correctly sized inputs return ?") {
pending
OcrNumbers.convert(List("   ",
"  _",
"  |",
"   ")) should be("?")
}

test(
"Input with a number of lines that is not a multiple of four raises an error") {
pending
OcrNumbers.convert(List(" _ ",
"| |",
"   ")) should be("?")
}

test(
"Input with a number of columns that is not a multiple of three raises an error") {
pending
OcrNumbers.convert(List("    ",
"   |",
"   |",
"    ")) should be("?")
}

test("Recognizes 110101100") {
pending
OcrNumbers.convert(
List("       _     _        _  _ ",
"  |  || |  || |  |  || || |",
"  |  ||_|  ||_|  |  ||_||_|",
"                           ")) should be("110101100")
}

test("Garbled numbers in a string are replaced with ?") {
pending
OcrNumbers.convert(
List("       _     _           _ ",
"  |  || |  || |     || || |",
"  |  | _|  ||_|  |  ||_||_|",
"                           ")) should be("11?10?1?0")
}

test("Recognizes 2") {
pending
OcrNumbers.convert(List(" _ ",
" _|",
"|_ ",
"   ")) should be("2")
}

test("Recognizes 3") {
pending
OcrNumbers.convert(List(" _ ",
" _|",
" _|",
"   ")) should be("3")
}

test("Recognizes 4") {
pending
OcrNumbers.convert(List("   ",
"|_|",
"  |",
"   ")) should be("4")
}

test("Recognizes 5") {
pending
OcrNumbers.convert(List(" _ ",
"|_ ",
" _|",
"   ")) should be("5")
}

test("Recognizes 6") {
pending
OcrNumbers.convert(List(" _ ",
"|_ ",
"|_|",
"   ")) should be("6")
}

test("Recognizes 7") {
pending
OcrNumbers.convert(List(" _ ",
"  |",
"  |",
"   ")) should be("7")
}

test("Recognizes 8") {
pending
OcrNumbers.convert(List(" _ ",
"|_|",
"|_|",
"   ")) should be("8")
}

test("Recognizes 9") {
pending
OcrNumbers.convert(List(" _ ",
"|_|",
" _|",
"   ")) should be("9")
}

test("Recognizes string of decimal numbers") {
pending
OcrNumbers.convert(
List("    _  _     _  _  _  _  _  _ ",
"  | _| _||_||_ |_   ||_||_|| |",
"  ||_  _|  | _||_|  ||_| _||_|",
"                              ")) should be("1234567890")
}

test(
"Numbers separated by empty lines are recognized. Lines are joined by commas.") {
pending
OcrNumbers.convert(
List("    _  _ ",
"  | _| _|",
"  ||_  _|",
"         ",
"    _  _ ",
"|_||_ |_ ",
"  | _||_|",
"         ",
" _  _  _ ",
"  ||_||_|",
"  ||_| _|",
"         ")) should be("123,456,789")
}
}``````
``````object OcrNumbers {
private val parseDigit: Map[List[String], Char] =
Map(List(" _ ", "| |", "|_|", "   ") -> '0',
List("   ", "  |", "  |", "   ") -> '1',
List(" _ ", " _|", "|_ ", "   ") -> '2',
List(" _ ", " _|", " _|", "   ") -> '3',
List("   ", "|_|", "  |", "   ") -> '4',
List(" _ ", "|_ ", " _|", "   ") -> '5',
List(" _ ", "|_ ", "|_|", "   ") -> '6',
List(" _ ", "  |", "  |", "   ") -> '7',
List(" _ ", "|_|", "|_|", "   ") -> '8',
List(" _ ", "|_|", " _|", "   ") -> '9') withDefaultValue '?'

private def convertLine(xs: List[String]): String =
if (xs.head.length % 3 != 0) "?"
else xs.map(_.grouped(3).toList).transpose.map(parseDigit).mkString

def convert(xs: List[String]): String =
xs.grouped(4).map(convertLine).mkString(",")
}``````