-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangulAdd.py
127 lines (105 loc) · 2.58 KB
/
hangulAdd.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
switcherAravt={
'조': 1000000000000,
'억': 100000000,
'만': 10000,
'천': 1000,
'백': 100,
'십': 10
}
switcherAravtEsreg={
13: '조',
9: '억',
5:'만',
4: '천',
3: '백',
2: '십'
}
switcherNum={
'구': 9,
'팔': 8,
'칠': 7,
'육': 6,
'오': 5,
'사': 4,
'삼': 3,
'이': 2,
'일': 1
}
switcherNumEsreg={
9: '구',
8: '팔',
7: '칠',
6: '육',
5: '오',
4: '사',
3: '삼',
2: '이',
1: '',
0: ''
}
def parse2IntList(numStr):
aravtList=['조', '억','만','천','백','십']
intList = []
i=0
while i < len(numStr):
ucode = numStr[i].encode('utf-8')
if ucode in aravtList:
aravt = switcherAravt.get(ucode, False)
if aravt == False:
return False
last = 0
integer = aravt
j=len(intList)-1
while j >= 0:
if intList[j]<aravt:
last+=intList[j]
intList.pop()
j-=1
if last>0 and last<aravt:
integer = last*aravt
intList.append(integer)
else:
integer = switcherNum.get(ucode, False)
if integer == False:
return False
intList.append(integer)
i+=1
return intList
def parse2Int(numList):
sum = 0
i=0
while i < len(numList):
sum+=numList[i]
i+=1
return sum
def getNum():
while True:
numStr = raw_input('Please enter integer in hangul to add.Ex: 일십만 \n')
numList = parse2IntList(numStr)
if numList == False:
print('Please enter correctly. Ex: 일십만\n')
else:
num = parse2Int(numList)
return num
def num2Str(num):
numStr = str(num)
result=""
i=0
while i<len(numStr):
integer = int(numStr[i])
result = result+(switcherNumEsreg.get(integer, False))
if result == False:
return False
aravt = switcherAravtEsreg.get(len(numStr)-i, False)
if aravt != False and integer!=0:
result = result + aravt
i+=1
return result
while True:
first = getNum();
sec = getNum();
result = num2Str(first+sec)
if result == False:
print('Please enter correctly')
else:
print('Result is '+result+'\n\n\n' )