### Google Code Jam: Alien Numbers Algorithm

27Jun08

Here I’m posting the source code written in Python for the Google Code Jam Practice Problems.

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))
base1 = len(str(data.strip()))
dlugosc = len(str(data))

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

nowaliczba = ""
while liczba>0:
nowaliczba = str(data.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. 2 Anders Sandvig

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

3. 3 naughty David

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. 8 game boy advance

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!