User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 392,071 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,228 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 35653 | Replies: 3
Reply
Join Date: Jun 2006
Posts: 1
Reputation: zx9 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
zx9 zx9 is offline Offline
Newbie Poster

Question word unscrambler

  #1  
Jun 22nd, 2006
hi,
This is my problem i have programmed a little word unscrambler i have a file wordlist.txt in my python folder and this program works just fine and unscrambles one word at a time but i dont how how to edit it to make it unscramble 10 words at a time can someone please help??
thanks
ps i want it to print the solution in this format:
word1, word2, word3,.......all the way to word10
here is my code
ps it prints the same solution multiple times
import string
def anagrams(s):
    if s == "":
        return [s]
    else:
        ans = []
        for an in anagrams(s[1:]):
            for pos in range(len(an)+1):
                ans.append(an[:pos]+s[0]+an[pos:])
        return ans
def dictionary(wordlist):
    dict = {}
    infile = open(wordlist, "r")
    for line in infile:
        word = line.split("\n")[0]
        dict[word] = 1
    infile.close()
    return dict
def main():
    diction = dictionary("wordlist.txt")
    for i in range(10):
        anagram = raw_input("Please enter a words you need to unscramble: ")
        anaLst = anagrams(anagram)
        for ana in anaLst:
            if diction.has_key(ana):
                print "The solution to the jumble is", ana
main()
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: word unscrambler

  #2  
Jun 24th, 2006
What do you want to do if there is no solution?
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: word unscrambler

  #3  
Jun 25th, 2006
I took your code and changed it mildly to give you an idea how to proceed ...
# unscramble a line of words

def permutation(s):
    if s == "":
        return [s]
    else:
        ans = []
        for an in permutation(s[1:]):
            for pos in range(len(an)+1):
                ans.append(an[:pos]+s[0]+an[pos:])
        return ans
    
def dictionary(wordlist):
    dict = {}
    infile = open(wordlist, "r")
    for line in infile:
        word = line.split("\n")[0]
        # all words in lower case!!!
        word = word.lower()
        dict[word] = 1
    infile.close()
    return dict

def main():
    diction = dictionary("wordlist.txt")

    # enter all the words that fit on a line or limit the number
    anagram = raw_input("Please enter space separated words you need to unscramble: ")
    wordLst = anagram.split(None)

    for word in wordLst:
        anaLst = permutation(word)
        for ana in anaLst:
            if diction.has_key(ana):
                diction[ana] = word
                #print "The solution to the jumble is" , ana
    solutionLst = []
    for k, v in diction.iteritems():
        if v != 1:
            solutionLst.append(k)
            print "%s unscrambled = %s" % (v, k)
    print solutionLst

main()
You could limit the scrambled words entered with a loop.
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: word unscrambler

  #4  
Aug 5th, 2006
I liked your recursive anagram creator, but it is expensive because it generates n! strings for a size n scramble. I tested it with

print anagrams("Hi There")
and my reasonably fast machine cried and locked up. :lol:

Another possibility would be to test whether your scramble is an anagram for each word in the list. You could do this by seeing whether each letter in the scramble is somewhere in the word, removing that letter from the word if found. Here's the idea:

def isanagram(source, target):
    """returns True if source is an anagram of target, False    
    otherwise."""
    
    # speed-up if your dictionary is large:
    # if len(source) != len(target): return False

    for i in source:
        pos = target.find(i)
        if pos == -1:
            return False
        else:
            # remove found letter from the target (non-destructively)
            target = target[:pos]+target[pos+1:]
    return True

In the worst case, this runs O(n^2 * length of dictionary), but with much better performance on average. I found a noticeable speed-up just on this: print isanagram("Hi There", "There Hi")

hope it helps,
Jeff Cagle
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 12:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC