2008년 08월 05일
[2008 GCJ] Practice - Alien Numbers
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.
Input
The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as
alien_number source_language target_language
Each language will be represented by a list of its digits, ordered from lowest to highest value. No digit will be repeated in any representation, all digits in the alien number will be present in the source language, and the first digit of the alien number will not be the lowest valued digit of the source language (in other words, the alien numbers have no leading zeroes). Each digit will either be a number 0-9, an uppercase or lowercase letter, or one of the following symbols !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Output
For each test case, output one line containing "Case #x: " followed by the alien number translated from the source language to the target language.
Limits
1 ≤ N ≤ 100.
Small dataset
1 ≤ num digits in alien_number ≤ 4,
2 ≤ num digits in source_language ≤ 16,
2 ≤ num digits in target_language ≤ 16.
Large dataset
1 ≤ alien_number (in decimal) ≤ 1000000000,
2 ≤ num digits in source_language ≤ 94,
2 ≤ num digits in target_language ≤ 94.
Sample
Input Output
4 Case #1: Foo
9 0123456789 oF8 Case #2: 9
Foo oF8 0123456789 Case #3: 10011
13 0123456789abcdef 01 Case #4: JAM!
CODE O!CDE? A?JM!.
————————————————————————————————————————
Answer
오랜만에 머리 좀 굴릴려고 하니 ㅠㅠ. 분명히 연습문제라고 써있어서 연습 해볼려고 했다.
왜 왜왜 난 반나절이나 걸린걸까.. 영어로 씌여져 있어서 그런가 ㅠㅠ
Argorithm
1. Convert alien_num made in source_lan into decimal_num
2. Convert decimal_num into destination_lan
요센 초등학교 때 배운다던 진법 문제를 반나절이나 걸려서 완성하다니 반성해본다.
import sys
def alien2dec_letter(alien_lan,alien_letter) :
i = 0
for c in alien_lan :
if alien_lan[i] == alien_letter :
return i
i += 1
return -1
def alien2dec(alien_lan,alien_num) :
n = 0
i = 1
for c in alien_num :
n += alien2dec_letter(alien_lan,c) * ( len(alien_lan) ** (len(alien_num)-i) )
i += 1
return n
def toggle_string(src) :
dst = str()
i = len(src) - 1
while 0 <= i :
dst += src [i]
i -= 1
return dst
def dec2alien(alien_lan,dec_num) :
i = 0
s = str()
while dec_num >= len(alien_lan) :
s += alien_lan[dec_num%len(alien_lan)]
dec_num /= len(alien_lan)
i += 1
s+= alien_lan[dec_num]
return toggle_string(s)
def alien2alien(alien_num,src_lan,dst_lan):
return dec2alien(dst_lan,alien2dec(src_lan,alien_num) )
def main():
# print 'carlo world'
# print alien2alien('ff','0123456789abcdef','0123456789')
if len(sys.argv) <> 3:
sys.stderr.write('usage: practice src_file dst_file\n')
sys.exit(2)
file1, file2 = sys.argv[1], sys.argv[2]
try:
FH = open(file1)
FH2 = open(file2,'wb')
i=0
for line in FH:
each_entry = list()
for s in line.strip('\n').split(' '):
each_entry.append(s)
if(len(each_entry)==3):
rtn = alien2alien(each_entry[0],each_entry[1],each_entry[2])
frtn ="Case #%d: %s\n" % (i,rtn),
print str("Case #%d: %s" % (i,rtn))
FH2.write(str("Case #%d: %s\n" % (i,rtn)))
i += 1
FH.close()
FH2.close()
except IOError:
sys.stderr.write(file1 + ': cannot open\n')
sys.exit(1)
if __name__ == '__main__':
main()
파이썬 처음 사용해 봤는데. 괜춘한 것 같다..
문제출처:www.google.com
A-large-practice.in
A-small-practice.in
A-large-practice.out
A-small-practice.out
practice.py
# by | 2008/08/05 09:11 | CodingQuestions | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]