Last active
September 7, 2019 18:13
-
-
Save prograsshopper/a1d530a626902eaab8195fea48eb9da7 to your computer and use it in GitHub Desktop.
get_sum_in_lang.py
This file contains 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
# Write a command-line program that prints out the sum of two non-negative integers as input arguments. | |
# Input arguments are UTF-8 encoded Korean characters only listed as '일이삼사오육칠팔구' and '십백천만억조', and also your program's output should be. | |
# The less you use ifs, the higher you get scored. Google Korean Numbering System if you are not familiar with. | |
import sys | |
def get_sum(first_num, second_num): | |
first_list = [s for s in first_num] | |
second_list = [s for s in second_num] | |
end = 0 | |
head = [] | |
if len(first_list) > len(second_list): | |
end = len(second_list) | |
head = first_list[:len(first_list) - end] | |
elif len(first_list) < len(second_list): | |
end = len(first_list) | |
head = second_list[:len(second_list) - end] | |
else: | |
end = len(first_list) | |
sum_result = [] | |
prev = 0 | |
for i in range(1, end+1): | |
current_num = int(first_list[-i]) + int(second_list[-i]) | |
if prev: | |
current_num += 1 | |
if current_num >= 10: | |
prev = 1 | |
current_num -= 10 | |
else: | |
prev = 0 | |
sum_result.insert(0, str(current_num)) | |
result = "".join(sum_result) | |
if prev: | |
if not head: | |
result = "1" + result | |
else: | |
for i in range(1, len(head)+1): | |
target = int(head[-i]) + 1 | |
if target >= 10: | |
head[-i] = str(target - 10) | |
else: | |
head[-i] = str(target) | |
prev = 0 | |
if prev: | |
head.insert(0, str(1)) | |
if head: | |
result = "".join(head) + result | |
return result | |
def convert_kor_num(kor_num): | |
str_num_1 = '일이삼사오육칠팔구' | |
str_num_2 = '십백천만억조' | |
number_dict_1 = {str_num_1[i-1]: i for i in range(1, len(str_num_1)+1)} | |
number_dict_2 = {str_num_2[i-1] : 10**i for i in range(1, len(str_num_2)+1)} | |
number_dict_2['억'] *= 1000 | |
number_dict_2['조'] **= 2 | |
split_num = [] | |
# 여기서 만, 억, 조 의 경우에는 천만..십억...천억...백조 등이 가능하기에 다른 케이스로 분류 | |
# 만, 억 조 의 단위 제외한 아래 숫자 따로 계산 | |
return split_num | |
def get_kor_num_sum(kor_num1, kor_num2): | |
kor_num1 = convert_kor_num(kor_num1) | |
kor_num2 = convert_kor_num(kor_num2) | |
final_result = get_sum(kor_num1, kor_num2) | |
print(final_result) | |
if __name__ == '__main__': | |
get_kor_num_sum(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment