I never got into things like TopCoder enough. I told myself I would this past year but lo’ and behold, it never happened. This summer, I’m going to actually try to participate in the Google Code Jam. It is unlikely that I’ll make it far, but it should still be fun!
Here was my solution to the first sample problem. This is actually just an excuse for me to test out syntax highlighting:
import sys
import fileinput
def eat(lang):
base = len(lang)
digits = {}
chars = {}
i = 0
for c in lang:
digits[c] = i
chars[i] = c
i = i + 1
return (base, digits, chars)
def parse(alien, source):
(base, digits, chars) = eat(source)
num = 0
s = len(alien)
for i in range(0, s):
num += (base ** (s-i-1)) * digits[alien[i]]
return num
def translate(num, target):
(base, digits, chars) = eat(target)
ret = ""
while num != 0:
rem = num % base
num = int(num / base)
ret = str(chars[rem]) + ret
return ret.lstrip(chars[0])
if __name__ == '__main__':
lines = fileinput.input(sys.argv[1])
num = int(lines.readline())
for i in range(1, num+1):
line = lines.readline()
(alien, source, target) = line.split()
num = parse(alien, source)
out = translate(num, target)
print 'Case #%d: %s' % (i, out)
For now, I’m just using Code Colorizer for the highlighting which is based on pygments for Python. If it becomes a problem, shouldn’t be too hard to do the highlighting locally. Hooray for Web2.0.
Leave a comment