 # rootulp's solution

## to OCR Numbers in the Ruby 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"

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby ocr_numbers_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride ocr_numbers_test.rb
``````

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

``````require 'minitest/autorun'
require_relative 'ocr_numbers'

# Common test data version: 1.0.0 80782b6
class OcrNumbersTest < Minitest::Test
def test_recognizes_0
# skip
assert_equal "0", OcrNumbers.convert(" _ \n| |\n|_|\n   ")
end

def test_recognizes_1
skip
assert_equal "1", OcrNumbers.convert("   \n  |\n  |\n   ")
end

skip
assert_equal "?", OcrNumbers.convert("   \n  _\n  |\n   ")
end

def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error
skip
assert_raises(ArgumentError) { OcrNumbers.convert(" _ \n| |\n   ") }
end

def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error
skip
assert_raises(ArgumentError) { OcrNumbers.convert("    \n   |\n   |\n    ") }
end

def test_recognizes_110101100
skip
assert_equal "110101100", OcrNumbers.convert("       _     _        _  _ \n  |  || |  || |  |  || || |\n  |  ||_|  ||_|  |  ||_||_|\n                           ")
end

def test_garbled_numbers_in_a_string_are_replaced_with_?
skip
assert_equal "11?10?1?0", OcrNumbers.convert("       _     _           _ \n  |  || |  || |     || || |\n  |  | _|  ||_|  |  ||_||_|\n                           ")
end

def test_recognizes_2
skip
assert_equal "2", OcrNumbers.convert(" _ \n _|\n|_ \n   ")
end

def test_recognizes_3
skip
assert_equal "3", OcrNumbers.convert(" _ \n _|\n _|\n   ")
end

def test_recognizes_4
skip
assert_equal "4", OcrNumbers.convert("   \n|_|\n  |\n   ")
end

def test_recognizes_5
skip
assert_equal "5", OcrNumbers.convert(" _ \n|_ \n _|\n   ")
end

def test_recognizes_6
skip
assert_equal "6", OcrNumbers.convert(" _ \n|_ \n|_|\n   ")
end

def test_recognizes_7
skip
assert_equal "7", OcrNumbers.convert(" _ \n  |\n  |\n   ")
end

def test_recognizes_8
skip
assert_equal "8", OcrNumbers.convert(" _ \n|_|\n|_|\n   ")
end

def test_recognizes_9
skip
assert_equal "9", OcrNumbers.convert(" _ \n|_|\n _|\n   ")
end

def test_recognizes_string_of_decimal_numbers
skip
assert_equal "1234567890", OcrNumbers.convert("    _  _     _  _  _  _  _  _ \n  | _| _||_||_ |_   ||_||_|| |\n  ||_  _|  | _||_|  ||_| _||_|\n                              ")
end

def test_numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_commas
skip
assert_equal "123,456,789", OcrNumbers.convert("    _  _ \n  | _| _|\n  ||_  _|\n         \n    _  _ \n|_||_ |_ \n  | _||_|\n         \n _  _  _ \n  ||_||_|\n  ||_| _|\n         ")
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
#   VERSION = 1 # Where the version number matches the one in the test.
# end
#
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end``````
``````# OCR
class OCR
GARBLE = '?'.freeze
RAW_NUMS = <<-RAW_NUMS.chomp
_     _  _     _  _  _  _  _
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|

RAW_NUMS

def initialize(input)
@input = input
@nums = split_blocks(RAW_NUMS)
end

def convert
split_chunks(input).map do |chunk|
split_blocks(chunk).map do |block|
translate_block(block)
end.join
end.join(',')
end

private

def translate_block(block)
nums.include?(block) ? nums.index(block).to_s : GARBLE
end

def split_chunks(chunks)
chunks.split("\n\n")
end

# split_blocks is too complex - consider splitting this up
def split_blocks(blocks)
blocks.split("\n")
.map { |block| block.scan(/.{1,3}/) }
.map { |row| row.map { |val| val.ljust(3) } }
.map { |row| row.empty? ? ['   '] : row } # for 1 and 4
.transpose
.map { |x| x.join("\n") }
end
end``````