2013-10-19 29 views
0

我在做euler問題來練習python,我似乎無法得到problem 17的正確答案。問題是找到所有數字1-1000中沒有空格或連字符的字母數。 任何評論將是有用的。號碼到基數蟒euler

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') 
+0

這確實是這個問題的大量代碼。你應該更加數學地做些事情,並節省很多麻煩:http://www.mathblog.dk/project-euler-17-letters-in-the-numbers-1-1000/ – kren470

回答

0

第一更改四十至四十(寫在問題陳述)

和ELIF第二 升== 3:塊 爲什麼R您的寫作INT(因爲我們之前已經討論過這些情況(我的意思是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()