Skip to content

Instantly share code, notes, and snippets.

@antonylu
Created November 3, 2019 02:55
Show Gist options
  • Select an option

  • Save antonylu/0a7e5da6abac62b7dcf75a9799fbfd9c to your computer and use it in GitHub Desktop.

Select an option

Save antonylu/0a7e5da6abac62b7dcf75a9799fbfd9c to your computer and use it in GitHub Desktop.
Solution of alien numbers at https://open.kattis.com/problems/aliennumbers
"""
https://open.kattis.com/problems/aliennumbers
Input
#alien_number source_language target_language
4
9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01
CODE O!CDE? A?JM!.
Output
Case #1: Foo
Case #2: 9
Case #3: 10011
Case #4: JAM!
"""
def base_to_int(code, lang):
l = len(lang)
s = 0
n = 0
m = {lang[i]: i for i in range(l)}
for i in reversed(code):
s += m[i] * l ** n
n += 1
return s
def int_to_base(num, lang):
lst = []
l = len(lang)
m = {i: lang[i] for i in range(l)}
while num > 0:
lst = [m[num % l]] + lst
num //= l
return "".join(lst)
result = []
for j in range(int(input())):
result.append('Case #{0}: {1}'.format(j+1, (lambda s: int_to_base(base_to_int(s[0], s[1]), s[2]))(input().split())))
print("\n".join(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment