Avatar of Tobi01

Tobi01's solution

to OCR Numbers in the Java Track

Published at Aug 20 2019 · 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"

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

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

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

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.

OpticalCharacterReaderTest.java

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class OpticalCharacterReaderTest {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void testReaderRecognizesSingle0() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "| |",
                "|_|",
                "   "
        ));

        assertEquals("0", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle1() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "   ",
                "  |",
                "  |",
                "   "
        ));

        assertEquals("1", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderReturnsQuestionMarkForUnreadableButCorrectlySizedInput() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "   ",
                "  _",
                "  |",
                "   "
        ));

        assertEquals("?", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderThrowsExceptionWhenNumberOfInputLinesIsNotAMultipleOf4() {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("Number of input rows must be a positive multiple of 4");

        new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "| |",
                "   "
        ));
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderThrowsExceptionWhenNumberOfInputColumnsIsNotAMultipleOf3() {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("Number of input columns must be a positive multiple of 3");

        new OpticalCharacterReader().parse(Arrays.asList(
                "    ",
                "   |",
                "   |",
                "    "
        ));
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesBinarySequence110101100() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "       _     _        _  _ ",
                "  |  || |  || |  |  || || |",
                "  |  ||_|  ||_|  |  ||_||_|",
                "                           "
        ));

        assertEquals("110101100", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderReplacesUnreadableDigitsWithQuestionMarksWithinSequence() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "       _     _           _ ",
                "  |  || |  || |     || || |",
                "  |  | _|  ||_|  |  ||_||_|",
                "                           "
        ));

        assertEquals("11?10?1?0", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle2() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                " _|",
                "|_ ",
                "   "
        ));

        assertEquals("2", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle3() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                " _|",
                " _|",
                "   "
        ));

        assertEquals("3", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle4() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "   ",
                "|_|",
                "  |",
                "   "
        ));

        assertEquals("4", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle5() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_ ",
                " _|",
                "   "
        ));

        assertEquals("5", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle6() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_ ",
                "|_|",
                "   "
        ));

        assertEquals("6", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle7() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "  |",
                "  |",
                "   "
        ));

        assertEquals("7", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle8() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_|",
                "|_|",
                "   "
        ));

        assertEquals("8", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle9() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_|",
                " _|",
                "   "
        ));

        assertEquals("9", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSequence1234567890() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "    _  _     _  _  _  _  _  _ ",
                "  | _| _||_||_ |_   ||_||_|| |",
                "  ||_  _|  | _||_|  ||_| _||_|",
                "                              "
        ));

        assertEquals("1234567890", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesAndCorrectlyFormatsMultiRowInput() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "    _  _ ",
                "  | _| _|",
                "  ||_  _|",
                "         ",
                "    _  _ ",
                "|_||_ |_ ",
                "  | _||_|",
                "         ",
                " _  _  _ ",
                "  ||_||_|",
                "  ||_| _|",
                "         "
        ));

        assertEquals("123,456,789", parsedInput);
    }

}
import java.util.LinkedList;
import java.util.List;

public class OpticalCharacterReader {

    private String values = "";
    private String helper = "";
    private String verarbeitung = "";
    private char[] daten;
    private char[] help;
    private int size = 0;
    private boolean blocked = false;
    private boolean segment1 = false;
    private boolean segment2 = false;
    private boolean segment3 = false;
    private boolean segment4 = false;
    private boolean segment5 = false;
    private boolean segment6 = false;
    private boolean segment7 = false;
    private boolean mehralseinezahlenreihe = false;

public String parse (List<String> Input) throws IllegalArgumentException
{
    if(Input.size()<4)
    {
        throw new IllegalArgumentException("Number of input rows must be a positive multiple of 4");
    }

    int counter = 0;
    int anzahlderzahlen =0;
    int anzahlderzeichenprozeile=0;
    int anzahlderzahlenprozeile =0;
    if(Input.size()==4) {
        for (int i = 0; i < Input.size(); i++) {
            verarbeitung += Input.get(i);
            if (blocked == false) {
                size = verarbeitung.length();
                blocked = true;
            }
        }

        daten = verarbeitung.toCharArray();

        anzahlderzahlen = (daten.length/4)/3;
        if(((daten.length/4))%3 != 0)
        {
            throw new IllegalArgumentException("Number of input columns must be a positive multiple of 3");
        }
        anzahlderzeichenprozeile = (daten.length/4);
    }else
    {
        for (int i = 0; i < Input.size(); i++) {
            verarbeitung += Input.get(i);
            if (blocked == false) {
                size = verarbeitung.length();
                blocked = true;
            }
        }

        daten = verarbeitung.toCharArray();
        System.out.println(daten);


        if(((daten.length/Input.size()))%3 != 0)
        {
            throw new IllegalArgumentException("Number of input columns must be a positive multiple of 3");
        }
        anzahlderzeichenprozeile = (daten.length/Input.size());
        anzahlderzahlen = daten.length/Input.size();
        anzahlderzahlenprozeile = (daten.length/Input.size())/3;

        mehralseinezahlenreihe = true;

    }

    counter =0;
    int counter2 = 0;
    int leerzeile= 0;
    for(int q = 0; q < anzahlderzahlen; q++) {

        if(mehralseinezahlenreihe == true && q >= 3 && q < 6)
        {
            leerzeile = anzahlderzeichenprozeile*2;
        }

        if(mehralseinezahlenreihe == true && q >= 6)
        {
            leerzeile = anzahlderzeichenprozeile*4;
        }

        if (daten[1+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '_') {
            segment1 = true;
        }
        if (daten[anzahlderzeichenprozeile+0+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '|') {
            segment2 = true;
        }
        if (daten[anzahlderzeichenprozeile+1+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '_') {
            segment3 = true;
        }
        if (daten[anzahlderzeichenprozeile+2+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '|') {
            segment4 = true;
        }
        if (daten[(anzahlderzeichenprozeile*2)+0+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '|') {
            segment5 = true;
        }
        if (daten[(anzahlderzeichenprozeile*2)+1+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '_') {
            segment6 = true;
        }
        if (daten[(anzahlderzeichenprozeile*2)+2+(q*3)+counter2*anzahlderzeichenprozeile+leerzeile] == '|') {
            segment7 = true;
        }



        values += whichnumber();

        if(anzahlderzahlen>anzahlderzahlenprozeile)
        {
            counter++;
            if (counter==anzahlderzahlenprozeile)
            {
                if(q!=anzahlderzahlen-1) {
                    values += ",";
                }
                counter = 0;
                counter2++;
            }
        }

        System.out.println(values);
        segment1 = false;
        segment2 = false;
        segment3 = false;
        segment4 = false;
        segment5 = false;
        segment6 = false;
        segment7 = false;


    }


    System.out.println(daten);
    return values;
}

public String whichnumber()
{
    if(segment1 == true && segment2 == true && segment3 == false && segment4 == true && segment5 == true && segment6 == true && segment7 == true)
    {
        return "0";
    }

    if(segment1 == false && segment2 == false && segment3 ==false && segment4 == true && segment5 == false && segment6 == false && segment7 == true)
    {
        return "1";
    }

    if(segment1 == true && segment2 == false && segment3 ==true && segment4 == true && segment5 == true && segment6 == true && segment7 == false)
    {
        return "2";
    }

    if(segment1 == true && segment2 == false && segment3 ==true && segment4 == true && segment5 == false && segment6 == true && segment7 == true)
    {
        return "3";
    }

    if(segment1 == false && segment2 == true && segment3 ==true && segment4 == true && segment5 == false && segment6 == false && segment7 == true)
    {
        return "4";
    }

    if(segment1 == true && segment2 == true && segment3 ==true && segment4 == false && segment5 == false && segment6 == true && segment7 == true)
    {
        return "5";
    }

    if(segment1 == true && segment2 == true && segment3 ==true && segment4 == false && segment5 == true && segment6 == true && segment7 == true)
    {
        return "6";
    }

    if(segment1 == true && segment2 == false && segment3 ==false && segment4 == true && segment5 == false && segment6 == false && segment7 == true)
    {
        return "7";
    }

    if(segment1 == true && segment2 == true && segment3 == true && segment4 == true && segment5 == true && segment6 == true && segment7 == true)
    {
        return "8";
    }

    if(segment1 == true && segment2 == true && segment3 == true && segment4 == true && segment5 == false && segment6 == true && segment7 == true)
    {
        return "9";
    }


    return "?";
}


}

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?