MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_022/sol1.py:21–42  ·  view source on GitHub ↗

Returns the total of all the name scores in the file. >>> solution() 871198282

()

Source from the content-addressed store, hash-verified

19
20
21def solution():
22 """Returns the total of all the name scores in the file.
23
24 >>> solution()
25 871198282
26 """
27 with open(os.path.dirname(__file__) + "/p022_names.txt") as file:
28 names = str(file.readlines()[0])
29 names = names.replace('"', "").split(",")
30
31 names.sort()
32
33 name_score = 0
34 total_score = 0
35
36 for i, name in enumerate(names):
37 for letter in name:
38 name_score += ord(letter) - 64
39
40 total_score += (i + 1) * name_score
41 name_score = 0
42 return total_score
43
44
45if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 2

splitMethod · 0.80
sortMethod · 0.80

Tested by

no test coverage detected