anagram finder / dict mapping question
Kam-Hung Soh
kamhung.soh at gmail.com
Thu May 8 03:33:36 EDT 2008
On Thu, 08 May 2008 15:42:07 +1000, dave <squareswallower at 1ya2hoo3.net>
wrote:
> This is what i've came up with. My problem is that I can't get them to
> properly evaluate.. when comparewords() runs it finds itself... Should
> I have the keys of mapdict iterate over itself? Is that possible?
>
> def annafind():
> fin = open('text.txt') # file has one word per line
> mapdic = {} # each word gets sorted & goes in here
> for line in fin:
> rawword = line.strip()
> word = list(rawword)
> word.sort()
> mapdic[''.join(word)] = 0
> return mapdic
>
>
> def comparewords(): ***not working as intended
> fin = open('text.txt')
> for line in fin:
> line = line.strip()
> word = list(line)
> word.sort()
> sortedword = (''.join(word))
> if sortedword in mapdic:
> print line
>
>
>
>
> On 2008-05-07 19:25:53 -0600, "Kam-Hung Soh" <kamhung.soh at gmail.com>
> said:
>
>> On Thu, 08 May 2008 11:02:12 +1000, dave <squareswallower at 1ya2hoo3.net>
>> wrote:
>>
>>> Hi All,
>>> I wrote a program that takes a string sequence and finds all the words
>>
>>> inside a text file (one word per line) and prints them:
>>> def anagfind(letters): #find anagrams of these letters
>>> fin = open('text.txt') #one word per line file
>>> wordbox = [] #this is where the words will go
>>> for line in fin:
>>> word = line.strip()
>>> count = 0
>>> for char in letters:
>>> if char not in word:
>>> break
>>> else:
>>> count += 1
>>> if count == len(word):
>>> wordbox.append(word)
>>> return wordbox
>>> Now I'd like to modify the code to naturally find all anagrams inside
>> a
>>> wordlist. What would be the best way to do this? Using Hints? Is it
>>
>>> possible to iterate over dict keys? How can I make a dict that maps
>>
>>> from a set of letters to a list of words that are spelled from those
>>
>>> letters? Wouldn't I need to make the set of letters a key in a dict?
>>> As always - Thanks for helping someone trying to learn...
>>> Dave
>>>
>> Suggestion: for each word, sort their characters and use them as the
>> dictionary key. If two words have the same combination of characters,
>> then they are anagrams. For example: "edam" and "made" are anagrams
>> because they have the letters 'a', 'd', 'e' and 'm'.
>> Refer "Programming Pearls" by Jon Bentley.
>> --
>> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
>> a>
>
>
Your code is always going to return the same list because every word is an
anagram of itself.
Tip: Create a list for each dictionary key, then add a word to the list if
that word is not in the list. So:
mapdic('adem') --> ["edam", "made"]
P.S. When replying, the convention is to add your response to the bottom,
not top of the message.
--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
More information about the Python-list
mailing list