Problem 42

Python 3

 1import fileinput
 2
 3def gen_n_triangle_numbers(num):
 4    return int((num * (num + 1))/2)
 5
 6def is_triangle_word(word):
 7    triangle_nums = list()
 8    for i in range(1, 100):
 9        triangle_nums.append(gen_n_triangle_numbers(i))
10    chars = list(word)
11    sum_word = 0
12    for char in chars:
13        sum_word += (ord(char) - 64)
14    iterator = 0
15    found = False
16    while iterator < len(triangle_nums) and not found:
17        if triangle_nums[iterator] == sum_word:
18            found = True
19            return True
20        iterator += 1
21    return False
22
23words = ""
24count = 0
25for line in fileinput.input("words.txt"):
26    words += line.strip()
27words = words.replace('"', '').split(',')
28for word in words:
29    if is_triangle_word(word):
30        count += 1
31print(count)