Compare commits
No commits in common. "1a5073c2a7d3bd30754d05d951e28b723c94e99e" and "96aa72d12328f24fd175457f9b542549d7b8ce10" have entirely different histories.
1a5073c2a7
...
96aa72d123
@ -1,4 +1,4 @@
|
|||||||
numerals = {
|
C = {
|
||||||
"-": "负",
|
"-": "负",
|
||||||
".": "点",
|
".": "点",
|
||||||
0: "零",
|
0: "零",
|
||||||
@ -15,95 +15,50 @@ numerals = {
|
|||||||
100: "百",
|
100: "百",
|
||||||
1000: "千",
|
1000: "千",
|
||||||
10000: "万",
|
10000: "万",
|
||||||
|
11: "十一",
|
||||||
|
12: "十二",
|
||||||
|
13: "十三",
|
||||||
|
14: "十四",
|
||||||
|
15: "十五",
|
||||||
|
16: "十六",
|
||||||
|
17: "十七",
|
||||||
|
18: "十八",
|
||||||
|
19: "十九",
|
||||||
}
|
}
|
||||||
|
|
||||||
# "Ten^x" dictonary
|
D = {2: 10, 3: 100, 4: 1000, 5: 10000}
|
||||||
T = {1: 0, 2: 10, 3: 100, 4: 1000, 5: 10000}
|
|
||||||
|
|
||||||
|
|
||||||
def to_chinese_numeral(n):
|
def to_chinese_numeral(n):
|
||||||
# Check if negative, turn positive and remeber state
|
# Check if n in C dictonary
|
||||||
|
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
|
||||||
|
|
||||||
# return C[n] if n in C dictonary
|
"""# Put single digits in list
|
||||||
if n % 100 != 0:
|
split_number = [int(x) for x in str(n)]
|
||||||
try:
|
|
||||||
if is_negative:
|
|
||||||
return "".join([numerals["-"], numerals[n]])
|
|
||||||
else:
|
|
||||||
return numerals[n]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Split number in iteger list and fractional (None if not exists)
|
arranged_number = []
|
||||||
if type(n) is not int:
|
for _ in range(0, len(split_number)):
|
||||||
n = str(n).split(".")
|
arranged_number.append(split_number[0])
|
||||||
integer = [int(x) for x in n[0]]
|
if len(split_number) in D:
|
||||||
fractional = [int(x) for x in n[1]]
|
arranged_number.append(D[len(split_number)])
|
||||||
else:
|
split_number.pop(0)
|
||||||
integer = [int(x) for x in str(n)]
|
if arranged_number[-1] == 0:
|
||||||
fractional = None
|
arranged_number.pop(-1)
|
||||||
|
|
||||||
# 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:
|
||||||
blueprint.insert(0, "-")
|
arranged_number.insert(0, "-")"""
|
||||||
|
|
||||||
if fractional is not None:
|
return is_negative
|
||||||
blueprint.append(".")
|
|
||||||
[blueprint.append(x) for x in fractional]
|
|
||||||
|
|
||||||
# Apply chinese encoding to blueprint and fractional
|
|
||||||
encoding = [numerals[x] for x in blueprint]
|
|
||||||
|
|
||||||
return "".join(encoding)
|
print(to_chinese_numeral(-90909)) # 9 10000 9
|
||||||
|
|||||||
@ -12,4 +12,3 @@ 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