Skip to content

Instantly share code, notes, and snippets.

@YUChoe
Created August 23, 2019 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YUChoe/bcc54b9a17a419500cd47c85509077dc to your computer and use it in GitHub Desktop.
Save YUChoe/bcc54b9a17a419500cd47c85509077dc to your computer and use it in GitHub Desktop.
MISO coding-test-2
# Test 2
# yonguk.choe@gmail.com
#
# Write a command-line program that prints out the sum of two non-negative integers as input arguments.
# Input and output arguments are UTF-8 encoded Korean characters only listed as '일이삼사오육칠팔구' and '십백천만억조'.
# The less you use ifs, the higher you get scored.
# Google Korean Numbering System if you are not familiar with.
#
# Expected result:
# $ python3.6 test2.py 오백삼십조칠천팔백구십만천오백삼십구 삼조사천이만삼천구
# inputs: 530000078901539, 3000040023009
# result: 533000118924548
import collections
import sys
korean_numbering_system_big_unit = collections.OrderedDict({'조': 1000000000000, '억': 100000000, '만': 10000})
korean_numbering_system_small_unit = collections.OrderedDict({'천': 1000, '백': 100, '십': 10})
korean_numbering_system_base10 = ['영', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구']
def conv_small_numbers(s):
int_value = 0
for idx in range(len(s)):
if s[idx] in korean_numbering_system_small_unit and s[idx] not in korean_numbering_system_base10:
try:
int_value += korean_numbering_system_base10.index(s[idx - 1]) * korean_numbering_system_small_unit[s[idx]]
except ValueError:
# 천 == (일천)
int_value += 1 * korean_numbering_system_small_unit[s[idx]]
try:
int_value += korean_numbering_system_base10.index(s[idx]) # test last digit again
except (ValueError, UnboundLocalError):
pass
return int_value
def conv_korean_numbers(s):
int_value = 0
tmp_s = s
for unit in korean_numbering_system_big_unit:
separated_by_unit = tmp_s.split(unit)
if len(separated_by_unit) > 1:
# int_value += korean_numbering_system_big_unit[unit] if conv_small_numbers(separated_by_unit[0]) == 0 else conv_small_numbers(separated_by_unit[0]) * korean_numbering_system_big_unit[unit]
if conv_small_numbers(separated_by_unit[0]) == 0:
# 조, 억, 만 == 일조, 일억, 일만
int_value += 1 * korean_numbering_system_big_unit[unit]
else:
int_value += conv_small_numbers(separated_by_unit[0]) * korean_numbering_system_big_unit[unit]
tmp_s = separated_by_unit[1]
int_value += conv_small_numbers(tmp_s)
return int_value
def print_usage_and_exit():
print("Usage:")
print("{} [non-negative UTF-8 Korean Number] [non-negative UTF-8 Korean Number]".format(sys.argv[0]))
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) != 3:
print_usage_and_exit()
arg_int_1 = conv_korean_numbers(sys.argv[1])
arg_int_2 = conv_korean_numbers(sys.argv[2])
print('{}'.format(arg_int_1 + arg_int_2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment