euler/python/e042.py

27 lines
684 B
Python
Raw Normal View History

def get_words():
with open("../txt/EulerProblem042.txt", "r") as f:
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)