Compare commits

..

No commits in common. "1a5073c2a7d3bd30754d05d951e28b723c94e99e" and "96aa72d12328f24fd175457f9b542549d7b8ce10" have entirely different histories.

2 changed files with 31 additions and 77 deletions

View File

@ -1,4 +1,4 @@
numerals = {
C = {
"-": "",
".": "",
0: "",
@ -15,95 +15,50 @@ numerals = {
100: "",
1000: "",
10000: "",
11: "十一",
12: "十二",
13: "十三",
14: "十四",
15: "十五",
16: "十六",
17: "十七",
18: "十八",
19: "十九",
}
# "Ten^x" dictonary
T = {1: 0, 2: 10, 3: 100, 4: 1000, 5: 10000}
D = {2: 10, 3: 100, 4: 1000, 5: 10000}
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:
n = abs(n)
is_negative = True
else:
is_negative = False
# return C[n] if n in C dictonary
if n % 100 != 0:
try:
"""# 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)
if is_negative:
return "".join([numerals["-"], numerals[n]])
else:
return numerals[n]
except KeyError:
pass
arranged_number.insert(0, "-")"""
# 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
return is_negative
# 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:
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)
print(to_chinese_numeral(-90909)) # 9 10000 9

View File

@ -12,4 +12,3 @@ 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) == "负十点零零零零零一"