-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patha2c.py
93 lines (77 loc) · 2.44 KB
/
a2c.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
# -*- coding: utf-8 -*-
""" Convert to Chinese numerals """
# Define exceptions
class NotIntegerError(Exception):
pass
def to_chinese(number):
""" convert integer to Chinese numeral """
chinese_numeral_dict = {
'0': '零',
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六',
'7': '七',
'8': '八',
'9': '九'
}
chinese_unit_map = [('', '十', '百', '千'),
('万', '十万', '百万', '千万'),
('亿', '十亿', '百亿', '千亿'),
('兆', '十兆', '百兆', '千兆'),
('吉', '十吉', '百吉', '千吉')]
chinese_unit_sep = ['万', '亿', '兆', '吉']
reversed_n_string = reversed(str(number))
result_lst = []
unit = 0
for integer in reversed_n_string:
if integer is not '0':
result_lst.append(chinese_unit_map[unit // 4][unit % 4])
result_lst.append(chinese_numeral_dict[integer])
unit += 1
else:
if result_lst and result_lst[-1] != '零':
result_lst.append('零')
unit += 1
result_lst.reverse()
# clean convert result, make it more natural
if result_lst[-1] is '零':
result_lst.pop()
result_lst = list(''.join(result_lst))
for unit_sep in chinese_unit_sep:
flag = result_lst.count(unit_sep)
while flag > 1:
result_lst.pop(result_lst.index(unit_sep))
flag -= 1
'''
length = len(str(number))
if 4 < length <= 8:
flag = result_lst.count('万')
while flag > 1:
result_lst.pop(result_lst.index('万'))
flag -= 1
elif 8 < length <= 12:
flag = result_lst.count('亿')
while flag > 1:
result_lst.pop(result_lst.index('亿'))
flag -= 1
elif 12 < length <= 16:
flag = result_lst.count('兆')
while flag > 1:
result_lst.pop(result_lst.index('兆'))
flag -= 1
elif 16 < length <= 20:
flag = result_lst.count('吉')
while flag > 1:
result_lst.pop(result_lst.index('吉'))
flag -= 1
'''
return ''.join(result_lst)
if __name__ == '__main__':
foo = ''
for i in range(1, 100001):
foo += to_chinese(i) + '\n'
print(foo)
# print('对不起,第{0}遍'.format(to_chinese(i)))