Last active
August 29, 2015 14:09
-
-
Save conorgriffin/c41f6bb0dfdbdd0f744e to your computer and use it in GitHub Desktop.
A Python solution to the 2009 Google Code Jam problem entitled Alien Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
""" | |
http://code.google.com/codejam/contest/32003/dashboard#s=p0 | |
""" | |
def process(current_case): | |
alien_number, source_language, target_language = current_case.split() | |
source_language = list(source_language) | |
target_language = list(target_language) | |
decimal_value = 0 | |
for index, numeral in enumerate(reversed(alien_number)): | |
# add the following to the current decimal value | |
# the position of the symbol in the source language (its value) | |
# multiplied by the number of symbols used by the language (its base) | |
# to the power of the position it was found in the alien_number (its magnitude) | |
decimal_value += source_language.index(numeral)*len(source_language)**index | |
target_number = '' | |
while decimal_value: | |
target_number = target_language[decimal_value % len(target_language)] + target_number | |
decimal_value /= len(target_language) | |
return target_number | |
if __name__ == "__main__": | |
filename = sys.argv[1] | |
cases = open(filename).read().splitlines() | |
cases.pop(0) # discard the first line of input | |
output = re.split('\.', filename)[0] + '-out.txt' # generate output file | |
f_out = open(output, 'w') | |
for n, case in enumerate(cases): | |
result = 'Case #' + str(n+1) + ': ' + process(case) | |
f_out.write(result + '\n') | |
print result | |
print 'Results saved to '+output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment