首页 > 代码库 > projecteuler---->problem=22----Names scores
projecteuler---->problem=22----Names scores
sing 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?
翻译:
下载这个文档names.txt,这个46KB的文本文档包含超过五千个姓氏,你要先把它们按字母表顺序排列好,然后把姓氏中每个字母在字母表中的索引加总,最后将所有索引的和乘以该姓氏在列表中的索引,就得到该姓氏的分值了。
例如,假定你已经把姓氏列表排好了,找到一个姓氏叫Colin,它的字母索引总和为3 + 15 + 12 + 9 + 14 = 53,已经它是第938个姓氏,于是它的分值就是938 × 53 = 49714。
你知道所有名字的总分值是多少吗?
import fileinput,string f=open("22.txt","r") s=f.readline() info=s.split(',') info.sort() resu=0 for i in range(0,len(info)): addsum=0 for j in range(0,len(info[i])): if info[i][j]>='A' and info[i][j] <= 'Z': addsum+=ord(info[i][j])-ord('A')+1 addsum*=(i+1) resu+=addsum print resu
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。