Names Scores¶
First, let's read the names into a list. We'll also strip the quotes around each name and sort them.
In [1]:
with open("txt/0022_names.txt") as f:
names = f.read().split(',')
for (i, name) in enumerate(names):
names[i] = name.strip('"')
names.sort()
The ord function returns the Unicode code point of the provided character. Conveniently, the code point for A is 65, B is 66, and so on, so we can just subtract 64 to get an (uppercase) letter's number value. (These code points were chosen to be the same as ASCII's for backward compatibility. Here's a short but informative video on Unicode and UTF-8.)
Add up these number values for a name, multiply by the position in the list, and sum up.
In [2]:
def name_score(pos, name):
return pos * sum(ord(c) - 64 for c in name)
sum(name_score(i, name) for (i, name) in enumerate(names, start=1))
Out[2]:
871198282
Copyright (C) 2025 filifa¶
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.