Hi, i don't know how to start with this problem.

Lets say i have 6 random letters in any order, "mnador" and i need to find all words those letters can make e.g. "random", "roam", "man", "and" etc. from a wordlist.txt that i have.

Any tips?

Recommended Answers

All 14 Replies

Member Avatar for Mouche

One way to approach it is analyze how many letters are in your scrambled word. Then go through the wordlist, and analyze how many letters are in each word. If the number of a certain letter in the scrambled word is equal to or greater than the number for the wordlist word, then you can make that word out of the scrambled word's letters.

other way is to remove one by one the letter from the copy of the usable letters, succeed if all were found, otherwise fail at first letter not found. Maybe you want to check first that length of the target word is shorter than usable letter string.

Thanks alot, think i can get started now atleast.

Found a solution for my problem with this code, but i don't think i understand this line fully chkword=chkword.replace(letter, '', 1) what does the "1" do? can you do it without the use of a function? is there other easy ways you can solve this?

def anagramchk(word,chkword):  
    for letter in word:  
        if letter in chkword:  
            chkword=chkword.replace(letter, '', 1)  
        else:  
            return False  
    return True
  
wordin=raw_input('Enter anagram:')  
f=open('wordlist.txt', 'r')  
for line in f:  
    line=line.strip()
    if anagramchk(line,wordin): 
        print line
f.close()
Member Avatar for Mouche
#str.replace(old, new[, count])
#Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Reference

So the following code essentially removes the given letter from chkword because it replaces ONE instance of the letter in chkword with '', which is nothing.

>>> chkword = "hello"
>>> letter = "l"
>>> chkword = chkword.replace(letter, '', 1)
>>> print chkword
helo

As the documentation shown above says, the 1 means only replace one of the instances of the letter in the string chkword.

If you had the following code without the 1, then your resulting chkword would be "heo". Both "l"s would be removed because the default behavior without the count parameter is to replace all instances of the given letter.

>>> chkword = "hello"
>>> letter = "l"
>>> chkword = chkword.replace(letter, '')
>>> print chkword
heo
commented: well explained +13

Thanks for sorting that out.

Is it is it possible to do the same but without a function? If you take the functions loop and insert it at line 13? can't seem to get it to work then.

Then you can not have return but must break out of loop appropriately.

Can't get it to work, thought it would be easier for a beginner like me to do it without a function. And easier to understand. Think i need some more help to do it without a function, so I can end this project :)

I think it's pretty important to incorporate functions, ESPECIALLY since you're a newbie. It wasn't that long ago that I was wrapping my head around functions. They're great because the can be used more than once, it's also a quick concept to begin to understand, it's simply a way of doing a given task. For example if I wanted to print out the same ending to a string for different strings I could write something like

def grade_name(name):
    name=name.capitalize()
    if name[0:4]=='Josh':
        name+=' is awesome.'
        return name
    elif name[0:4]=='Tony':
        name+=' is a great teacher'
        return name
    else:
        name+=' is using this program'
        return name

now instead of writing this all over the place I could simply do something like:

coming_in=input('Enter a name: ')
grade_name(coming_in)

Can't you do it by taking the file with the wordlist, using the "readlines" method and inside a "for" loop, split the word into individual characters (with them being in a list of course), then append that list into a predefined empty list created prior? Then all you would have to do is split the anagram into individual characters, count the occurrences of the letters, do the same for the word lists (inside the list) inside of another "for" loop, and see if they match. Sorry if I come off as hard to understand, I'm kind of answering this question in a free-writing kind of way.

Can't you do it by taking the file with the wordlist, using the "readlines" method and inside a "for" loop, split the word into individual characters (with them being in a list of course), then append that list into a predefined empty list created prior? Then all you would have to do is split the anagram into individual characters, count the occurrences of the letters, do the same for the word lists (inside the list) inside of another "for" loop, and see if they match. Sorry if I come off as hard to understand, I'm kind of answering this question in a free-writing kind of way.

If you do it this way, you wouldn't need a function. I think you could still use the sys module to allow for scripting of this process as well (i.e., run it through a terminal or cmd on Windows) by converting the user input to a string.

Can't you do it by taking the file with the wordlist, using the "readlines" method and inside a "for" loop, split the word into individual characters (with them being in a list of course), then append that list into a predefined empty list created prior? Then all you would have to do is split the anagram into individual characters, count the occurrences of the letters, do the same for the word lists (inside the list) inside of another "for" loop, and see if they match. Sorry if I come off as hard to understand, I'm kind of answering this question in a free-writing kind of way.

Thanks I will try that.

Sorry can't take this stupid stomping around (assuming that you are only interesting which are exact anagram, not words that can be part of anagrams):

def is_anagram(base, word):
    return base == sorted(c for c in word if c.isalpha())

anagram_base = sorted(c for c in input('Give words for anagram: ').lower() if c.isalpha())
words = ['item', 'semi', 'similar', 'emit', 'dew', "we'd"]
print('Anagrams are: %s' % ', '.join(word for word in words if is_anagram(anagram_base, word)))


""" use:
Give words for anagram: edw
Anagrams are: dew, we'd
"""

If you need to be efficient you would do it once for all words and be done with it:
One word anagrams by lookup

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.