### 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

Advertisements

Filed under: Informatics, Programming, Python | 11 Comments

Tags: algorith, code, google, jam, Python

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

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

has any one got the output file??

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

Nice solution

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

Thanks

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.

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!