首先,我是python和一般编程的初学者。我正在尝试解决将正整数转换为单词的leetcode问题。我的算法似乎几乎是正确的,但存在一些瓶颈。
我不知道如何摆脱数量少于一百的one
,请查看输出,这将导致转换错误。
我已经有一段时间没有运气了,以为我应该寻求帮助。谢谢
def numberToWords(num):
store = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15 : 'fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen',
19: 'Nineteen'}
word = ['Billion', 'Million', 'Thousand', 'Hundred', 'Ninety', 'Eighty', 'Seventy', 'Sixty', 'Fifty',\
'Forty', 'Thirty', 'Twenty', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One']
value = [1000000000, 1000000, 1000, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
if num == 0: return 'Zero'
result = ''
if num in store: return store[num]
for i in range(len(value)):
if (num //value[i] >= 1):
y = num // value[i]
result += numberToWords(y) + " " + word[i] + " "
num %= value[i]
return result
print(numberToWords(12345))
我的输出:"Twelve Thousand Three Hundred One Forty One Five "
预期输出:"Twelve Thousand Three Hundred Forty Five"
答案 0 :(得分:0)
您的核心逻辑很好。本质上,您的方法将数字分解为十的幂。 (数量*值)
例如2004 = 2 * 1000 + 0 * 100 + 0 * 10 + 4 * 1
然后将其转换为具有连接的数量和值的字符串:
例如 2 * 1000 + 1 * 4 =('两个'+'千')+('一个'+'四')。
基本上2 * 1000是两个Thousands
。错误是,在最后一个小数点后,您的代码将1 * 4转换为四,因为从技术上讲1 * 4实际上是四个。
因此,请检查您是否位于小数点后一位,并省略该值的字符串,这在下面的代码中已经完成。
还有其他一些固定的情况。例如,20014是2万和14,而不是2万,10和4。这是通过检查num是否在每次循环迭代中都位于store
中而不是仅在开始时进行的。
def numberToWords(num):
store = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15 : 'fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen',
19: 'Nineteen'}
word = ['Billion', 'Million', 'Thousand', 'Hundred', 'Ninety', 'Eighty', 'Seventy', 'Sixty', 'Fifty',\
'Forty', 'Thirty', 'Twenty', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One']
value = [1000000000, 1000000, 1000, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
if num == 0: return 'Zero'
result = ''
if num in store:
return store[num]
for i in range(len(value)):
if (num //value[i] >= 1):
y = num // value[i]
if num in store: # <-- Check if num now has a standard word
result += store[num]
break
elif num > 9 and y > 1: # <-- check for last decimal place
result += numberToWords(y) + " " + word[i] + " "
elif num > 9 and y == 1:
result += word[i] + " "
else:
result += 'and ' + numberToWords(num)
num %= value[i]
return result
print(numberToWords(20014)) # Twenty Thousand Fourteen
print(numberToWords(121)) # Hundred Twenty One
答案 1 :(得分:0)
如果value [i]小于100,则不要打印一个。在这里:
def numberToWords(num):
store = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15 : 'fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen',
19: 'Nineteen'}
word = ['Billion', 'Million', 'Thousand', 'Hundred', 'Ninety', 'Eighty', 'Seventy', 'Sixty', 'Fifty',\
'Forty', 'Thirty', 'Twenty', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One']
value = [1000000000, 1000000, 1000, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
if num == 0: return 'Zero'
result = ''
if num in store: return store[num]
for i in range(len(value)):
if (num //value[i] >= 1):
y = num // value[i]
if value[i] < 100:
result += word[i] + " "
else:
result+=numberToWords(y) + " " + word[i] + " "
num %= value[i]
return result
print(numberToWords(12345))
输出:123.345万