Updating all of the new ones that are done.
This commit is contained in:
96
go/ocr-numbers/README.md
Normal file
96
go/ocr-numbers/README.md
Normal file
@@ -0,0 +1,96 @@
|
||||
# Ocr Numbers
|
||||
|
||||
Write a program that, given a 3 x 4 grid of pipes, underscores, and spaces, can 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"
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://exercism.io/languages/go).
|
||||
|
||||
## Source
|
||||
|
||||
Inspired by the Bank OCR kata [http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR](http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR)
|
||||
|
||||
## Submitting Incomplete Problems
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
65
go/ocr-numbers/ocr.go
Normal file
65
go/ocr-numbers/ocr.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package ocr
|
||||
|
||||
import "strings"
|
||||
|
||||
// recognizeDigit takes a 3x4 grid of pipes, underscores, and spaces
|
||||
// and determines the digit represented.
|
||||
func recognizeDigit(in string) string {
|
||||
switch in {
|
||||
case "\n _ \n| |\n|_|\n ":
|
||||
return "0"
|
||||
case "\n \n |\n |\n ":
|
||||
return "1"
|
||||
case "\n _ \n _|\n|_ \n ":
|
||||
return "2"
|
||||
case "\n _ \n _|\n _|\n ":
|
||||
return "3"
|
||||
case "\n \n|_|\n |\n ":
|
||||
return "4"
|
||||
case "\n _ \n|_ \n _|\n ":
|
||||
return "5"
|
||||
case "\n _ \n|_ \n|_|\n ":
|
||||
return "6"
|
||||
case "\n _ \n |\n |\n ":
|
||||
return "7"
|
||||
case "\n _ \n|_|\n|_|\n ":
|
||||
return "8"
|
||||
case "\n _ \n|_|\n _|\n ":
|
||||
return "9"
|
||||
}
|
||||
return "?"
|
||||
}
|
||||
|
||||
// parseChar takes 3 lines and a number and returns
|
||||
// the string value of that character
|
||||
func parseChar(lines []string, num int) string {
|
||||
startIdx := 3 * num
|
||||
lenNeed := startIdx + 3
|
||||
if len(lines[0]) < lenNeed || len(lines[1]) < lenNeed {
|
||||
return ""
|
||||
}
|
||||
char := "\n" + lines[0][(startIdx+0):(startIdx+3)]
|
||||
char += "\n" + lines[1][(startIdx+0):(startIdx+3)]
|
||||
char += "\n" + lines[2][(startIdx+0):(startIdx+3)]
|
||||
char += "\n" + lines[3][(startIdx+0):(startIdx+3)]
|
||||
return recognizeDigit(char)
|
||||
}
|
||||
|
||||
// Recognize takes a string, splits it up by \n and
|
||||
// then sends 4 line chunks and calls parseChar on it
|
||||
func Recognize(in string) []string {
|
||||
var ret []string
|
||||
lines := strings.Split(in, "\n")
|
||||
numRet := len(lines) / 4
|
||||
for i := 0; i < numRet; i++ {
|
||||
var retString string
|
||||
startIdx := (i * 4) + 1
|
||||
procLines := lines[startIdx : startIdx+4]
|
||||
numChars := len(lines[startIdx]) / 3
|
||||
for j := 0; j < numChars; j++ {
|
||||
retString += parseChar(procLines, j)
|
||||
}
|
||||
ret = append(ret, retString)
|
||||
}
|
||||
return ret
|
||||
}
|
130
go/ocr-numbers/ocr_numbers_test.go
Normal file
130
go/ocr-numbers/ocr_numbers_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
// Go requirements:
|
||||
//
|
||||
// Define a function recognizeDigit as README Step 1 except make it recognize
|
||||
// all ten digits 0 to 9. Pick what you like for parameters and return values
|
||||
// but make it useful as a subroutine for README step 2.
|
||||
//
|
||||
// For README Step 2 define,
|
||||
//
|
||||
// func Recognize(string) []string
|
||||
//
|
||||
// and implement it using recognizeDigit.
|
||||
//
|
||||
// Input strings tested here have a \n at the beginning of each line and
|
||||
// no trailing \n on the last line. (This makes for readable raw string
|
||||
// literals.)
|
||||
//
|
||||
// For bonus points, gracefully handle misformatted data. What should you
|
||||
// do with a partial cell? Discard it? Pad with spaces? Report it with a
|
||||
// "?" character? What should you do if the first character is not \n?
|
||||
|
||||
package ocr
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var tests = []struct {
|
||||
in string
|
||||
out []string
|
||||
}{
|
||||
{`
|
||||
_
|
||||
| |
|
||||
|_|
|
||||
`, []string{"0"}},
|
||||
{`
|
||||
|
||||
|
|
||||
|
|
||||
`, []string{"1"}},
|
||||
{`
|
||||
_
|
||||
_|
|
||||
|_
|
||||
`, []string{"2"}},
|
||||
{`
|
||||
_
|
||||
_|
|
||||
_|
|
||||
`, []string{"3"}},
|
||||
{`
|
||||
|
||||
|_|
|
||||
|
|
||||
`, []string{"4"}},
|
||||
{`
|
||||
_
|
||||
|_
|
||||
_|
|
||||
`, []string{"5"}},
|
||||
{`
|
||||
_
|
||||
|_
|
||||
|_|
|
||||
`, []string{"6"}},
|
||||
{`
|
||||
_
|
||||
|
|
||||
|
|
||||
`, []string{"7"}},
|
||||
{`
|
||||
_
|
||||
|_|
|
||||
|_|
|
||||
`, []string{"8"}},
|
||||
{`
|
||||
_
|
||||
|_|
|
||||
_|
|
||||
`, []string{"9"}},
|
||||
{`
|
||||
_
|
||||
|| |
|
||||
||_|
|
||||
`, []string{"10"}},
|
||||
{`
|
||||
|
||||
| |
|
||||
| |
|
||||
`, []string{"?"}},
|
||||
{`
|
||||
_ _ _ _
|
||||
| || | || | | || || |
|
||||
| ||_| ||_| | ||_||_|
|
||||
`, []string{"110101100"}},
|
||||
{`
|
||||
_ _ _
|
||||
| || | || | || || |
|
||||
| | _| ||_| | ||_||_|
|
||||
`, []string{"11?10?1?0"}},
|
||||
{`
|
||||
_ _ _ _ _ _ _ _
|
||||
| _| _||_||_ |_ ||_||_|| |
|
||||
||_ _| | _||_| ||_| _||_|
|
||||
`, []string{"1234567890"}},
|
||||
{`
|
||||
_ _
|
||||
| _| _|
|
||||
||_ _|
|
||||
|
||||
_ _
|
||||
|_||_ |_
|
||||
| _||_|
|
||||
|
||||
_ _ _
|
||||
||_||_|
|
||||
||_| _|
|
||||
`, []string{"123", "456", "789"}},
|
||||
}
|
||||
|
||||
var _ = recognizeDigit // step 1.
|
||||
|
||||
func TestRecognize(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
if res := Recognize(test.in); !reflect.DeepEqual(res, test.out) {
|
||||
t.Fatalf("Recognize(`%s`) = %q, want %q.", test.in, res, test.out)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user