Last active
October 4, 2020 11:23
-
-
Save nasirovsh/958cc6232c32ddd915ced74f6828c952 to your computer and use it in GitHub Desktop.
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
| # -*- coding: utf-8 -*- | |
| import sys | |
| class Solution: | |
| ''' | |
| Test #2 | |
| ''' | |
| def read_input(self, argv): | |
| if len(argv) < 2: | |
| return None, None | |
| a = argv[0] | |
| b = argv[1] | |
| return a, b | |
| def word_to_num(self, text=''): | |
| w2n_below_shib = {'구': 9, '팔': 8, '칠': 7, '육': 6, '오': 5, '사': 4, '삼': 3, '이': 2, '일': 1, '': 0} | |
| w2n_below_man = {'천': 1000, '백': 100, '십': 10} | |
| below_man = ['천', '백', '십'] | |
| w2n_jo_and_below = {'조': 10**12, '억': 10**8, '만': 10**4} | |
| jo_and_below = ['조', '억', '만'] | |
| def parse_below_man(text=''): | |
| '''il, shib, bek, chon ''' | |
| if not text: return 0 | |
| for unit in below_man: # ['천', '백', '십'] | |
| idx = text.find(unit) | |
| if idx != -1: | |
| left_text, right_text = text[:idx], text[idx + 3:] | |
| left_num = w2n_below_shib.get(left_text) | |
| left_num = (left_num if left_num != 0 else 1) # might be absent ie: '[천] 백삼십이' | |
| right_num = parse_below_man(right_text) | |
| out = left_num * w2n_below_man.get(unit) + right_num | |
| return out | |
| '''less than 10: 삼, 이 ''' | |
| return w2n_below_shib.get(text) | |
| def parse_jo_and_below(text=''): | |
| '''jo, ok, man''' | |
| if not text: return 0 | |
| for unit in jo_and_below: # ['조', '억', '만'] | |
| idx = text.find(unit) | |
| if idx != -1: | |
| left_text, right_text = text[:idx], text[idx + 3:] | |
| left_num = parse_below_man(left_text) # left might be absent ie: '[만] 칠천일' | |
| left_num = (left_num if left_num != 0 else 1) | |
| right_num = parse_jo_and_below(right_text) | |
| out = left_num * w2n_jo_and_below.get(unit) + right_num | |
| return out | |
| '''less than man''' | |
| return parse_below_man(text) | |
| result = (0 if not text else parse_jo_and_below(text)) | |
| return result | |
| def num_to_word(self, num): | |
| below_shib = ['', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구', '십'] | |
| n2w = { | |
| 1000000000000: '조', 100000000: '억', 10000: '만', 1000: '천', 100: '백', 10: '십', | |
| 9: '구', 8: '팔', 7: '칠', 6: '육', 5: '오', 4: '사', 3: '삼', 2: '이', 1: '일' | |
| } | |
| units = [10, 100, 10**3, 10**4, 10**8, 10**12] | |
| def convert(n): | |
| if n < 10: | |
| out = [below_shib[n]] | |
| return out | |
| for i in range(1, len(units)): | |
| if n < units[i]: | |
| d = units[i-1] | |
| q, r = divmod(n, d) | |
| left = ([''] if q == 1 and d < 10 ** 8 else convert(q)) | |
| right = convert(r) | |
| n_w = left + [n2w.get(d)] + right | |
| return n_w | |
| #jos | |
| q, r = divmod(n, 10 ** 12) | |
| n_w = convert(q) + [n2w[10 ** 12]] + convert(r) | |
| return n_w | |
| return ''.join(convert(num)) | |
| def main(argv): | |
| solution = Solution() | |
| a, b = solution.read_input(argv) | |
| a_num = solution.word_to_num(a) | |
| b_num = solution.word_to_num(b) | |
| out_sum = a_num + b_num | |
| out_sum_word = solution.num_to_word(out_sum) | |
| print('{}'.format(out_sum_word)) | |
| if __name__ == "__main__": | |
| main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment