Sha256: 8d79f98a56c1f5202c0083fce214f76ca93cc0f27c1e5e7a4e3e1ca032998ccb

Contents?: true

Size: 1.02 KB

Versions: 229

Compression:

Stored size: 1.02 KB

Contents

package ocr

import "strings"

var digit = map[string]byte{
	" _ | ||_|   ": '0',
	"     |  |   ": '1',
	" _  _||_    ": '2',
	" _  _| _|   ": '3',
	"   |_|  |   ": '4',
	" _ |_  _|   ": '5',
	" _ |_ |_|   ": '6',
	" _   |  |   ": '7',
	" _ |_||_|   ": '8',
	" _ |_| _|   ": '9',
}

func recognizeDigit(lines []string, start int) byte {
	if len(lines) < 4 {
		return '?'
	}
	need := start + 3
	for _, l := range lines {
		if len(l) < need {
			return '?'
		}
	}
	if d, ok := digit[lines[0][start:need]+
		lines[1][start:need]+
		lines[2][start:need]+
		lines[3][start:need]]; ok {
		return d
	}
	return '?'
}

func Recognize(s string) []string {
	lines := strings.Split(s, "\n")
	if len(lines[0]) == 0 {
		lines = lines[1:]
	}
	r := make([]string, (len(lines)+3)/4)
	for i := range r {
		max := 0
		for l := 0; l < 4 && l < len(lines); l++ {
			if n := len(lines[l]); n > max {
				max = n
			}
		}
		b := make([]byte, (max+2)/3)
		for j := range b {
			b[j] = recognizeDigit(lines, j*3)
		}
		r[i] = string(b)
		lines = lines[4:]
	}
	return r
}

Version data entries

229 entries across 229 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.179 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.178 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.177 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.176 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.175 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.174 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.173 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.172 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.171 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.170 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.169 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.167 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.166 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.165 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.164 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.163 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.162 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.161 tracks/go/exercises/ocr-numbers/example.go
trackler-2.2.1.160 tracks/go/exercises/ocr-numbers/example.go