Skip to content

Commit 88987ad

Browse files
author
mueller
committed
Added checksum computation
Added some checks
1 parent 5bacaa3 commit 88987ad

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

Diff for: BankOCR.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
def splitDigits(code):
44
lines = [l for l in code.split('\n')]
5-
# assure that all lines have the same amount of characters
5+
if len(lines) < 3:
6+
raise ValueError("Invalid number of lines ({0})".format(len(lines)))
67
numChars = max([len(l) for l in lines])
8+
# assure that all lines have the same amount of characters
79
def adjustLine(l):
810
return l + ' ' * max(numChars-len(l), 0);
911
lines = [adjustLine(l) for l in lines]
@@ -23,17 +25,39 @@ def adjustLine(l):
2325
#create a dict that maps each digit in string representation to its number (also str to keep leading 0)
2426
__digitMap = dict([(d,str(i)) for i,d in enumerate(splitDigits(__numbers))])
2527
def convertDigit(digit):
26-
return __digitMap[digit]
28+
try:
29+
return __digitMap[digit]
30+
except KeyError:
31+
return '?'
2732

2833
def convertDigits(digits):
2934
for d in splitDigits(digits):
3035
yield convertDigit(d)
3136

37+
def checksum(number):
38+
if len(number) == 9:
39+
sum = 0
40+
for i, n in enumerate(number):
41+
try:
42+
sum += int(n) * (9-i)
43+
except ValueError:
44+
return False;
45+
return sum % 11 == 0
46+
return False;
47+
3248
def test(input, expected):
33-
actual = ''.join(convertDigits(input))
49+
number = ''.join(convertDigits(input))
50+
actual = number
51+
if '?' in actual:
52+
actual += ' ILL'
53+
# elif not checksum(actual):
54+
# actual += ' ERR'
55+
3456
if (actual != expected):
3557
print(input)
36-
print("{0} ({1})".format(actual, expected))
58+
print("Got: {0}\n Expected: {1}) ".format(actual, expected))
59+
else:
60+
print(actual)
3761

3862
# for k,v in __digitMap.items():
3963
# print(k)

Diff for: BankOCR_Test.txt

+12
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@
4242
| _| _||_||_ |_ ||_||_|
4343
||_ _| | _||_| ||_| _|
4444
123456789;
45+
_ _ _ _ _ _ _ _
46+
| || || || || || || ||_ |
47+
|_||_||_||_||_||_||_| _| |
48+
000000051;
49+
_ _ _ _ _ _ _
50+
|_||_|| || ||_ | | | _
51+
| _||_||_||_| | | | _|
52+
49006771? ILL;
53+
_ _ _ _ _ _ _
54+
| _| _||_| _ |_ ||_||_|
55+
||_ _| | _||_| ||_| _
56+
1234?678? ILL;

0 commit comments

Comments
 (0)