2019-07-17 03:17:21 +02:00
|
|
|
def get_words():
|
2021-04-22 21:17:19 +02:00
|
|
|
with open("../txt/e042.txt", "r") as f:
|
2019-07-17 03:17:21 +02:00
|
|
|
s = f.read()
|
|
|
|
words = [w.strip('"') for w in s.split(",")]
|
|
|
|
return words
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_word_value(word):
|
|
|
|
word = word.upper()
|
|
|
|
return sum([ord(letter) - 64 for letter in word])
|
|
|
|
|
|
|
|
|
|
|
|
def get_triangle_numbers(n):
|
|
|
|
return {n * (n + 1) // 2 for n in range(1, 101)}
|
|
|
|
|
|
|
|
|
|
|
|
def euler_042():
|
|
|
|
triangle_numbers = get_triangle_numbers(100)
|
|
|
|
return len([word for word in get_words()
|
|
|
|
if calculate_word_value(word) in triangle_numbers])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
assert(calculate_word_value("sky") == 55)
|
|
|
|
print("e042.py: " + str(euler_042()))
|
|
|
|
assert(euler_042() == 162)
|