Problem 22
This commit is contained in:
parent
30003d04d7
commit
32d74a1e6f
37
0022.py
Normal file
37
0022.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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)
|
||||||
1
0022_names.txt
Normal file
1
0022_names.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user