Compare commits
3 Commits
96aa72d123
...
1a5073c2a7
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a5073c2a7 | |||
| ada5130c9b | |||
| c9bcc399d6 |
@ -1,4 +1,4 @@
|
||||
C = {
|
||||
numerals = {
|
||||
"-": "负",
|
||||
".": "点",
|
||||
0: "零",
|
||||
@ -15,50 +15,95 @@ C = {
|
||||
100: "百",
|
||||
1000: "千",
|
||||
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):
|
||||
# Check if n in C dictonary
|
||||
try:
|
||||
return C[n]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
# Check if negative
|
||||
# Check if negative, turn positive and remeber state
|
||||
if n < 0:
|
||||
n = abs(n)
|
||||
is_negative = True
|
||||
else:
|
||||
is_negative = False
|
||||
|
||||
"""# Put single digits in list
|
||||
split_number = [int(x) for x in str(n)]
|
||||
|
||||
arranged_number = []
|
||||
for _ in range(0, len(split_number)):
|
||||
arranged_number.append(split_number[0])
|
||||
if len(split_number) in D:
|
||||
arranged_number.append(D[len(split_number)])
|
||||
split_number.pop(0)
|
||||
if arranged_number[-1] == 0:
|
||||
arranged_number.pop(-1)
|
||||
|
||||
# return C[n] if n in C dictonary
|
||||
if n % 100 != 0:
|
||||
try:
|
||||
if is_negative:
|
||||
arranged_number.insert(0, "-")"""
|
||||
return "".join([numerals["-"], numerals[n]])
|
||||
else:
|
||||
return numerals[n]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
return is_negative
|
||||
# Split number in iteger list and fractional (None if not exists)
|
||||
if type(n) is not int:
|
||||
n = str(n).split(".")
|
||||
integer = [int(x) for x in n[0]]
|
||||
fractional = [int(x) for x in n[1]]
|
||||
else:
|
||||
integer = [int(x) for x in str(n)]
|
||||
fractional = None
|
||||
|
||||
# Blueprint list will contain building instruction on how to encode the number: 90090 -> [9, 10000, 0, 9, 10]
|
||||
blueprint = []
|
||||
|
||||
print(to_chinese_numeral(-90909)) # 9 10000 9
|
||||
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:
|
||||
blueprint.insert(0, "-")
|
||||
|
||||
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]
|
||||
|
||||
return "".join(encoding)
|
||||
|
||||
@ -12,3 +12,4 @@ def test_to_chinese():
|
||||
assert to_chinese_numeral(10000) == "一万"
|
||||
assert to_chinese_numeral(10006) == "一万零六"
|
||||
assert to_chinese_numeral(10306.005) == "一万零三百零六点零零五"
|
||||
assert to_chinese_numeral(-10.000001) == "负十点零零零零零一"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user