-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 22
More file actions
29 lines (21 loc) · 942 Bytes
/
Copy pathproblem 22
File metadata and controls
29 lines (21 loc) · 942 Bytes
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
"""
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into letterbetical order. Then working out the letterbetical value for each name, multiply this value by its letterbetical position in the list to obtain a name name_score.
For example, when the list is sorted into letterbetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a name_score of 938 × 53 = 49714.
What is the total of all the name name_scores in the file?
"""
def score(name):
for letter in name:
return sum(ord(c) -64 for c in name.strip('"'))
path= 'names.txt'
names_file = open(path,'r')
names = names_file.read().split(',')
names.sort()
#print(len(names))
#print(names[937])
#print(score("COLIN"))
position = 0
sumof = 0
for name in names:
position +=1
sumof += (position * score(name))
print(sumof)