1

我正在做欧拉问题来练习 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')
4

1 回答 1

0

首先,您将 40 更改为 40(如问题陈述中所述)

第二个在 elif l==3: block why r you writing if int(c[-3]!=0): 因为我们之前已经讨论过这些情况(我的意思是 c[-3]==1,2, 3...不等于零)如果我错了,请纠正我并尝试在您的代码中添加一些注释。

http://ideone.com/egeX0k

def countLetter(n):
p=['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine']
q=['','Ten','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
r=['','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']

c=str(n)
l=len(c)
a=""

if l==1:
    a+=p[int(c[0])]
elif l==2:
    if c[1]=='0':
        a+=q[int(c[0])]
    elif c[0]=='1':
        a+=r[int(c[1])]
    else:
        a+=q[int(c[0])]+p[int(c[1])]
elif n==100:
    a+="OneHundred"
elif l==3:
    a+=p[int(c[0])]+"Hundred"
    if c[1]=='0' and c[2]=='0':
        a+=""
    elif c[2]=='0' and c[1]!='0':
        a+="And"+q[int(c[1])]
    elif c[1]=='1':
        a+="And"+r[int(c[2])]
    else:
        a+="And"+q[int(c[1])]+p[int(c[2])]
else:
    a+="OneThousand"

#print a,len(a)
return len(a)

def main():
    sum=0
    for i in range(1,1001):
        sum+=countLetter(i)
    print sum

main()
于 2013-10-19T17:49:45.467 回答