-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem42.py
More file actions
38 lines (30 loc) · 809 Bytes
/
Copy pathProblem42.py
File metadata and controls
38 lines (30 loc) · 809 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
35
36
37
38
# Prompt: Using {assets/p042_words.txt}..., how many are triangle words?
#
# Execute: python Problem42.py
#
# Answer: 162
import math
# constant
abc = [0, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U" ,"V", "W", "X", "Y", "Z"]
# Evaluate inverse function of triangle. If result is an integer, n is triangular
def iT(n):
t = ((math.sqrt(8 * n + 1) - 1) / 2)
return t == int(t)
def wordToInt(w):
sum = 0
for c in w:
sum += abc.index(c)
return sum
# Read file into string f
f = open("assets/p042_words.txt", "r")
f = f.read()
# Ignore first and last quote in string
f = f[1:-1]
# Split string into array
f = f.split("\",\"")
# Counter of occurrences of triangular numbers
c = 0
for w in f:
if iT(wordToInt(w)):
c += 1
print c