Google Code Jam

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

Recent Entries

  • New York City

    I spent last weekend in New York City. It was my second time in New York but the first time I really got to see...

  • Ctrl, Alt, Delete

    My life has been crazy and it looks like the weeks will only get even more hectic. I always measure how busy I should be...

  • Perspective

    Despite the lack of activity on this blog for so long, I was surprised to learn that I still am getting more traffic than before....

  • Back!

    Sorry. I was gone for a long time. Blogging is a paradox. You can never be doing interesting things and blogging at the same time....

  • Good Service

    It was my girlfriend's birthday today and to celebrate, we went out to eat at a nice restaurant in Sacramento. Sacramento had a wine-and-dine week...

Close