Solution to problem 22
This commit is contained in:
42
src/Python/Problem022.py
Normal file
42
src/Python/Problem022.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Created on 31 Dec 2018
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for problem 22 of Project Euler
|
||||||
|
https://projecteuler.net/problem=22
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from utils import timeit
|
||||||
|
|
||||||
|
|
||||||
|
@timeit("Problem 22")
|
||||||
|
def compute():
|
||||||
|
"""
|
||||||
|
Using names.txt, a 46K text file containing over five-thousand first names,
|
||||||
|
begin by sorting it into alphabetical order. Then working out the
|
||||||
|
alphabetical value for each name, multiply this value by its alphabetical
|
||||||
|
position in the list to obtain a name score.
|
||||||
|
|
||||||
|
For example, when the list is sorted into alphabetical order, COLIN, which
|
||||||
|
is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So,
|
||||||
|
COLIN would obtain a score of 938 × 53 = 49714.
|
||||||
|
|
||||||
|
What is the total of all the name scores in the file?
|
||||||
|
"""
|
||||||
|
file = Path("/datos/Scripts/Gitea/Project_Euler/src/files/Problem22.txt")
|
||||||
|
with open(file, 'r') as f:
|
||||||
|
names = sorted(f.read().replace('"', '').split(','))
|
||||||
|
|
||||||
|
result = 0
|
||||||
|
for idx, name in enumerate(names, 1):
|
||||||
|
result += sum(ord(c) - 64 for c in name) * idx
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
print(f"Result for Problem 22: {compute()}")
|
||||||
Reference in New Issue
Block a user