feat: function now creates blueprint for integer part
This commit is contained in:
parent
96aa72d123
commit
c9bcc399d6
@ -26,7 +26,8 @@ C = {
|
|||||||
19: "十九",
|
19: "十九",
|
||||||
}
|
}
|
||||||
|
|
||||||
D = {2: 10, 3: 100, 4: 1000, 5: 10000}
|
# "Ten^x" dictonary
|
||||||
|
T = {2: 10, 3: 100, 4: 1000, 5: 10000}
|
||||||
|
|
||||||
|
|
||||||
def to_chinese_numeral(n):
|
def to_chinese_numeral(n):
|
||||||
@ -36,29 +37,61 @@ def to_chinese_numeral(n):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Check if negative
|
# Check if negative, turn positive and remeber state
|
||||||
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
|
# Split number in iteger list and fractional (None if not exists)
|
||||||
split_number = [int(x) for x in str(n)]
|
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
|
||||||
|
|
||||||
arranged_number = []
|
# Blueprint list will contain building instruction on how to encode the number: 90090 -> [9, 10000, 0, 9, 10]
|
||||||
for _ in range(0, len(split_number)):
|
blueprint = []
|
||||||
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:
|
blueprint.append(integer[0]) # First digit won't be 0 it can just be appended
|
||||||
arranged_number.insert(0, "-")"""
|
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, 1, -1):
|
||||||
|
if blueprint[i] == 0:
|
||||||
|
blueprint.pop(i)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Remove grouped zeros
|
||||||
|
was_zero = False
|
||||||
|
for j, i in enumerate(blueprint):
|
||||||
|
if i == 0 and not was_zero:
|
||||||
|
was_zero = True
|
||||||
|
elif i == 0 and was_zero:
|
||||||
|
blueprint.pop(j)
|
||||||
|
elif i != 0 and was_zero:
|
||||||
|
was_zero = False
|
||||||
|
|
||||||
|
print(blueprint)
|
||||||
|
print(fractional)
|
||||||
|
|
||||||
return is_negative
|
return is_negative
|
||||||
|
|
||||||
|
|
||||||
print(to_chinese_numeral(-90909)) # 9 10000 9
|
to_chinese_numeral(-90090.358)
|
||||||
|
to_chinese_numeral(90000)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user