Created
March 13, 2014 12:14
-
-
Save Nagasaki45/9527289 to your computer and use it in GitHub Desktop.
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
t9 = {'a': '2', | |
'b': '22', | |
'c': '222', | |
'd': '3', | |
'e': '33', | |
'f': '333', | |
'g': '4', | |
'h': '44', | |
'i': '444', | |
'j': '5', | |
'k': '55', | |
'l': '555', | |
'm': '6', | |
'n': '66', | |
'o': '666', | |
'p': '7', | |
'q': '77', | |
'r': '777', | |
's': '7777', | |
't': '8', | |
'u': '88', | |
'v': '888', | |
'w': '9', | |
'x': '99', | |
'y': '999', | |
'z': '9999', | |
' ': '0'} | |
def text_to_t9(s): | |
translated = [t9[s[0]]] | |
for char in s[1:]: | |
if t9[char].startswith(translated[-1][0]): | |
translated.append(' ') | |
translated.append(t9[char]) | |
return ''.join(translated) | |
assert '44 444' == text_to_t9('hi') | |
assert '999337777' == text_to_t9('yes') | |
assert '333666 6660 022 2777' == text_to_t9('foo bar') | |
assert '4433555 555666096667775553' == text_to_t9('hello world') | |
import sys | |
input_file = sys.argv[1] | |
with open(input_file) as f: | |
lines = f.read().splitlines() | |
cases = int(lines[0]) | |
if len(lines[1:]) != cases: | |
raise Exception('Number of cases different from number of lines') | |
for i, l in enumerate(lines[1:]): | |
print('Case #{}: {}'.format(i + 1, text_to_t9(l))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment