Names scores
First read the problem description.
from urllib.request import urlopen
def read_names():
= ""
names_data try:
with open("22.txt") as f:
= f.read()
names_data except FileNotFoundError:
with urlopen("https://xojoc.pw/project-euler/22.txt") as response:
= response.read().decode()
names_data
return (name.strip('"\n') for name in names_data.split(sep=','))
def name_score(name):
return sum(ord(l) - ord('A') + 1 for l in name)
= 0
total = sorted(read_names())
names for nth, name in enumerate(names, start=1):
+= nth * name_score(name) total
Source code of the solution(s):