-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_words.py
52 lines (43 loc) · 1.28 KB
/
calc_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Python program to show words that can be printed on a calcualtor
# by entering digits and turning the calculator upside down
#
# e.g. hello, boobies...
#
# To run, it needs a dictionary as input
# e.g.
# aspell -d fr dump master | aspell -l fr expand | unaccent ISO-8859-1 | python calc_words.py | sort
#
# If other languages are needed, change 'en' to another ISO code
# The 'expand' option is needed to expand variations of words
#
calculator_letters = {
'o': '0',
'l': '1',
'i': '1',
'z': '2',
'e': '3',
'h': '4',
's': '5',
'g': '6',
'l': '7',
'b': '8'
}
# can a word be encoded on a calculator?
def is_encodable(x):
s1 = set(x);
s2 = set(calculator_letters.keys());
return s1.issubset(s2)
# return the digits required to show the word (in reverse order)
def encode(x):
return x.translate(str.maketrans(''.join(calculator_letters.keys()), ''.join(calculator_letters.values())))[::-1]
def encode_and_print_it(x):
print("%s -> %s" % (x, encode(x)))
if __name__ == "__main__":
while True:
try:
line = input()
except EOFError:
break
for word in line.split():
if is_encodable(word):
encode_and_print_it(word)