2016-12-05 7 views
0

私はProject Euler#17で作業しており、範囲外のエラーのインデックスを取得しています。ここで関数でループを使用してオイラー17でIndexErrorを取得

は、問題のテキストです:

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

私は、引数として数を取ることができます機能を使用し、整数として1〜1000の数字を格納したループを使用してリストを作成したいですそれを英数字の文字列に変換し、それぞれの単語の数を数えて整数として返します。

次に、リストから整数を取り込み、それぞれに関数を渡すforループを作成します。ここで

は私のコードです:

letter_count = [] 

#Function that intakes integer and returns number of words it has in english. 
def numToWords(num): 
    #Store strings that contain the names of the numbers. 
    #The 0th place item is plank so that the numbers start at 1. 
    units = ['', 'one', 'two', 'three','four','five','six','seven','eight','nine'] 
    teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen,'  'seventeen','eighteen','nineteen'] 
    tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'] 
    thousands = ['', 'thousand'] 

    words = [] 
    if num==0: words.append('zero') #Special case for when the number is 0. 
    else: 
     numStr = '%d'%num #Stores number as string 
     numStrLen = len(numStr) #Checks length of string using decimal value 
     groups = (numStrLen+2)/3 
     numStr = numStr.zfill(groups*3) 
     for i in range(0, groups*3, 3): 
      h,t,u = int(numStr[i]), int(numStr[i+1]), int(numStr[i+2]) 
      g = groups - (i/3+1) 
      if h>=1: 
       words.append(units[h]) 
       words.append('hundred') 
       if num > 101 and num < 110: 
        words.append('and') #Adds the 'and' for numbers 101-109. 
      if t>1: 
       words.append(tens[t]) 
       if u>=1: words.append(units[u]) 
      elif t==1: 
       if u>=1: words.append(teens[u]) 
       else: words.append(tens[t]) 
      else: 
       if u>=1: words.append(units[u]) 
      if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',') 
    return len(words) 

#Loop to take integers in 1 to 1000 as arguments in the function 
#and append them to a new list 
for i in range(1,1001,1): 
    letter_count.append(numToWords(i)) 

#Print the sum of the new list to get the total number of words. 
print(sum(letter_count)) 

私は(エルキャップのPythonのbashコマンドを使用して)コードを実行すると、私は次のようなエラーメッセージが出ます:

Traceback (most recent call last): File "euler017.py", line 40, in letter_count.append(numToWords(i)) File "euler017.py", line 31, in numToWords if u>=1: words.append(teens[u]) IndexError: list index out of range

を、私はたくさんの上の機能をテストしました個々の乱数のそれは働いた。私も1から10の数字のループでそれをテストしました。ループが100以上でエラーが発生したときです。 https://stackoverflow.com/a/19193721/5960074

を、私はこれが事実であるかどうかはわかりませんよ。私は昔の記事で見たものを修正するための機能

。私はリストを変更するのではなく、新しいリストを作成するので、何かが範囲外になることがありますか?

ありがとうございました。

+0

私はそこに46行目があるとは思わないでしょうか? – OldBunny2800

+0

私はカウント44 – OldBunny2800

+0

Ooops!それを修正!ありがとう。 – admiralmattbar

答えて

3
は引用符のうち、16と17の間にカンマを移動

あなたは

'sixteen,'  'seventeen' 

を持っていますが、それが有効だから

'sixteen', 'seventeen' 

あなたのエラーはインタプリタで気づいていませんしたいですPython:

String literal concatenation

+0

ありがとう!私はそれを捕まえなかったとは信じられません。 – admiralmattbar

関連する問題