-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.py
More file actions
44 lines (36 loc) · 1.17 KB
/
42.py
File metadata and controls
44 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# Scores words from input and counts those whose values are triangular numbers.
"""Project Euler Problem 42: https://projecteuler.net/problem=42"""
def solve():
"""Count triangle words in the provided list."""
def is_triangle_number(y, x=1, i=1):
"""Return True if y is a triangular number."""
while x < y:
x = (i * (i + 1)) // 2
i += 1
return x == y
f = open("./0042_words.txt", "r")
print(
len(
list(
filter(
is_triangle_number,
[
sum(ord(c) - 64 for c in w)
for w in [
w
for w in [
word.replace('"', "")
for word in [
word.replace('"', "")
for word in f.read().strip().split(",")
]
]
]
],
)
)
)
)
if __name__ == "__main__":
solve()