-
Notifications
You must be signed in to change notification settings - Fork 2
/
p022.py
32 lines (27 loc) · 785 Bytes
/
p022.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# Project Euler
# http://projecteuler.net/problem=22
# Problem 22
# Working out the alphabetical value for each name, using the text file
# "problem22_data.txt", multiply its value by its alphabetical position
# in the list to obtain a name score.
#
# What is the total of all the name scores in the file?
def sum_letters(x):
x=x.strip('"').lower()
letters="abcdefghijklmnopqrstuvwxyz"
letter_sum=0
for c in x:
letter_sum+=letters.index(c)+1
return letter_sum
if __name__ == "__main__":
f=open("problem22_data.txt",'r')
names=f.read()
names=names.split(",")
for i in xrange(len(names)):
names[i]=names[i].strip('"').strip("'").lower()
names=sorted(names)
total=0
for i in xrange(len(names)):
total+=(i+1)*sum_letters(names[i])
print total