Moved problems till e045.py to Python 2.
This commit is contained in:
26
python/e042.py
Normal file
26
python/e042.py
Normal file
@@ -0,0 +1,26 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user