【发布时间】:2018-05-14 12:59:19
【问题描述】:
我一直在尝试解决 Project Euler 的问题 22,但我似乎无法得到正确的答案或发现我的代码有任何问题。我复制并粘贴了 txt 文件的内容,而不是直接从我的代码中访问它。
https://projecteuler.net/problem=22
namelst = open(names.txt)
namedict = {}
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for x in namelst:
score = 0 #creates score for each word
for y in x:
score += alphabet.index(y.lower())+1 #adds alphabetic score for each letter
namedict[x] = score
namedict = sorted(namedict.values()) #creates list of scores ordered by size
scoresum = 0
for x in namedict:
index = namedict.index(x)+1
scoresum += x*index #multiplies score sum by order in the list
print(scoresum)
不幸的是,这给我留下了985466567 的答案,而871198282 是正确的答案。我的代码或方法有什么错误吗?
如果有人可以提供帮助,那就太好了!
【问题讨论】:
标签: python python-3.x list