Avatar of paulfioravanti

paulfioravanti's solution

to OCR Numbers in the Ruby Track

Published at Jul 13 2018 · 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"

Some editors trim whitespace. If you rely on trailing whitespace in a multiline string, instead use a format that doesn't rely on trailing whitespace, or adjust the settings in your editor.

A multiline string cheatsheet for ruby


For installation and learning resources, refer to the Ruby resources 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.2.0 965ecad
class OcrNumbersTest < Minitest::Test
  def test_recognizes_0
    # skip
    input = [" _ ",
             "| |",
             "|_|",
             "   "].join("\n")
    assert_equal "0", OcrNumbers.convert(input)
  end

  def test_recognizes_1
    skip
    input = ["   ",
             "  |",
             "  |",
             "   "].join("\n")
    assert_equal "1", OcrNumbers.convert(input)
  end

  def test_unreadable_but_correctly_sized_inputs_return_question_mark
    skip
    input = ["   ",
             "  _",
             "  |",
             "   "].join("\n")
    assert_equal "?", OcrNumbers.convert(input)
  end

  def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error
    skip
    input = [" _ ",
             "| |",
             "   "].join("\n")
    assert_raises(ArgumentError) do
      OcrNumbers.convert(input)
    end
  end

  def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error
    skip
    input = ["    ",
             "   |",
             "   |",
             "    "].join("\n")
    assert_raises(ArgumentError) do
      OcrNumbers.convert(input)
    end
  end

  def test_recognizes_110101100
    skip
    input = ["       _     _        _  _ ",
             "  |  || |  || |  |  || || |",
             "  |  ||_|  ||_|  |  ||_||_|",
             "                           "].join("\n")
    assert_equal "110101100", OcrNumbers.convert(input)
  end

  def test_garbled_numbers_in_a_string_are_replaced_with_question_mark
    skip
    input = ["       _     _           _ ",
             "  |  || |  || |     || || |",
             "  |  | _|  ||_|  |  ||_||_|",
             "                           "].join("\n")
    assert_equal "11?10?1?0", OcrNumbers.convert(input)
  end

  def test_recognizes_2
    skip
    input = [" _ ",
             " _|",
             "|_ ",
             "   "].join("\n")
    assert_equal "2", OcrNumbers.convert(input)
  end

  def test_recognizes_3
    skip
    input = [" _ ",
             " _|",
             " _|",
             "   "].join("\n")
    assert_equal "3", OcrNumbers.convert(input)
  end

  def test_recognizes_4
    skip
    input = ["   ",
             "|_|",
             "  |",
             "   "].join("\n")
    assert_equal "4", OcrNumbers.convert(input)
  end

  def test_recognizes_5
    skip
    input = [" _ ",
             "|_ ",
             " _|",
             "   "].join("\n")
    assert_equal "5", OcrNumbers.convert(input)
  end

  def test_recognizes_6
    skip
    input = [" _ ",
             "|_ ",
             "|_|",
             "   "].join("\n")
    assert_equal "6", OcrNumbers.convert(input)
  end

  def test_recognizes_7
    skip
    input = [" _ ",
             "  |",
             "  |",
             "   "].join("\n")
    assert_equal "7", OcrNumbers.convert(input)
  end

  def test_recognizes_8
    skip
    input = [" _ ",
             "|_|",
             "|_|",
             "   "].join("\n")
    assert_equal "8", OcrNumbers.convert(input)
  end

  def test_recognizes_9
    skip
    input = [" _ ",
             "|_|",
             " _|",
             "   "].join("\n")
    assert_equal "9", OcrNumbers.convert(input)
  end

  def test_recognizes_string_of_decimal_numbers
    skip
    input = ["    _  _     _  _  _  _  _  _ ",
             "  | _| _||_||_ |_   ||_||_|| |",
             "  ||_  _|  | _||_|  ||_| _||_|",
             "                              "].join("\n")
    assert_equal "1234567890", OcrNumbers.convert(input)
  end

  def test_numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_commas
    skip
    input = ["    _  _ ",
             "  | _| _|",
             "  ||_  _|",
             "         ",
             "    _  _ ",
             "|_||_ |_ ",
             "  | _||_|",
             "         ",
             " _  _  _ ",
             "  ||_||_|",
             "  ||_| _|",
             "         "].join("\n")
    assert_equal "123,456,789", OcrNumbers.convert(input)
  end
end
# frozen_string_literal: true

module OcrNumbers
  CONVERSIONS = Hash.new("?").tap do |hash|
    hash[" _ | ||_|   "] = "0"
    hash["     |  |   "] = "1"
    hash[" _  _||_    "] = "2"
    hash[" _  _| _|   "] = "3"
    hash["   |_|  |   "] = "4"
    hash[" _ |_  _|   "] = "5"
    hash[" _ |_ |_|   "] = "6"
    hash[" _   |  |   "] = "7"
    hash[" _ |_||_|   "] = "8"
    hash[" _ |_| _|   "] = "9"
  end.freeze
  private_constant :CONVERSIONS
  HEIGHT = 4
  private_constant :HEIGHT
  WIDTH = 3
  private_constant :WIDTH

  module_function

  def convert(pipes)
    pipes = pipes.split("\n")
    raise ArgumentError unless valid_pipes?(pipes)

    pipes
      .map { |row| row.scan(/.{#{WIDTH}}/) }
      .transpose
      .map { |number_array| number_array.each_slice(HEIGHT).to_a }
      .transpose
      .map { |number_array| ocr_to_number(number_array) }
      .join(",")
  end

  def valid_pipes?(pipes)
    (pipes.length % HEIGHT).zero? &&
      pipes.all? { |pipe| (pipe.length % WIDTH).zero? }
  end
  private_class_method :valid_pipes?

  def ocr_to_number(number_array)
    number_array.map { |arr| CONVERSIONS[arr.join] }.join
  end
  private_class_method :ocr_to_number
end

Community comments

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

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?