mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Problem 042
git-svn-id: file:///srv/svn/euler@68 e5f4c3ec-3c0c-11df-b522-21efaa4426b5
This commit is contained in:
parent
8ab85e4082
commit
bd19185828
2 changed files with 50 additions and 0 deletions
49
e042.py
Normal file
49
e042.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""How many triangle words does the list of common English words contain?
|
||||
|
||||
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
|
||||
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
|
||||
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
|
||||
|
||||
Using words.txt (p042/words.txt), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
|
||||
"""
|
||||
|
||||
import csv
|
||||
|
||||
def triangle_generator():
|
||||
i = 0
|
||||
n = 0
|
||||
while True:
|
||||
i = i + 1
|
||||
n = n + i
|
||||
yield n
|
||||
|
||||
def main():
|
||||
word_list = []
|
||||
reader = csv.reader(open('p042/words.txt'), delimiter=',', quotechar='"')
|
||||
for row in reader:
|
||||
word_list = word_list + row
|
||||
word_list = sorted(word_list)
|
||||
|
||||
total = 0
|
||||
words = {}
|
||||
for word in word_list:
|
||||
score = 0
|
||||
for c in word:
|
||||
score = score + (ord(c) - 64)
|
||||
words[word] = score
|
||||
|
||||
max_score = max(words.values())
|
||||
triangles = []
|
||||
for i in triangle_generator():
|
||||
if i > max_score:
|
||||
break
|
||||
triangles.append(i)
|
||||
for (word, score) in words.iteritems():
|
||||
if score in triangles:
|
||||
total = total + 1
|
||||
print 'Total:', total
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
1
p042/words.txt
Normal file
1
p042/words.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue