Created
December 20, 2012 15:25
-
-
Save cliffxuan/4345928 to your computer and use it in GitHub Desktop.
Python solution to Google Codejam: http://code.google.com/codejam/contest/351101/dashboard#s=p2
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 | |
import sys | |
code_map = { | |
'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 output(row): | |
output = [] | |
prev_char = '' | |
for char in row: | |
if prev_char and code_map[char][0] == code_map[prev_char][0]: | |
output.append(' ') | |
output.append(code_map[char]) | |
prev_char = char | |
return ''.join(output) | |
def main(): | |
file_name = sys.argv[1] | |
input = open(file_name) | |
cnt = 0 | |
for line in input: | |
cnt += 1 | |
if cnt > 1: | |
print 'Case #%s: %s' %(cnt - 1, output(line.strip('\n'))) | |
class Test(unittest.TestCase): | |
def test_normal(self): | |
self.assertEqual(output('hi'), '44 444') | |
self.assertEqual(output(' '), '0') | |
self.assertEqual(output('yes'), '999337777') | |
self.assertEqual(output('foo bar'), '333666 6660 022 2777') | |
self.assertEqual(output('hello world'), '4433555 555666096667775553') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment