Google Code Jam: Alien Numbers Algorithm

27Jun08

Here I’m posting the source code written in Python for the Google Code Jam Practice Problems.
The tasks is available after logging at google – in: http://code.google.com/codejam/contest/ in Practice Problems link.

I don’t consider this code to be optimal or the quickest, so I’m open for your suggestions and remarks, what could have been done better

from math import pow
print "enter the filename: "
pliczek = str(raw_input())

pierwsza=1
f=open(pliczek, 'r' )
g=open("result " + pliczek, 'w' )

for data in f:
    if (pierwsza==1):
        pierwsza=0
        ile = 1
    else:
        data = data.split(' ' )
        base0 = len(str(data[1]))
        base1 = len(str(data[2].strip()))
        dlugosc = len(str(data[0]))

        liczba = 0
        for i in str(data[0]):
            liczba += data[1].index(i) * pow(base0,dlugosc - 1)
            dlugosc-=1
        liczba = int(liczba)

        nowaliczba = ""
        while liczba>0:
            nowaliczba = str(data[2].strip())[liczba%base1] + nowaliczba
            liczba = liczba/base1

        g.write('Case #'+str(ile) + ': '+nowaliczba + '\n' )
        ile+=1
f.close()
g.close()

by the way, I noticed some little bug in WordPress – it’s changing “‘)” to a smiley even when it’s written as a source code



10 Responses to “Google Code Jam: Alien Numbers Algorithm”

  1. 1 ecik

    Here’s my code: http://wklej.org/id/b54aa70be1😉

  2. Mind posting a link to the practice problems? It might help with understanding the code🙂

  3. has any one got the output file??

  4. 4 masteranza

    well this algorithm makes it if only you have the input file, which you can obtain on google code jam ->practice problems->alien numbers

  5. 5 Sumit

    Nice solution

  6. 6 Prateek

    Hey Can one explain the logic. How to decode the alien from Source to Target

    Thanks

  7. 7 C. Chu

    It’s pretty simple. Note that the alien languages are simply symbol substitutions for Arabic numbers. So in reality the problem is just converting between bases, and using a symbol lookup table to translate from Arabic numbers to alien numbers. If you can convert between base N and base M with numbers, you can do so in alien languages. Look around on the ‘Net for how to convert between bases.

    What I did was convert the first number into decimal, then convert that decimal number to the target language.

  8. Spot on with this write-up, I actually believe that this website needs a
    lot more attention. I’ll probably be returning to read more, thanks for the info!


  1. 1 Google code jam solution for alien numbers | united-coders.com
  2. 2 united-coders - Google code jam solution for alien numbers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: