Solution for problem 22 of Project Euler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alphabet = {'"':0,'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8,'I':9,'J':10,'K':11,'L':12,'M':13,'N':14,'O':15,'P':16,'Q':17,'R':18,'S':19,'T':20,'U':21,'V':22,'W':23,'X':24,'Y':25,'Z':26} | |
def name_value(name, pos): | |
sum = 0 | |
for letter in name: | |
sum += alphabet[letter] | |
return sum * pos | |
names_file = open("names.txt") | |
names = names_file.readlines() | |
names_file.close() | |
names_list = sorted(names[0].split(',')) | |
sum = 0; | |
counter = 1 | |
for name in names_list: | |
sum += name_value(name, counter) | |
counter += 1 | |
print sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment