Google code jam solution for alien numbers

Christian Harms's picture

Time again for a new google code jam article about alien numbers. This time instead of decoding words from an other alien language we have to convert numbers from one alien digit system to another.

The problem: 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.

convert binary to hex numbers

The basic idea was to convert binary numbers to hex numbers.

  1. def bin2hex(binStr):
  2.     binChar = "01"
  3.     binBase = 2
  4.     hexChar = "0123456789ABCDEF"
  5.     hexBase = 16
  6.     n = 0
  7.     for c in binStr:
  8.         n=n*binBase+binChar.find(c)
  9.     res = ""
  10.     while n:
  11.         res = hexChar[n%hexBase] + res
  12.         n = n / hexBase
  13.     return res

binBase and hexBase are imported to calculate the integer n, but the both values are the same like the length of the digit string.

  • binBase == len(binChar)
  • hexBase == len(hexChar)

common function convert hex to binary numbers

  1. def convert(num, src, trg):
  2.     n = 0
  3.     for c in num:
  4.         n=n*len(src)+src.find(c)
  5.     res = ""
  6.     while n:
  7.         res = trg[n%len(trg)] + res
  8.         n/=len(trg)
  9.     return res

convert examples as yoda condition from binary, hex or octal into decimal:

  • '13' == convert('1101', '01', '0123456789')
  • '31' == convert('1F', '0123456789ABCDEF', '0123456789')
  • '80' == convert('88', '012345678', '0123456789')

convert examples as yoda condition from decimal to binary, hex or octal:

  • '1101' == convert('13', '0123456789', '01')
  • '1F' == convert('31', '0123456789', '0123456789ABCDEF')
  • '88' == convert('80', '0123456789', '012345678')

You can use the code to convert from hex-decimal to binary or octal numbers. This works with any other char mapping based number system.

the solution

To solve this problem use the alien char mappings, convert the source alien number into an integer and produce from the integer the target alien number. Because the integer in python is not limited to 32bit, there is no need to use any big-int library.

To solve the complete task you have to convert every input line (which has the alien number num, the source digits and the target digits) and print the solution.

  1. #!/usr/bin/python
  2. import sys
  3. fp = file(sys.argv[1])
  4. for case in range(int(fp.next())):
  5.     (num, src, trg) = fp.next().split()
  6.     n = 0
  7.     for c in num:
  8.         n=n*len(src)+src.find(c)
  9.     res = ""
  10.     while n:
  11.         res = trg[n%len(trg)] + res
  12.         n/=len(trg)
  13.     print "Case #%d: %s" % (case+1, res)
  14. fp.close()

run time

The two input files was not large and the run time was not the challenge.

  1. time python 2010_alien_numbers.py A-large.in > A-large.out
  2.  
  3. real    0m0.063s
  4. user    0m0.052s
  5. sys     0m0.012s

other solutions

Because the alien number problem is in the practice section you can find solutions in other programming languages:

Comments

 Twitter Trackbacks for Google code jam solution for alien n's picture

[...] Google code jam solution for alien numbers | united-coders.com united-coders.com/christian-harms/google-code-jam-solution-for-alien-numbers – view page – cached Time again for a new google code jam article about alien numbers. This time instead of decoding words from an other alien language we have to convert numbers from one alien digit system to another. Tweets about this link [...]

Alien Numbers « Programming Praxis's picture

[...] 24, 2010 Today’s exercise comes to us from the Google Code Jam via Christian Harms’ blog: [...]

kilork's picture

Visit http://codejamer.com – javascript online editor for google code jam.

Solutions for the google code jam 2012 qualify round | unite's picture

[...] alien numbers from the Practice [...]