Skip to main content

Euler 0022

The Problem:

Get the total sum of a file of names if each letter in the name is mapped from 1-26 and then multiplied by the position in an alphabetical sort.

Considerations and Approach:

We read in the file into an array of names. After that it just needs to be sorted and then propagated through to add the letter sum multiplied by the alphabetical position to the total sum.

The Code:

names = open("names.txt", "r")

name_list = sorted([x[1:-1] for x in names.readline().split(",")])

names_score = 0
for i in range(len(name_list)):
    current_name = name_list[i]
    current_score = 0
    for letter in current_name:
        #print(letter, ord(letter) - 64)
        current_score += ord(letter) - 64

    names_score += current_score*(i+1)
    print(name_list[i], (i+1)*current_score, names_score)

print(len(name_list))