project-euler/0022.py
2018-03-29 13:39:11 +02:00

38 lines
693 B
Python

from functools import reduce
import util
target = 871198282
chars = list('abcdefghijklmnopqrstuvwxyz')
def char_to_index (char):
index = chars.index(char)
return index + 1
def get_alphabetical_value (string):
result = reduce(lambda x, y: x + char_to_index(y), list(string.lower()), 0)
return result
def get_score (var):
index, string = var
alphabetical_value = get_alphabetical_value(string)
return (index + 1) * alphabetical_value
file = open('0022_names.txt', 'r')
names = file.read()[1:-1].split('","')
names.sort()
scores = list(map(get_score, enumerate(names)))
result = reduce(lambda x, y: x + y, scores)
print(result)
print(result == target)