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

## to OCR Numbers in the Elixir Track

Published at Aug 19 2020 · 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"

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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

### ocr_numbers_test.exs

``````defmodule OcrNumbersTest do
use ExUnit.Case

# @tag :pending
test "Recognizes 0" do
number =
OcrNumbers.convert([
" _ ",
"| |",
"|_|",
"   "
])

assert number == {:ok, "0"}
end

@tag :pending
test "Recognizes 1" do
number =
OcrNumbers.convert([
"   ",
"  |",
"  |",
"   "
])

assert number == {:ok, "1"}
end

@tag :pending
test "Unreadable but correctly sized inputs return ?" do
number =
OcrNumbers.convert([
"   ",
"  _",
"  |",
"   "
])

assert number == {:ok, "?"}
end

@tag :pending
test "Input with a number of lines that is not a multiple of four raises an error" do
number =
OcrNumbers.convert([
" _ ",
"| |",
"   "
])

assert number == {:error, 'invalid line count'}
end

@tag :pending
test "Input with a number of columns that is not a multiple of three raises an error" do
number =
OcrNumbers.convert([
"    ",
"   |",
"   |",
"    "
])

assert number == {:error, 'invalid column count'}
end

@tag :pending
test "Recognizes 110101100" do
number =
OcrNumbers.convert([
"       _     _        _  _ ",
"  |  || |  || |  |  || || |",
"  |  ||_|  ||_|  |  ||_||_|",
"                           "
])

assert number == {:ok, "110101100"}
end

@tag :pending
test "Garbled numbers in a string are replaced with ?" do
number =
OcrNumbers.convert([
"       _     _           _ ",
"  |  || |  || |     || || |",
"  |  | _|  ||_|  |  ||_||_|",
"                           "
])

assert number == {:ok, "11?10?1?0"}
end

@tag :pending
test "Recognizes 2" do
number =
OcrNumbers.convert([
" _ ",
" _|",
"|_ ",
"   "
])

assert number == {:ok, "2"}
end

@tag :pending
test "Recognizes 3" do
number =
OcrNumbers.convert([
" _ ",
" _|",
" _|",
"   "
])

assert number == {:ok, "3"}
end

@tag :pending
test "Recognizes 4" do
number =
OcrNumbers.convert([
"   ",
"|_|",
"  |",
"   "
])

assert number == {:ok, "4"}
end

@tag :pending
test "Recognizes 5" do
number =
OcrNumbers.convert([
" _ ",
"|_ ",
" _|",
"   "
])

assert number == {:ok, "5"}
end

@tag :pending
test "Recognizes 6" do
number =
OcrNumbers.convert([
" _ ",
"|_ ",
"|_|",
"   "
])

assert number == {:ok, "6"}
end

@tag :pending
test "Regonizes 7" do
number =
OcrNumbers.convert([
" _ ",
"  |",
"  |",
"   "
])

assert number == {:ok, "7"}
end

@tag :pending
test "Recognizes 8" do
number =
OcrNumbers.convert([
" _ ",
"|_|",
"|_|",
"   "
])

assert number == {:ok, "8"}
end

@tag :pending
test "Recognizes 9" do
number =
OcrNumbers.convert([
" _ ",
"|_|",
" _|",
"   "
])

assert number == {:ok, "9"}
end

@tag :pending
test "Recognizes string of decimal numbers" do
number =
OcrNumbers.convert([
"    _  _     _  _  _  _  _  _ ",
"  | _| _||_||_ |_   ||_||_|| |",
"  ||_  _|  | _||_|  ||_| _||_|",
"                              "
])

assert number == {:ok, "1234567890"}
end

@tag :pending
test "Numbers separated by empty lines are recognized. Lines are joined by commas." do
number =
OcrNumbers.convert([
"    _  _ ",
"  | _| _|",
"  ||_  _|",
"         ",
"    _  _ ",
"|_||_ |_ ",
"  | _||_|",
"         ",
" _  _  _ ",
"  ||_||_|",
"  ||_| _|",
"         "
])

assert number == {:ok, "123,456,789"}
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule OcrNumbers do
@doc """
Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or
whether it is garbled.
"""
@spec convert([String.t()]) :: {:ok, String.t()} | {:error, charlist()}
def convert(input) do
input
|> validate
|> process
end

defp validate(input) do
input
|> create_token
|> validate_line_count
|> validate_column_count
end

defp create_token(raw_input), do: {:ok, raw_input}

defp validate_line_count({:ok, input}) do
groups_of_4?(rem(length(input), 4) == 0, input)
end
defp groups_of_4?(true, input), do: {:ok, input}
defp groups_of_4?(false, _inp), do: {:error, 'invalid line count'}

defp validate_column_count({:ok, input}) do
x = Enum.all?(input, &(rem(String.length(&1), 3) == 0))
groups_of_3_cols?(x, input)
end
defp validate_column_count(o_o), do: o_o
defp groups_of_3_cols?(true, input), do: {:ok, input}
defp groups_of_3_cols?(false, _inp), do: {:error, 'invalid column count'}

defp process({:error, _} = o_o), do: o_o
defp process({:ok, input}) do
sol = input
|> Enum.chunk_every(4)
|> Enum.map(&break_up_digits/1)
|> Enum.map(&ocr_digits/1)
|> Enum.join(",")
{:ok, sol}
end

defp break_up_digits(input) do
collect_digits(chunkenized_rows(input), [])
end

defp collect_digits([[],[],[],[]], acc), do: Enum.reverse(acc)
defp collect_digits(input, acc) do
head_digit = input |> Enum.map(&hd/1) |> Enum.join
rest = Enum.map(input, &tl/1)
collect_digits(rest, [head_digit | acc])
end

defp chunkenized_rows(input) do
Enum.map(input, &break_row_into_groups_of_3_chars/1)
end

defp break_row_into_groups_of_3_chars(row) do
row
|> String.graphemes
|> Enum.chunk_every(3)
|> Enum.map(&Enum.join/1)
end

defp ocr_digits(list_of_digits) do
list_of_digits |> Enum.map(&to_number/1) |> Enum.join
end

defp to_number(" _ | ||_|   "), do: "0"
defp to_number("     |  |   "), do: "1"
defp to_number(" _  _||_    "), do: "2"
defp to_number(" _  _| _|   "), do: "3"
defp to_number("   |_|  |   "), do: "4"
defp to_number(" _ |_  _|   "), do: "5"
defp to_number(" _ |_ |_|   "), do: "6"
defp to_number(" _   |  |   "), do: "7"
defp to_number(" _ |_||_|   "), do: "8"
defp to_number(" _ |_| _|   "), do: "9"
defp to_number(_anything_else), do: "?"
end``````

## Community comments

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

### sotoseattle's Reflection

Trying to code with skinny margins, smaller functions and more clarity.