# # Latimer, 9.02.2009 # # Frequency Count, Version 1.0 # # Input: dictionary of six-letter words # Process: determine the frequency of each letter across the entire dictionary # Output: each letter A-Z and their total number of appearances in all words # #from string import lowercase def read_words(fname): # infile=open(fname) # temp=infile.read() # infile.close() # return temp.split('\n')[:-1] infile=open(fname) templist=[] word=infile.readline() while word: templist.append(word[0:len(word)-1]) # strip off the end of line '\n' from the end of word word=infile.readline() return templist def main(): wlist=read_words('words.txt') lowercase = "abcdefghijklmnopqrstuvwxyz" # ht=dict(zip(lowercase,[0]*26)) # hash table ht={} #hash table for i in range(26): ht[lowercase[i]] = 0; print "hash table(dictionary) initialized: %s" % ht for w in wlist: for ch in w: ht[ch]+=1 for ch in lowercase: # in Windows use lowercase[:26] instead print '%s %4d'%(ch,ht[ch]) if __name__=='__main__': main() # # Notes # ----- # 1. Loop over words in dictionary (5K), loop over letters in word (6), # increment count for that letter (30K total) # 2. Loop over letters (26), loop over all words (5K), loop over letters # in word (6), count occurrences of current letter (780K total) #