Help us understand the problem. What is going on with this article?

Project Euler 22「名前のスコア」

More than 1 year has passed since last update.

Problem 22 「名前のスコア」

5000個以上の名前が書かれている46Kのテキストファイル filenames.txt を用いる. まずアルファベット順にソートせよ.
のち, 各名前についてアルファベットに値を割り振り, リスト中の出現順の数と掛け合わせることで, 名前のスコアを計算する.
たとえば, リストがアルファベット順にソートされているとすると, COLINはリストの938番目にある. またCOLINは 3 + 15 + 12 + 9 + 14 = 53 という値を持つ. よってCOLINは 938 × 53 = 49714 というスコアを持つ.
ファイル中の全名前のスコアの合計を求めよ.

def hoge(path):
    ans = 0
    names = []
    with open(path) as f:
        for line in f:
            if line.strip() == '':
                continue
            names += (line.strip().replace('"', '').split(','))
    names.sort()
    score = { chr(i): i-64 for i in range(65, 65+26) }
    for i, name in enumerate(names, start=1):
        ans += sum([ score[n] for n in name ]) * i
    return ans

print hoge('./names.txt')
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away