Avatar of JaeHyoLee

JaeHyoLee's solution

to OCR Numbers in the Lua Track

Published at Jul 13 2018 · 2 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 the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language 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_spec.lua

local ocr = require('ocr-numbers')

describe('ocr-numbers', function()
  it('should recognize zero', function()
    local actual = ocr.convert(
      ' _ \n' ..
      '| |\n' ..
      '|_|\n' ..
      '   '
    )
    assert.equal('0', actual)
  end)

  it('should recognize one', function()
    local actual = ocr.convert(
      '   \n' ..
      '  |\n' ..
      '  |\n' ..
      '   '
    )
    assert.equal('1', actual)
  end)

  it('should recognize two', function()
    local actual = ocr.convert(
      ' _ \n' ..
      ' _|\n' ..
      '|_ \n' ..
      '   '
    )
    assert.equal('2', actual)
  end)

  it('should recognize three', function()
    local actual = ocr.convert(
      ' _ \n' ..
      ' _|\n' ..
      ' _|\n' ..
      '   '
    )
    assert.equal('3', actual)
  end)

  it('should recognize four', function()
    local actual = ocr.convert(
      '   \n' ..
      '|_|\n' ..
      '  |\n' ..
      '   '
    )
    assert.equal('4', actual)
  end)

  it('should recognize five', function()
    local actual = ocr.convert(
      ' _ \n' ..
      '|_ \n' ..
      ' _|\n' ..
      '   '
    )
    assert.equal('5', actual)
  end)

  it('should recognize six', function()
    local actual = ocr.convert(
      ' _ \n' ..
      '|_ \n' ..
      '|_|\n' ..
      '   '
    )
    assert.equal('6', actual)
  end)

  it('should recognize seven', function()
    local actual = ocr.convert(
      ' _ \n' ..
      '  |\n' ..
      '  |\n' ..
      '   '
    )
    assert.equal('7', actual)
  end)

  it('should recognize eight', function()
    local actual = ocr.convert(
      ' _ \n' ..
      '|_|\n' ..
      '|_|\n' ..
      '   '
    )
    assert.equal('8', actual)
  end)

  it('should recognize nine', function()
    local actual = ocr.convert(
      ' _ \n' ..
      '|_|\n' ..
      ' _|\n' ..
      '   '
    )
    assert.equal('9', actual)
  end)

  it('should identify garble', function()
    local actual = ocr.convert(
      '   \n' ..
      '| |\n' ..
      '| |\n' ..
      '   '
    )
    assert.equal('?', actual)
  end)

  it('should recognize two numbers on the same line', function()
    local actual = ocr.convert(
      '    _ \n' ..
      '  || |\n' ..
      '  ||_|\n' ..
      '      '
    )
    assert.equal('10', actual)
  end)

  it('should recognize multiple numbers on the same line', function()
    local actual = ocr.convert(
      '    _  _     _  _  _  _  _  _ \n' ..
      '  | _| _||_||_ |_   ||_||_|| |\n' ..
      '  ||_  _|  | _||_|  ||_| _||_|\n' ..
      '                              '
    )
    assert.equal('1234567890', actual)
  end)

  it('should identify garble mixed into a line of numbers', function()
    local actual = ocr.convert(
      '       _     _           _ \n' ..
      '  |  || |  || |     || || |\n' ..
      '  |  | _|  ||_|  |  ||_||_|\n' ..
      '                           '
    )
    assert.equal('11?10?1?0', actual)
  end)

  it('should convert multiple lines of numbers as comma separated values', function()
    local actual = ocr.convert(
      '    _  _ \n' ..
      '  | _| _|\n' ..
      '  ||_  _|\n' ..
      '         \n' ..
      '    _  _ \n' ..
      '|_||_ |_ \n' ..
      '  | _||_|\n' ..
      '         \n' ..
      ' _  _  _ \n' ..
      '  ||_||_|\n' ..
      '  ||_| _|\n' ..
      '         '
    )
    assert.equal('123,456,789', actual)
  end)

  it('should generate error if input is incorrectly sized', function()
      assert.has_error(function() ocr.convert(
        ' _ \n' ..
        '||\n' ..
        '|_|\n' ..
        '   ') end)

      assert.has_error(function() ocr.convert(
        '     _ \n' ..
        '  | _| _|\n' ..
        '  |_  _|\n' ..
        '         \n') end)

      assert.has_error(function() ocr.convert(
        '    _  _ \n' ..
        '  | _| _|\n' ..
        '  ||_ \n' ..
        '         \n') end)
  end)
end)
local num_0 =
  ' _ \n' ..
  '| |\n' ..
  '|_|\n' ..
  '   '
local num_1 =
  '   \n' ..
  '  |\n' ..
  '  |\n' ..
  '   '
local num_2 =
  ' _ \n' ..
  ' _|\n' ..
  '|_ \n' ..
  '   '
local num_3 =
  ' _ \n' ..
  ' _|\n' ..
  ' _|\n' ..
  '   '
local num_4 =
  '   \n' ..
  '|_|\n' ..
  '  |\n' ..
  '   '
local num_5 =
  ' _ \n' ..
  '|_ \n' ..
  ' _|\n' ..
  '   '
local num_6 =
  ' _ \n' ..
  '|_ \n' ..
  '|_|\n' ..
  '   '
local num_7 =
  ' _ \n' ..
  '  |\n' ..
  '  |\n' ..
  '   '
local num_8 =
  ' _ \n' ..
  '|_|\n' ..
  '|_|\n' ..
  '   '
local num_9 =
  ' _ \n' ..
  '|_|\n' ..
  ' _|\n' ..
  '   '
local numbers = { num_1, num_2, num_3, num_4, num_5, num_6, num_7, num_8, num_9, num_0 }

local function convert_now(ocrTable)
  local result = ''
  for i=1, #ocrTable do
    local converted = '?'
    for j=1, #numbers do
      if ocrTable[i] == numbers[j] then
        converted = tostring(j%10)
        break
      end
    end
    result = result .. converted
  end
  return result
end

local function convert(s)
  local ocrTable = {}
  local row = 1
  local result = ''
  for v in s:gmatch('[^\n]+') do
    local n = 1
    for i=1, #v, 3 do
      ocrTable[n] = (ocrTable[n] or '') .. v:sub(i, i+2)
      if row<4 then ocrTable[n] = ocrTable[n] .. '\n' end
      n=n+1
    end
    if row == 4 then 
      if #result ~= 0 then result = result ..',' end
      result = result .. convert_now(ocrTable)
      row=1 ocrTable={}
    else row = row+1 end
  end
  return result
end

return {
  convert = convert
}

Community comments

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

Instead of doing a linear search through numbers, you could reorganize it as a hash and do a constant-time hash lookup. I guess this isn't a big deal since numbers only has 10 elements, though.

Avatar of ryanplusplus

Should convert_now be convert_row?

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?