Skip to content

Instantly share code, notes, and snippets.

@muminoff
Last active September 19, 2020 08:53
Show Gist options
  • Save muminoff/3baa768d2c926ec183ad8b7c04b1617a to your computer and use it in GitHub Desktop.
Save muminoff/3baa768d2c926ec183ad8b7c04b1617a to your computer and use it in GitHub Desktop.
Test #2
#!/usr/bin/env python3.8
#
# Test #2
# smuminov@gmail.com
#
# 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.
#!/usr/bin/env python3.8
import sys
big_units = dict(조=1000000000000, 억=100000000, 만=10000)
small_units = dict(천=1000, 백=100, 십=10)
base = ["영", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"]
def kns2num(num: str) -> int:
fallback = 0
for unit in big_units:
sep = num.split(unit)
if len(sep) > 1:
if convert(sep[0]) == 0:
fallback += 1 * big_units[unit]
else:
fallback += convert(sep[0]) * big_units[unit]
num = sep[1]
fallback += convert(num)
return fallback
def convert(number):
fallback = 0
get_by_index = lambda idx: number[idx]
for index in range(len(number)):
current = get_by_index(index)
if current in small_units and current not in base:
try:
previous = get_by_index(index - 1)
fallback += base.index(previous) * small_units[current]
except:
print("whoops")
fallback += 1 * small_units[current]
try:
fallback += base.index(current)
except:
print("whoops")
pass
return fallback
def main():
if len(sys.argv) == 3:
n1 = sys.argv[1]
n2 = sys.argv[2]
if n1 and n2:
n1 = kns2num(n1)
n2 = kns2num(n2)
# print(n1)
# print(n2)
print(n1 + n2)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment