欧拉计划 第42题
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
import sys
fp = open('words.txt')
words = fp.read()
fp.close()
words = words.replace('\n', '')
words = words.replace('"', '')
words = words.split(',')
#print words
num = [0 for i in xrange(27)]
hashtable = {}
for i in xrange(1, 27):
num[i] = (1+i)*i/2
hashtable[num[i] = i
#print num
#print hashtable
cnt = 0
for word in words:
s = 0
for ch in word:
s += (ord(ch) - ord('A') + 1)
if hashtable.has_key(s):
cnt += 1
print cnt
结果:162
#-*- coding:utf-8 -*-
import os
import sys
fp = open('words.txt')
words = fp.read()
fp.close()
words = words.replace('\n', '')
words = words.replace('"', '')
words = words.split(',')
#print words
num = [0 for i in xrange(27)]
hashtable = {}
for i in xrange(1, 27):
num[i] = (1+i)*i/2
hashtable[num[i] = i
#print num
#print hashtable
cnt = 0
for word in words:
s = 0
for ch in word:
s += (ord(ch) - ord('A') + 1)
if hashtable.has_key(s):
cnt += 1
print cnt
结果:162
还没人赞这篇日记