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

## to OCR Numbers in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

``````\$ elixir ocr_numbers_test.exs
``````

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

For more detailed information about the Elixir track, please 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.

### ocr_numbers_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

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``````
``````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]) :: String.t
def convert([
" _ ",
"| |",
"|_|",
"   ",
]), do: {:ok, "0"}

def convert([
"   ",
"  |",
"  |",
"   ",
]), do: {:ok, "1"}

def convert([
" _ ",
" _|",
"|_ ",
"   ",
]), do: {:ok, "2"}

def convert([
" _ ",
" _|",
" _|",
"   ",
]), do: {:ok, "3"}

def convert([
"   ",
"|_|",
"  |",
"   ",
]), do: {:ok, "4"}

def convert([
" _ ",
"|_ ",
" _|",
"   ",
]), do: {:ok, "5"}

def convert([
" _ ",
"|_ ",
"|_|",
"   ",
]), do: {:ok, "6"}

def convert([
" _ ",
"  |",
"  |",
"   ",
]), do: {:ok, "7"}

def convert([
" _ ",
"|_|",
"|_|",
"   ",
]), do: {:ok, "8"}

def convert([
" _ ",
"|_|",
" _|",
"   ",
]), do: {:ok, "9"}

def convert([
<<_ :: bytes-size(3)>>,
<<_ :: bytes-size(3)>>,
<<_ :: bytes-size(3)>>,
<<_ :: bytes-size(3)>>,
]), do: {:ok, "?"}

def convert([
<<l1h :: bytes-size(3), l1t :: binary>>,
<<l2h :: bytes-size(3), l2t :: binary>>,
<<l3h :: bytes-size(3), l3t :: binary>>,
<<l4h :: bytes-size(3), l4t :: binary>>,
]) do
case convert([l1t, l2t, l3t, l4t]) do
{:error, error} -> {:error, error}
{:ok, tail_result} ->
case convert([l1h, l2h, l3h, l4h]) do
{:error, error} -> {:error, error}
end
end
end

def convert([_, _, _, _]), do: {:error, 'invalid column count'}

def convert([l1, l2, l3, l4 | tail]) do
case convert(tail) do
{:error, error} -> {:error, error}
{:ok, tail_result} ->
case convert([l1, l2, l3, l4]) do
{:error, error} -> {:error, error}
end
end
end

def convert(_), do: {:error, 'invalid line count'}
end``````