@@ -23,28 +23,33 @@ def build_encmap(map) :
23
23
dict [map [i ]] = "" + col * "." + " " + row * "."
24
24
i += 1
25
25
dict ['k' ] = dict ['c' ]
26
- dict [' ' ] = ''
27
26
return dict
28
27
29
28
def encode_tap (text , errors = 'strict' ) :
30
29
map = 'abcdefghijlmnopqrstuvwxyz'
31
30
ENCMAP = build_encmap (map )
32
31
encoded = ""
33
32
for i , letter in enumerate (text ) :
34
- encoded += ENCMAP [letter .lower ()]
33
+ try :
34
+ encoded += ENCMAP [letter .lower ()]
35
+ except KeyError :
36
+ pass
35
37
if i != len (text ) - 1 and letter != ' ' :
36
38
encoded += ' '
37
39
return encoded , len (text )
38
40
39
41
40
- def decode_tap (text , errors = 'strict ' ) :
42
+ def decode_tap (text , errors = 'ignore ' ) :
41
43
map = 'abcdefghijlmnopqrstuvwxyz'
42
44
ENCMAP = build_encmap (map )
43
45
decoded = ""
44
46
for elem in text .split (" " ) :
45
- decoded += next (key for key , value in ENCMAP .items () if value == elem )
47
+ try :
48
+ decoded += next (key for key , value in ENCMAP .items () if value == elem )
49
+ except StopIteration :
50
+ print ("Invalid character(s) in the input. This is what could be decoded :" )
46
51
return decoded , len (text )
47
52
48
53
49
- add ("tap" , encode_tap , decode_tap , ignore_case = "encode " )
54
+ add ("tap" , encode_tap , decode_tap , ignore_case = "both " )
50
55
0 commit comments