-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem=22.py
More file actions
29 lines (20 loc) · 901 Bytes
/
problem=22.py
File metadata and controls
29 lines (20 loc) · 901 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
28
29
# Names Scores - https://projecteuler.net/problem=22
def compute():
import string
# Read names from the file
with open('0022_names.txt', 'r') as f:
names = f.read().replace('"', '').split(',')
# Sort names alphabetically
names.sort()
# Create a dictionary to map letters to their alphabetical values
letter_values = {letter: index for index, letter in enumerate(string.ascii_uppercase, start=1)}
# Function to calculate the score of a name
def name_score(name, position):
score = sum(letter_values[char] for char in name)
return score * position
# Calculate the total score for all names
total_score = sum(name_score(name, index + 1) for index, name in enumerate(names))
return str(total_score)
if __name__ == "__main__":
result = compute()
print(f"The total of all the name scores in the file is: {result}")