Created
March 14, 2011 10:06
-
-
Save ohe/868970 to your computer and use it in GitHub Desktop.
Une implémentation possible du problèmes KataBankOCR (voir codingdojo.org)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
ALL_0 = """\ | |
_ _ _ _ _ _ _ _ _ | |
| || || || || || || || || | | |
|_||_||_||_||_||_||_||_||_| | |
""" | |
ALL_1 = """\ | |
| | | | | | | | | | |
| | | | | | | | | | |
""" | |
ALL_2 = """\ | |
_ _ _ _ _ _ _ _ _ | |
_| _| _| _| _| _| _| _| _| | |
|_ |_ |_ |_ |_ |_ |_ |_ |_ | |
""" | |
ALL_3 = """\ | |
_ _ _ _ _ _ _ _ _ | |
_| _| _| _| _| _| _| _| _| | |
_| _| _| _| _| _| _| _| _| | |
""" | |
ALL_4 = """\ | |
|_||_||_||_||_||_||_||_||_| | |
| | | | | | | | | | |
""" | |
ALL_5 = """\ | |
_ _ _ _ _ _ _ _ _ | |
|_ |_ |_ |_ |_ |_ |_ |_ |_ | |
_| _| _| _| _| _| _| _| _| | |
""" | |
ALL_6 = """\ | |
_ _ _ _ _ _ _ _ _ | |
|_ |_ |_ |_ |_ |_ |_ |_ |_ | |
|_||_||_||_||_||_||_||_||_| | |
""" | |
ALL_7 = """\ | |
_ _ _ _ _ _ _ _ _ | |
| | | | | | | | | | |
| | | | | | | | | | |
""" | |
ALL_8 = """\ | |
_ _ _ _ _ _ _ _ _ | |
|_||_||_||_||_||_||_||_||_| | |
|_||_||_||_||_||_||_||_||_| | |
""" | |
ALL_9 = """\ | |
_ _ _ _ _ _ _ _ _ | |
|_||_||_||_||_||_||_||_||_| | |
_| _| _| _| _| _| _| _| _| | |
""" | |
ALL_DIFF= """\ | |
_ _ _ _ _ _ _ | |
| _| _||_||_ |_ ||_||_| | |
||_ _| | _||_| ||_| _| | |
""" | |
ERROR = """\ | |
_ _ _ _ _ _ _ | |
| _| _| _||_ |_ ||_||_| | |
||_ _| | _||_| ||_| _| | |
""" | |
ID_ACCOUNTS = [ALL_0, ALL_1, ALL_2, ALL_3, ALL_4, ALL_5, ALL_6, | |
ALL_7, ALL_8, ALL_0] | |
class test_id(unittest.TestCase): | |
def test_id_0(self): | |
self.assertTrue(convert(ALL_0), "0000000000") | |
def test_id_1(self): | |
self.assertTrue(convert(ALL_1), "111111111") | |
def test_id_all(self): | |
RESULTS= [9*str(e) for e in range(10)] | |
for account, result in zip(ID_ACCOUNTS, RESULTS): | |
self.assertTrue(convert(account), result) | |
class test_diff(unittest.TestCase): | |
def test_all_diff(self): | |
self.assertTrue(convert(ALL_DIFF), "123456789") | |
class test_error(unittest.TestCase): | |
def test_error(self): | |
self.assertEqual(convert(ERROR), "123?56789") | |
######################### MAIN CODE ############################### | |
ZERO = """\ | |
_ | |
| | | |
|_| | |
""" | |
ONE = """\ | |
| | |
| | |
""" | |
TWO = """\ | |
_ | |
_| | |
|_ | |
""" | |
THREE = """\ | |
_ | |
_| | |
_| | |
""" | |
FOUR = """\ | |
|_| | |
| | |
""" | |
FIVE = """\ | |
_ | |
|_ | |
_| | |
""" | |
SIX = """\ | |
_ | |
|_ | |
|_| | |
""" | |
SEVEN = """\ | |
_ | |
| | |
| | |
""" | |
EIGHT = """\ | |
_ | |
|_| | |
|_| | |
""" | |
NINE = """\ | |
_ | |
|_| | |
_| | |
""" | |
NUMBERS = {ONE: "1", | |
TWO: "2", | |
THREE: "3", | |
FOUR: "4", | |
FIVE: "5", | |
SIX: "6", | |
SEVEN: "7", | |
EIGHT: "8", | |
NINE: "9", | |
ZERO: "0"} | |
def convert(input): | |
output = "" | |
for idx in range(0,27,3): | |
number = '\n'.join( [e[idx:idx+3] for e in input.split('\n')]) | |
if number in NUMBERS.keys(): | |
output += NUMBERS[number] | |
else: | |
output += '?' | |
return output | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment