Does '#hash' mean anything in IDLE?
John Coleman
jcoleman at franciscan.edu
Fri Mar 3 09:06:26 EST 2006
John Salerno wrote:
> John Coleman wrote:
> > John Coleman wrote:
> >> Greetings,
> >> I am currently trying to learn Python through the excellent
> >> "Learning Python" book.
>
> me too!
>
> > It isn't just #hash, but also things like #dict, #int, #len at the
> > start of a comment line which defeats IDLE's colorization algorithm.
> > Interestingly, things like #while or #for behave as expected so it
> > seems to be built-ins rather than keywords which are the problem. To
> > answer my own question - this is pretty clearly a (harmless) bug.
>
> also notice that putting a space after # stops the problem
How do you like Python so far? I like dictionary objects the best so
far. I'm coming to Python from VBScript, so I already knew the value of
such things, but in Python they are better supported.
Here is the program I was talking about, which *really* shows the power
of dictionaries:
*****************************************************************************************
#Python program to discover word with most 1-word anagrams
#The following hash function has the property
#that words which are anagrams of each other
#hash to the same string. It assumes that input
#is lower case in range 'a' to 'z'
def letter_hash(word):
codes = 26 * [0]
for c in word:
codes[ord(c)-97] +=1
return_string = ''
for i in range(26):
j = codes[i]
if j > 0:
return_string += (str(j)+chr(i+97))
return return_string
#main program:
hashes = {}
#first load dictionary of hashes
for line in open('C:\\yawl.txt').readlines():
word = line.strip().lower() #to be safe
my_hash = letter_hash(word)
hashes.setdefault(my_hash,[]).append(word)
#now find word with most anagrams
max_len = 0
max_words = []
for word_list in hashes.itervalues():
if len(word_list) > max_len:
max_len = len(word_list)
max_words = word_list
print max_words
**********************************************************
"yawl" stands for "Yet Another Word List". It is a text-list of some
240,000 English words, including all sorts of archaic and technical
phrases. Google for "yawl word list" if you want to track down a copy.
The output is
['apers', 'apres', 'asper', 'pares', 'parse', 'pears', 'prase',
'presa', 'rapes', 'reaps', 'spaer', 'spare', 'spear']
These 13 words are anagrams of each other. They contain some pretty
obscure words: asper is a 17th century Turkish coin and spaer is an
archaic Scottish-dialect word word for prophet (you can see "speaker"
if you squint).
-John Coleman
More information about the Python-list
mailing list