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?


In [1]:
f = open('./p022_names.txt', 'r')
d = f.read()
f.close()

names = sorted([s[1:-1] for s in d.split(',')])
pprint(names[:20])

assert names.index('COLIN') == 938-1

def name_score(n):
    return sum([ord(c) - ord('a') + 1 for c in n.lower()])

assert(name_score(n='COLIN') == 53)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-aa349e181824> in <module>()
      4 
      5 names = sorted([s[1:-1] for s in d.split(',')])
----> 6 pprint(names[:20])
      7 
      8 assert names.index('COLIN') == 938-1

NameError: name 'pprint' is not defined

In [ ]:
total = 0
for i, n in enumerate(names):
    total += (i+1)*name_score(n=n)
    
print total

In [ ]: