vendredi 15 octobre 2010

Euler #42

#!/usr/bin/env python

def word_value(w):
    return sum(map(lambda x: ord(x) - ord('A') + 1, w))

def triangle_numbers(l):
    res = []
    n = 1
    t = 1
    while t < l:
        t = n*(n+1)/2
        res += [t]
        n += 1
    return res

if __name__ == '__main__':
    f = open('words.txt', 'r')
    words = f.read()
    f.close()
    words = map(word_value, map(lambda x: x[1:-1], words.split(',')))
    triangles = triangle_numbers(max(words))
    n = 0
    for w in words:
        if w in triangles: n += 1
    print n
    

Aucun commentaire:

Publier un commentaire