# from math import log def num_generator alphabet yield gen num_generato

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69``` ```from math import log def num_generator(alphabet): yield '' gen = num_generator(alphabet) while True: tail = gen.next() for c in alphabet: yield tail + c def wrapper(alphabet, generator): while True: s = generator.next() if s and not s[0] == alphabet[0]: yield s def do_stuff(alphabet): gen = num_generator(alphabet) return wrapper(alphabet, gen) def alien_to_num(source_alpha, source_word): good_alpha = map(chr, range(len(source_alpha))) good_word = source_word for i,c in enumerate(source_alpha): good_word = good_word.replace(source_alpha[i], chr(i)) power = len(source_alpha) sum = 0 for i,c in enumerate(good_word[::-1]): sum += ord(c) * power**i return sum def num_to_alien(num, dest_alpha): power = len(dest_alpha) max_index = 0 s=[] while power**max_index < num: max_index += 1 index = max_index while num > 0: remainder = num % power s.append(remainder) num /= power return ''.join( dest_alpha[c] for c in s[::-1] ) def translate(source_word, source_alpha, dest_alpha): source_gen = do_stuff(source_alpha) dest_gen = do_stuff(dest_alpha) while True: s_n = source_gen.next() d_n = dest_gen.next() print source_word, s_n, d_n if s_n == source_word: return d_n def translate2(source_word, source_alpha, dest_alpha): return num_to_alien(alien_to_num(source_alpha, source_word), dest_alpha) def process_string(s): s = s.strip().split("\n") num = s[0] assert len(s)-1 == int(num) result = '' for i,q in enumerate(s[1:]): result += "Case #%d: %s\n" % (i+1, translate2(*q.split())) return result ```