-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path#42.py
More file actions
34 lines (27 loc) · 745 Bytes
/
#42.py
File metadata and controls
34 lines (27 loc) · 745 Bytes
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
from math import sqrt
def wordValue(word):
"""Calculate the word value.
Converting each letter in a word to a number corresponding to its
alphabetical position and adding these values we form a word value.
"""
val = 0
for alpha in word[1:len(word)-1]:
val += ord(alpha)-64
return val
def isTriangular(val):
"""
val -- word value
n -- nth term
"""
n = (sqrt(1+8*val)-1)/2
if int(n) == n:
return True
return False
with open('p042_words.txt') as f:
all_words = f.read().split(",")
count = 0
for word in all_words:
# Calculate the value of word and if it's triangular!
if isTriangular(wordValue(word)):
count += 1
print count