#22项目欧拉 - 我一直得错了答案

时间:2016-03-23 12:09:45

标签: python project

The original question

  

使用names.txt(右键单击并将“保存链接/目标为...”),一个46K文本   包含超过五千个名字的文件,首先对其进行排序   按字母顺序排列。然后计算出字母值   每个名称,将该值乘以其中的字母位置   列表以获得名称分数。

     

例如,当列表按字母顺序排序时,COLIN,   值得3 + 15 + 12 + 9 + 14 = 53,是第938名   名单。因此,COLIN将获得938×53 = 49714的分数。

     

文件中所有名称分数的总和是多少?

fr = open('name.txt', 'r')
line = fr.read()
nametolist = line.split(',')

def namescores(nametolist):

    sum=0
    total=0
    for i in range(len(nametolist)):
        nametolist[i] = nametolist[i][1:-1]
    print(nametolist)
    for i in range(len(nametolist)):
        for j in range(len(nametolist[i])):
            if nametolist[i][j] == 'A':
                sum+=1
            elif nametolist[i][j] == 'B':
                sum+=2
            elif nametolist[i][j] == 'C':
                sum+=3
            elif nametolist[i][j] == 'D':
                sum+=4
            elif nametolist[i][j] == 'E':
                sum+=5
            elif nametolist[i][j] == 'F':
                sum+=6
            elif nametolist[i][j] == 'G':
                sum+=7
            elif nametolist[i][j] == 'H':
                sum+=8
            elif nametolist[i][j] == 'I':
                sum+=9
            elif nametolist[i][j] == 'J':
                sum+=10
            elif nametolist[i][j] == 'K':
                sum+=11
            elif nametolist[i][j] == 'L':
                sum+=12
            elif nametolist[i][j] == 'M':
                sum+=13
            elif nametolist[i][j] == 'N':
                sum+=14
            elif nametolist[i][j] == 'O':
                sum+=15
            elif nametolist[i][j] == 'P':
                sum+=16
            elif nametolist[i][j] == 'Q':
                sum+=17
            elif nametolist[i][j] == 'R':
                sum+=18
            elif nametolist[i][j] == 'S':
                sum+=19
            elif nametolist[i][j] == 'T':
                sum+=20
            elif nametolist[i][j] == 'U':
                sum+=21
            elif nametolist[i][j] == 'V':
                sum+=22
            elif nametolist[i][j] == 'W':
                sum+=23
            elif nametolist[i][j] == 'X':
                sum+=24
            elif nametolist[i][j] == 'Y':
                sum+=25
            else:
                sum+=26
        total += sum*(i+1)
        sum=0

1 个答案:

答案 0 :(得分:0)

您还没有对名称进行排序。问题,

begin by sorting it into alphabetical order.

因此,在计算之前使用nametolist.sort()可以解决您的问题。

但请考虑使用字典进行字母分数映射而不是26(如果是情况)并且不要使用阴影内置方法sum
像这样:

import string
#create dictionary
lettersDict = {c: L for L,c in enumerate(string.ascii_uppercase, 1)}

with open('names.txt', 'r') as fr: #using with would be better when opening files
    line = fr.read()
nametolist = line.split(',')

total=0
for i in range(len(nametolist)):
    nametolist[i] = nametolist[i][1:-1]
nametolist.sort() #sort after getting rid of punctuations

for i in range(len(nametolist)):
    wordsum = 0
    for j in range(len(nametolist[i])):
        wordsum += lettersDict[nametolist[i][j]]
    total += wordsum*(i+1)
print total