As I am practicing for google codejam 2012 right now, I came across the alien number problem google gave for practice. There are alot of problems out there and I am doing different ones for practicing but solving the google ones seems to be most efficient I think.
The Problem is described as follows:

The decimal numeral system is composed of ten digits, which we represent as “0123456789” (the digits in a system are written from lowest to highest). Imagine you have discovered an alien numeral system composed of some number of digits, which may or may not be the same as those used in decimal. For example, if the alien numeral system were represented as “oF8”, then the numbers one through ten would be (F, 8, Fo, FF, F8, 8o, 8F, 88, Foo, FoF). We would like to be able to work with numbers in arbitrary alien systems. More generally, we want to be able to convert an arbitrary number that’s written in one alien system into a second alien system.

To get the work done, you do not need much code but you need some knowledge of conversion from one to another numeral system.
The easiest way to solve the problem is by using an intermediate value in a known language. I chose the decimal system.


To convert from a source_language to the intermediate system you can simply use (in python):

tmp = 0
# for each digit in the alien_number get its index and its value
for n, digit in enumerate(alien_number[::-1]):
#
tmp += source_language.index(digit)*len(source_language)**n

Simply multiply the current digit with the number of digits in the source_language to the power of the digits position in the source_language (beginning to count at 0 at the last digit).
To give you an obvious example, let’s assume we have an alien_number of 987 in the decimal system, then we can also write 987 = 7*10^0 + 8*10^1 + 9*10^2, which should remember related to place value charts from elementary school ;).
Afterwards you can simply convert from the intermediate system to the target_language using:

target_number= ''
while tmp:
target_number = target_language[tmp%len(target_language)] + target_number
tmp /= len(target_language) # reduce tep value

The clue here is to convert in slices. You need look up your at the remainder of the devision of tmp and the number of digits in the target_language.
Then you insert it in front of all the digits you already have in your target_number and divide the temporary value by the number of digits in the target_language.
Putting both together, adding some file I/O and some comments you get the solution in just a few lines of code:

#!/usr/bin/python
# http://code.google.com/codejam/contest/32003/dashboard#s=p0
import sys

def solve(alien_number, source_language, target_language):
# use a temporary integer as intermediate value in a known language
tmp = 0
# for each digit in the alien_number get its index and its value
for n, digit in enumerate(alien_number[::-1]):
#
tmp += source_language.index(digit)*len(source_language)**n

# generate the target_number by appending
target_number= ''
while tmp:
target_number = target_language[tmp%len(target_language)] + target_number
tmp /= len(target_language) # reduce tep value
return target_number

filename = sys.argv[1] #get filename from arguments
cases = open(filename).read().splitlines()
cases.pop(0)
output = filename.split('.in')[0] + '.out' # generate output filename
f_out = open(output,'w')
for n, case in enumerate(cases):
# for each test case do the convertion
[alien_number, source_language, target_language] = case.split(' ')
target_number = solve(alien_number, source_language, target_language)
f_out.write('Case #' + str(n+1)+': '+ target_number +'\n')

One Reply to “google code jam 2012: How to solve alien numbers problem”

Leave a Reply