Hey guys I finally got my word unscrambler working the other day, now I fire it up with same code same everything and for some reason getting double output from a list that shouldn't be. So if someone could either look this over and find a problem or maybe do a test run on their computer and let me know if something is currupt somehow on my system that would be great to. I just can't tell if its the code or my computer or the interprater that is messed up. I'll attach some sample files to use to run it. Thanks again.

Here is the code.

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
thelist = []
myvar = -1
c = -1
p = -1
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')
#add file contents to lists.
while p < 9:
    i = -1
    p = p + 1
    print p
    for line in wordlistfile:
        wordlist.append(line.strip())
    for line in possiblefile:
        possible.append(line.strip())
#match length of words to narrow possible matches
    for line in wordlist:
        i = i + 1
        if len(wordlist[i]) == len(possible[p]):
            matches.append(wordlist[i])
#narrow further by checking for instances of charecters
    word = possible[p]
    x = len(matches)
    while c < x:
        c = c + 1
        for letter in word:
            if c == x:
                break
            if not letter in matches[c]:
                del matches[c]
                x = x - 1
#start rearanging word.
    word = word.strip().lower()
    for pos in matches:
        tmp1 = []
        tmp2 = []
        tmp1 = list(pos.lower().strip())
        tmp2 = list(word)
        tmp1.sort()
        tmp2.sort()
        if tmp1 == tmp2:
            print p
            myvar = myvar + 1
            thelist.append(pos)
            if myvar == 10:
                print(thelist[myvar])
                break
            elif myvar < 9:
                print(thelist[myvar] + ', ')

wordlistfile.close()
possiblefile.close()

On first blush, you need to create your wordlist outside the loop, or you will keep appending to it all the time:

#decode scrambled word from a word list in 30 sec or less.
matches = []
thelist = []
myvar = -1
c = -1
p = -1
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')
#add file contents to lists.
wordlist = []
possible = []
for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())

while p < 9:
    i = -1
    p = p + 1
    print p
    for line in wordlistfile:
        wordlist.append(line.strip())
    for line in possiblefile:
        possible.append(line.strip())
#match length of words to narrow possible matches
    for line in wordlist:
        i = i + 1
        if len(wordlist[i]) == len(possible[p]):
            matches.append(wordlist[i])
#narrow further by checking for instances of charecters
    word = possible[p]
    x = len(matches)
    while c < x:
        c = c + 1
        for letter in word:
            if c == x:
                break
            if not letter in matches[c]:
                del matches[c]
                x = x - 1
#start rearanging word.
    word = word.strip().lower()
    for pos in matches:
        tmp1 = []
        tmp2 = []
        tmp1 = list(pos.lower().strip())
        tmp2 = list(word)
        tmp1.sort()
        tmp2.sort()
        if tmp1 == tmp2:
            print p
            myvar = myvar + 1
            thelist.append(pos)
            if myvar == 10:
                print(thelist[myvar])
                break
            elif myvar < 9:
                print(thelist[myvar] + ', ')

wordlistfile.close()
possiblefile.close()

O.K. got that and seems reasonable enough, but still getting this output from my p variable first call from before goes into the lists secound after the output of the lists.

0
0
larson, 
1
1
digital1, 
2
2
apache, 
3
3
reggie, 
3
reggie, 
4
4
joanna, 
5
5
arthur, 
6
6
saturn, 
7
7
ncc1701, 
8
8
9
9
angela

As you can see when it gets to reggie for some reason there is one extra ouput which throws the whole thing out of whack!!! It really shouldn't do that but still is.

There are two "print p" in your code.

Sorry, I left the original

for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())

still in the loop. Just remove it. On first blush meant that I didn't test it out, just looked at your code.

Why it doesn't print out 'friends', I don't know.

Yeah I did remove the appends from the loop and that output I posted is just showing that the lists seem to be giving a double output on item 3 reggie thus screwing the indexing up tward the bottom of the code, but it shouldn't be the way its coded. Line 13 in the output should never happen, and I think that is why the rest of it don't work right either. If anyone can tell me why it is happening or come up with some type of coding to stop it from doing that would be very helpful.

By using set() you can really streamline your code:

# modified to allow testing
wordlistfile = open('wordlist.txt', 'r')
possiblefile = open('possible.txt', 'r')

# add file contents to lists
wordlist = []
possible = []
for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())

match_list = []
for poss in possible:
    for word in wordlist:
        # narrow down the wordlist with len() and set()
        if len(poss) == len(word):
            if set(poss) == set(word):
                # now partial match the characters
                count = 0
                for c in poss:
                    if c in word:
                        count += 1
                if count == len(poss):
                    match_list.append((poss, word))

# show result
print "there are %d matches:" % len(match_list)
for match in match_list:
    print "%-10s -->  %-10s" % (match)

"""
my result -->
there are 10 matches:
raolns     -->  larson
1itdalig   -->  digital1
caeaph     -->  apache
eeirgg     -->  reggie
nanajo     -->  joanna
ahtrru     -->  arthur
sunatr     -->  saturn
cc0171n    -->  ncc1701
rfisedn    -->  friends
aangle     -->  angela
"""

A faster way

wordlistfile = open('wordlist.txt', 'r')
possiblefile = open('possible.txt', 'r')

D = {}
for line in wordlistfile:
  word = line.strip()
  s = "".join(sorted(word))
  if not s in D:
    D[s] = []
  D[s].append(word)

for line in possiblefile:
  scrambled = line.strip()
  s = "".join(sorted(scrambled))
  if s in D:
    print scrambled, sorted(D[s])

:)

Thanks for pointing out the set module I just did some research on it and its great. Seems strange to me though that sort was acting the way it was, although I notice by looking at your code I need to work on my grammer as I put it in another post. I'm workin on it!!! Anyways I think I'll modify the program to use set instead of sort for now, but I wanna find out why sort not working correctly, so I know how to use it in the future. Oh and thanks for the hint on appending the lists to Gribouillis.

You want it real sweet and short?

# add file contents to lists
wordlist = [line.strip() for line in file('wordlist.txt')]
possible = [line.strip() for line in file('possible.txt')]

match_list = []
for poss in possible:
    for word in wordlist:
        # narrow down the wordlist with len() and set()
        if len(poss) == len(word) and set(poss) == set(word):
                match_list.append((poss, word))

# show result
print "there are %d matches:" % len(match_list)
for match in match_list:
    print "%-10s -->  %-10s" % (match)

Pulling all the tricks of Python, but harder to read and understand:

for poss in [line.strip() for line in file('possible.txt')]:
    for word in [line.strip() for line in file('wordlist.txt')]:
        if len(poss) == len(word) and set(poss) == set(word):
                print "%-10s -->  %-10s" % (poss, word)

ZZucker could you explain to me where or how the variables like %d are getting there information from I have never used that type of variable before.

O.K. I'm understanding that it is used to convert between types, but what is the -10 in %-10s for? Wouldn't you only need to do %s?

O.K that makes a lot more sense now, so the -10 is a flag you can set. Didn't quite catch that at first, thanks for the links to that information.

print "%-10s --> %-10s" % (poss, word)
"%-10s --> %-10s" is a formatting string where %s is a place holder for a string and %-10s means put the string into a 10 space field left adjusted. Conversely %d is a placeholder for an integer and %10.3f would be a placeholder for a float number putting it into a 10 space field with 3 decimals showing.

Look under % formatting in the manual. It works similar to the C language printf().

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.