Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86654
  • 博文数量: 47
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-11 12:11
文章分类

全部博文(47)

文章存档

2008年(47)

我的朋友

分类:

2008-11-13 10:33:21

Problem 22

19 July 2002

Using (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?

def fun22():
    fp    = open("22.txt")
    line  = fp.readline()
    names = line.split(",")
    names = [i[1:-1] for i in names]
    names.sort()
    result = 0
    for i in range(len(names)):
        al = 0
        for j in names[i]:
            al += (ord(j)-ord('A')+1)
        result += (i+1)*al
    return result

没什么难度

answer is 871198282



阅读(425) | 评论(0) | 转发(0) |
0

上一篇:Problem 21

下一篇:Problem 24

给主人留下些什么吧!~~