2
2
3
3
def splitDigits (code ):
4
4
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 )))
6
7
numChars = max ([len (l ) for l in lines ])
8
+ # assure that all lines have the same amount of characters
7
9
def adjustLine (l ):
8
10
return l + ' ' * max (numChars - len (l ), 0 );
9
11
lines = [adjustLine (l ) for l in lines ]
@@ -23,17 +25,39 @@ def adjustLine(l):
23
25
#create a dict that maps each digit in string representation to its number (also str to keep leading 0)
24
26
__digitMap = dict ([(d ,str (i )) for i ,d in enumerate (splitDigits (__numbers ))])
25
27
def convertDigit (digit ):
26
- return __digitMap [digit ]
28
+ try :
29
+ return __digitMap [digit ]
30
+ except KeyError :
31
+ return '?'
27
32
28
33
def convertDigits (digits ):
29
34
for d in splitDigits (digits ):
30
35
yield convertDigit (d )
31
36
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
+
32
48
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
+
34
56
if (actual != expected ):
35
57
print (input )
36
- print ("{0} ({1})" .format (actual , expected ))
58
+ print ("Got: {0}\n Expected: {1}) " .format (actual , expected ))
59
+ else :
60
+ print (actual )
37
61
38
62
# for k,v in __digitMap.items():
39
63
# print(k)
0 commit comments