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