Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 欧拉计划:金额变化幅度很大_Python - Fatal编程技术网

Python 欧拉计划:金额变化幅度很大

Python 欧拉计划:金额变化幅度很大,python,Python,我目前正在尝试解决Euler项目中的问题22…如下所示: **Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name,

我目前正在尝试解决Euler项目中的问题22…如下所示:

**Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?**
以下是我为解决此问题而编写的代码:

f = open("F:\gnames.txt", "r")
strr = f.read()
w = strr.replace('"', "")
li = w.split(',')
dic = {}
sum = 0
for ee in li:
    for e in ee:
        if (e == "A"):
            sum+=1
        elif (e == "B"):
            sum+=2
        elif (e == "C"):
            sum+=3
        elif (e == "D"):
            sum+=4    
        elif (e == "E"):
            sum+=5
        elif (e == "F"):
            sum+=6
        elif (e == "G"):
            sum+=7
        elif (e == "H"):
            sum+=8
        elif (e == "I"):
            sum+=9
        elif (e == "J"):
            sum+=10
        elif (e == "K"):
            sum+=11
        elif (e == "L"):
            sum+=12
        elif (e == "M"):
            sum+=13
        elif (e == "N"):
            sum+=14
        elif (e == "O"):
            sum+=15
        elif (e == "P"):
            sum+=16
        elif (e == "Q"):
            sum+=17
        elif (e == "R"):
            sum+=18
        elif (e == "S"):
            sum+=19
        elif (e == "T"):
            sum+=20
        elif (e == "U"):
            sum+=21
        elif (e == "V"):
            sum+=22
        elif (e == "W"):
            sum+=23
        elif (e == "X"):
            sum+=24
        elif (e == "Y"):
            sum+=25
        else:
            sum+=26
    dic[ee] = sum
    sum = 0
x = sorted(dic.items(), key=lambda t: t[1])
main_sum = 0
index = 0
for c in x:
    t = c[1]*index
    main_sum = main_sum + t
    index+=1
print main_sum    

实际答案是871198282。但是,我的代码给出的答案是995996966,与实际答案相比,这个数字是124798684。我的代码似乎有什么问题

我认为您的问题在于
x=sorted(dic.items(),key=lambda t:t[1])
。这将根据分数而不是名称的字母顺序对词典项进行排序。将lambda中的
t[1]
更改为
t[0]
,我怀疑您会得到更好的结果


另一个可能的问题(有点模棱两可)是当你去加分数时你的索引。您将从零开始
索引
,但说明建议938名称应乘以938,而不是其基于零的索引,即937。您可能需要从
index=1

开始,我猜尽管您调用了
replace
strip
调用,但文件中仍然包含字母以外的字符。尝试将
if
/
elif
链的末尾更改为
elif e==“Z”:sum+=26;其他:打印(“找到未知字符:{!r}”。格式(e))
并查看是否报告了任何内容。否。没有报告。是的。它现在给了我正确的答案!非常感谢。