TL;DR
def chinese_to_arabic(chinese_number):
chinese_arabic_dict = {
'零': 0, '一': 1, '二': 2, '三': 3, '四': 4,
'五': 5, '六': 6, '七': 7, '八': 8, '九': 9,
'十': 10, '百': 100, '千': 1000, '万': 10000, '亿': 100000000
}
total = 0
r = 1 # The factor that will multiply the unit, such as 10 for 十, 100 for 百.
for i in range(len(chinese_number) - 1, -1, -1):
val = chinese_arabic_dict.get(chinese_number[i])
if val >= 10 and i == 0: # This is for when the Chinese number begins with 十 (such as 十一).
if val > r:
r = val
total = total + val
else:
r = r * val
elif val >= 10:
if val > r:
r = val
else:
r = r * val
else:
total = total + r * val
return total
print(chinese_to_arabic('一')) # Should return 1
print(chinese_to_arabic('二')) # Should return 2
print(chinese_to_arabic('十一')) # Should return 11
print(chinese_to_arabic('二十五')) # Should return 25
print(chinese_to_arabic('一百三十四')) # Should return 134
print(chinese_to_arabic('七千八百九十七')) # Should return 7897
Thanks to the help of ChatGPT 4!
Origin prompt:
Would you write a python function to convert chinese number to arabic number?
一 = 1
二 = 2
十一 = 11
二十五 = 25
一百三十四 = 134
七千八百九十七 = 7897
...