我正在做欧拉问题来练习 python,我似乎无法得到问题 17的正确答案。问题是找到所有数字 1-1000 中没有空格或连字符的字母数。任何评论都会很有用。
Python代码:
def numberToString(number):
""" Writes a cardinal number to a string. """
numberString=""
c = str(number)
l = len(c)
while l>0:
if l == 1: #unit digit
if int(c[-1]) == 1:
numberString += 'one'
elif int(c[-1]) == 2:
numberString += 'two'
elif int(c[-1]) == 3:
numberString += 'three'
elif int(c[-1]) == 4:
numberString += 'four'
elif int(c[-1]) == 5:
numberString += 'five'
elif int(c[-1]) == 6:
numberString += 'six'
elif int(c[-1]) == 7:
numberString += 'seven'
elif int(c[-1]) == 8:
numberString += 'eight'
elif int(c[-1]) == 9:
numberString += 'nine'
l = l-1
elif l == 2 and int(c[-2])==1 and int(c[-1]) != 0: #teens
if int(c[-1]) == 1:
numberString += 'eleven'
elif int(c[-1]) == 2:
numberString += 'twelve'
elif int(c[-1]) == 3:
numberString += 'thirteen'
elif int(c[-1]) == 4:
numberString += 'fourteen'
elif int(c[-1]) == 5:
numberString += 'fifteen'
elif int(c[-1]) == 6:
numberString += 'sixteen'
elif int(c[-1]) == 7:
numberString += 'seventeen'
elif int(c[-1]) == 8:
numberString += 'eighteen'
elif int(c[-1]) == 9:
numberString += 'nineteen'
l = l-2
elif l == 2: #tens
if int(c[-2]) == 1:
numberString += 'ten'
elif int(c[-2]) == 2:
numberString += 'twenty'
elif int(c[-2]) == 3:
numberString += 'thirty'
elif int(c[-2]) == 4:
numberString += 'fourty'
elif int(c[-2]) == 5:
numberString += 'fifty'
elif int(c[-2]) == 6:
numberString += 'sixty'
elif int(c[-2]) == 7:
numberString += 'seventy'
elif int(c[-2]) == 8:
numberString += 'eighty'
elif int(c[-2]) == 9:
numberString += 'ninety'
if int(c[-1]) != 0 and int(c[-2]) > 1:
numberString += '-'
l = l-1
elif l == 3: #hundreds
if int(c[-3]) == 1:
numberString += 'one'
elif int(c[-3]) == 2:
numberString += 'two'
elif int(c[-3]) == 3:
numberString += 'three'
elif int(c[-3]) == 4:
numberString += 'four'
elif int(c[-3]) == 5:
numberString += 'five'
elif int(c[-3]) == 6:
numberString += 'six'
elif int(c[-3]) == 7:
numberString += 'seven'
elif int(c[-3]) == 8:
numberString += 'eight'
elif int(c[-3]) == 9:
numberString += 'nine'
if int(c[-3]) != 0:
numberString += ' hundred'
if int(c[-1])+int(c[-2]) != 0:
numberString += ' and '
l = l-1
elif l == 4: #thousands
if int(c[-4]) == 1:
numberString += 'one'
elif int(c[-4]) == 2:
numberString += 'two'
elif int(c[-4]) == 3:
numberString += 'three'
elif int(c[-4]) == 4:
numberString += 'four'
elif int(c[-4]) == 5:
numberString += 'five'
elif int(c[-4]) == 6:
numberString += 'six'
elif int(c[-4]) == 7:
numberString += 'seven'
elif int(c[-4]) == 8:
numberString += 'eight'
elif int(c[-4]) == 9:
numberString += 'nine'
if int(c[-4]) != 0:
numberString += ' thousand'
if int(c[-3]) != 0:
numberString += ' '
l = l-1
return numberString
def NumbersInString(min, max):
""" Writes all cardinal numbers on a seperate line on a string, from min to max. """
allNumberString=""
for i in range (min,max+1):
allNumberString += numberToString(i)
allNumberString += '\n'
return allNumberString
def stringNumberCount(numberString):
""" Returns the ammount of letters without spaces, hyphens or newlines. """
numberString=numberString.replace(' ', '')
numberString=numberString.replace('-', '')
numberString=numberString.replace('\n', '')
return len(numberString)
def run(filename, min=1, max=1000):
s=NumbersInString(min, max)
string='Number of letters without spaces or hyphens: '+str(stringNumberCount(s))+'\n'+'List of cardinal numbers:\n'
string += s
f=open(filename, 'w')
f.write(string)
f.close
run('output.txt')