 # crane-yuan's solution

## to OCR Numbers in the Java Track

Published at Jul 30 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.

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

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

@Test
" _ ",
"| |",
"|_|",
"   "
));

assertEquals("0", parsedInput);
}

@Ignore("Remove to run test")
@Test
"   ",
"  |",
"  |",
"   "
));

assertEquals("1", parsedInput);
}

@Ignore("Remove to run test")
@Test
"   ",
"  _",
"  |",
"   "
));

assertEquals("?", parsedInput);
}

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

" _ ",
"| |",
"   "
));
}

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

"    ",
"   |",
"   |",
"    "
));
}

@Ignore("Remove to run test")
@Test
"       _     _        _  _ ",
"  |  || |  || |  |  || || |",
"  |  ||_|  ||_|  |  ||_||_|",
"                           "
));

assertEquals("110101100", parsedInput);
}

@Ignore("Remove to run test")
@Test
"       _     _           _ ",
"  |  || |  || |     || || |",
"  |  | _|  ||_|  |  ||_||_|",
"                           "
));

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

@Ignore("Remove to run test")
@Test
" _ ",
" _|",
"|_ ",
"   "
));

assertEquals("2", parsedInput);
}

@Ignore("Remove to run test")
@Test
" _ ",
" _|",
" _|",
"   "
));

assertEquals("3", parsedInput);
}

@Ignore("Remove to run test")
@Test
"   ",
"|_|",
"  |",
"   "
));

assertEquals("4", parsedInput);
}

@Ignore("Remove to run test")
@Test
" _ ",
"|_ ",
" _|",
"   "
));

assertEquals("5", parsedInput);
}

@Ignore("Remove to run test")
@Test
" _ ",
"|_ ",
"|_|",
"   "
));

assertEquals("6", parsedInput);
}

@Ignore("Remove to run test")
@Test
" _ ",
"  |",
"  |",
"   "
));

assertEquals("7", parsedInput);
}

@Ignore("Remove to run test")
@Test
" _ ",
"|_|",
"|_|",
"   "
));

assertEquals("8", parsedInput);
}

@Ignore("Remove to run test")
@Test
" _ ",
"|_|",
" _|",
"   "
));

assertEquals("9", parsedInput);
}

@Ignore("Remove to run test")
@Test
"    _  _     _  _  _  _  _  _ ",
"  | _| _||_||_ |_   ||_||_|| |",
"  ||_  _|  | _||_|  ||_| _||_|",
"                              "
));

assertEquals("1234567890", parsedInput);
}

@Ignore("Remove to run test")
@Test
"    _  _ ",
"  | _| _|",
"  ||_  _|",
"         ",
"    _  _ ",
"|_||_ |_ ",
"  | _||_|",
"         ",
" _  _  _ ",
"  ||_||_|",
"  ||_| _|",
"         "
));

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

}``````
``````import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public String parse(List<String> list) {

int row = list.size();
int column = list.get(0)
.length();
if (row % 4 != 0) {
throw new IllegalArgumentException("Number of input rows must be a positive multiple of 4");
}
if (column % 3 != 0) {
throw new IllegalArgumentException("Number of input columns must be a positive multiple of 3");
}

List<List<String>> matrix = new ArrayList<>();
for (int i = 0; i < row; i += 4) {
List<String> temp = new ArrayList<>();
for (int j = 0; j < 4; j++) {
}
}

List<String> result = new ArrayList<>();

for (List<String> line : matrix) {
StringBuilder sb = new StringBuilder();
List<List<String>> nums = splitColumn(line);
for (List<String> num : nums) {
sb.append(convertOCR(num));
}
}
return String.join(",", result);
}

private static String convertOCR(List<String> singleNumList) {
// @formatter:off
List<String> numList =Arrays.asList(
"    _  _     _  _  _  _  _  _ ",
"  | _| _||_||_ |_   ||_||_|| |",
"  ||_  _|  | _||_|  ||_| _||_|",
"                              "
);
// @formatter:on

List<List<String>> matrix = splitColumn(numList);
for (int i = 0; i < matrix.size(); i++) {
List<String> num = matrix.get(i);
if (checkListString(num, singleNumList)) {
return String.valueOf((i + 1) % 10);
}
}
return "?";
}

private static List<List<String>> splitColumn(List<String> list) {
int column = list.get(0)
.length();

List<List<String>> matrix = new ArrayList<>();
for (int i = 0; i < column; i += 3) {
List<String> temp = new ArrayList<>();
for (int j = 0; j < 4; j++) {
.substring(i, i + 3));
}
}
return matrix;
}

private static boolean checkListString(List<String> list1, List<String> list2) {
return list1.equals(list2);
}

}``````