Compare commits
3 Commits
96aa72d123
...
1a5073c2a7
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a5073c2a7 | |||
| ada5130c9b | |||
| c9bcc399d6 |
@ -1,4 +1,4 @@
|
|||||||
C = {
|
numerals = {
|
||||||
"-": "负",
|
"-": "负",
|
||||||
".": "点",
|
".": "点",
|
||||||
0: "零",
|
0: "零",
|
||||||
@ -15,50 +15,95 @@ C = {
|
|||||||
100: "百",
|
100: "百",
|
||||||
1000: "千",
|
1000: "千",
|
||||||
10000: "万",
|
10000: "万",
|
||||||
11: "十一",
|
|
||||||
12: "十二",
|
|
||||||
13: "十三",
|
|
||||||
14: "十四",
|
|
||||||
15: "十五",
|
|
||||||
16: "十六",
|
|
||||||
17: "十七",
|
|
||||||
18: "十八",
|
|
||||||
19: "十九",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
D = {2: 10, 3: 100, 4: 1000, 5: 10000}
|
# "Ten^x" dictonary
|
||||||
|
T = {1: 0, 2: 10, 3: 100, 4: 1000, 5: 10000}
|
||||||
|
|
||||||
|
|
||||||
def to_chinese_numeral(n):
|
def to_chinese_numeral(n):
|
||||||
# Check if n in C dictonary
|
# Check if negative, turn positive and remeber state
|
||||||
try:
|
|
||||||
return C[n]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Check if negative
|
|
||||||
if n < 0:
|
if n < 0:
|
||||||
n = abs(n)
|
n = abs(n)
|
||||||
is_negative = True
|
is_negative = True
|
||||||
else:
|
else:
|
||||||
is_negative = False
|
is_negative = False
|
||||||
|
|
||||||
"""# Put single digits in list
|
# return C[n] if n in C dictonary
|
||||||
split_number = [int(x) for x in str(n)]
|
if n % 100 != 0:
|
||||||
|
try:
|
||||||
|
if is_negative:
|
||||||
|
return "".join([numerals["-"], numerals[n]])
|
||||||
|
else:
|
||||||
|
return numerals[n]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
arranged_number = []
|
# Split number in iteger list and fractional (None if not exists)
|
||||||
for _ in range(0, len(split_number)):
|
if type(n) is not int:
|
||||||
arranged_number.append(split_number[0])
|
n = str(n).split(".")
|
||||||
if len(split_number) in D:
|
integer = [int(x) for x in n[0]]
|
||||||
arranged_number.append(D[len(split_number)])
|
fractional = [int(x) for x in n[1]]
|
||||||
split_number.pop(0)
|
else:
|
||||||
if arranged_number[-1] == 0:
|
integer = [int(x) for x in str(n)]
|
||||||
arranged_number.pop(-1)
|
fractional = None
|
||||||
|
|
||||||
|
# Blueprint list will contain building instruction on how to encode the number: 90090 -> [9, 10000, 0, 9, 10]
|
||||||
|
blueprint = []
|
||||||
|
|
||||||
|
blueprint.append(integer[0]) # First digit won't be 0 it can just be appended
|
||||||
|
blueprint.append(T[len(integer)]) # Length of integer determines power of ten
|
||||||
|
integer.pop(0) # Pop first digit
|
||||||
|
|
||||||
|
# create rest of blueprint by looping rest of integer
|
||||||
|
for _ in range(0, len(integer)):
|
||||||
|
# 0 needs no power of ten
|
||||||
|
if integer[0] == 0:
|
||||||
|
blueprint.append(0)
|
||||||
|
else:
|
||||||
|
blueprint.append(integer[0])
|
||||||
|
blueprint.append(T[len(integer)])
|
||||||
|
integer.pop(0)
|
||||||
|
|
||||||
|
# Pop last digit in blueprint if it's a 0
|
||||||
|
for i in range(len(blueprint) - 1, 0, -1):
|
||||||
|
if blueprint[i] == 0:
|
||||||
|
blueprint.pop(i)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Remove grouped zeros
|
||||||
|
was_zero = False
|
||||||
|
index_to_remove = []
|
||||||
|
for j, i in enumerate(blueprint):
|
||||||
|
if i == 0 and not was_zero:
|
||||||
|
was_zero = True
|
||||||
|
elif i == 0 and was_zero:
|
||||||
|
index_to_remove.append(j)
|
||||||
|
elif i != 0 and was_zero:
|
||||||
|
was_zero = False
|
||||||
|
|
||||||
|
index_to_remove.sort(
|
||||||
|
reverse=True
|
||||||
|
) # Reverse indices to avoid index errors when applying pop()
|
||||||
|
[blueprint.pop(x) for x in index_to_remove]
|
||||||
|
|
||||||
|
# remove first item in blueprint if second item in blueprint is between 10 and 19. Skip step if index error
|
||||||
|
try:
|
||||||
|
if sum(blueprint[0:2]) == 11:
|
||||||
|
blueprint.pop(0)
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Add symbols - if n was negative and . if it was a fraction
|
||||||
if is_negative:
|
if is_negative:
|
||||||
arranged_number.insert(0, "-")"""
|
blueprint.insert(0, "-")
|
||||||
|
|
||||||
return is_negative
|
if fractional is not None:
|
||||||
|
blueprint.append(".")
|
||||||
|
[blueprint.append(x) for x in fractional]
|
||||||
|
|
||||||
|
# Apply chinese encoding to blueprint and fractional
|
||||||
|
encoding = [numerals[x] for x in blueprint]
|
||||||
|
|
||||||
print(to_chinese_numeral(-90909)) # 9 10000 9
|
return "".join(encoding)
|
||||||
|
|||||||
@ -12,3 +12,4 @@ def test_to_chinese():
|
|||||||
assert to_chinese_numeral(10000) == "一万"
|
assert to_chinese_numeral(10000) == "一万"
|
||||||
assert to_chinese_numeral(10006) == "一万零六"
|
assert to_chinese_numeral(10006) == "一万零六"
|
||||||
assert to_chinese_numeral(10306.005) == "一万零三百零六点零零五"
|
assert to_chinese_numeral(10306.005) == "一万零三百零六点零零五"
|
||||||
|
assert to_chinese_numeral(-10.000001) == "负十点零零零零零一"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user