-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem22.py
35 lines (26 loc) · 806 Bytes
/
problem22.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
33
# function to read the names from the input file.
def readFile():
file = open("inputs/problem22.txt", "r")
names = file.read().split(",")
file.close()
return names
# this function will return a new list of names sorted in alphabetical order.
def sortNames(names):
sortedNames = sorted(names)
return sortedNames
# this function will return the value of a word. The value of a word is the sum of the values of its letters.
def wordValue(word):
word = word.strip('"')
value = 0
for letter in word:
value += ord(letter) - 64
return value
# main function
def main():
names = readFile()
sortedNames = sortNames(names)
total = 0
for i in range(len(sortedNames)):
total += (i + 1) * wordValue(sortedNames[i])
print(total)
main()