Project Euler 22

Project Euler 22

题目

Names score

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 \(938\text{th}\) name in the list. So, COLIN would obtain a score of \(938 \times 53 = 49714\).

What is the total of all the name scores in the file?

解决方案

将读入的字符串用一个列表存下。然后直接排序,直接计算相加得到结果。

代码

1
2
3
4
5
6
7
8
9
10
ls = open('p022_names.txt', 'r').readlines()[0].split(',')
ls.sort()
ans = 0
for i in range(len(ls)):
s = ls[i].replace("\"", "")
val = 0
for ch in s:
val += ord(ch) - ord('A') + 1
ans += val * (i + 1)
print(ans)
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
Ujimatsu Chiya 微信 微信
Ujimatsu Chiya 支付宝 支付宝