Hello Kent,

Yes I would like to see your implementation. It seems to work but I get
error at the end.

concat=''.join

mainword="python"
cmpword="pot"
 # sort the letters in the target word; for example 'python' becomes
'hnopty'
tmplst=[]
for letters in mainword:
        tmplst.append(letters)

tmplst.sort()
mainword=concat(tmplst)
# sort the letters in the word you want to test, for example 'pot' becomes
'opt'
tmplst=[]
for letters in cmpword:
        tmplst.append(letters)

tmplst.sort()
cmpword=concat(tmplst)

inword=False
# walk through test letters looking for them in the target list. 
print cmpword, mainword
for letter in range(len(cmpword)): #Test
        for let in range(len(mainword)):#Target
                print cmpword[letter], mainword[let],
                # if it matches the current target letter, go to the next 
letter in each
list
                if cmpword[letter]==mainword[let]:
                        print "Match: "
                        let +=1
                        letter +=1
                # if it is less than the current target letter, go to the next 
target
letter
                if cmpword[letter]<mainword[let]:
                        print "No Match: "
                        let+=1
                #         - if the test letter is greater than the target 
letter, or you run
                #           out of target letters, there is no match
                if cmpword[letter]>mainword[let]:       
                        print "No Match:"
                inword=False
        inword=True


if inword==True:
        print cmpword + " is IN: " + mainword
else:
        print cmpword + " is NOT in: " + mainrowd


opt hnopty
o h No Match:
o n No Match:
o o Match: 
p p Match: 
t t Match: 
Traceback (most recent call last):
  File "/home/kreglet/bin/knt.py", line 45, in <module>
    if cmpword[letter]<mainword[let]:
IndexError: string index out of range




Kent Johnson wrote:
> 
> 
> Another way to do this:
> - sort the letters in the target word; for example 'python' becomes
> 'hnopty'
> - sort the letters in the word you want to test, for example 'pot' becomes
> 'opt'
> - walk through test letters looking for them in the target list. For
> each test letter,
>   - if it matches the current target letter, go to the next letter in each
> list
>   - if it is less than the current target letter, go to the next target
> letter
>   - if the test letter is greater than the target letter, or you run
> out of target letters, there is no match
>   - if you get to the end of the test letters, you have a match
> 
> The code for this is pretty simple and doesn't require much in the way
> of data, just the two lists of letters. If you like I can share my
> implementation.
> 
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Algorithm-tp25107922p25142030.html
Sent from the Python - tutor mailing list archive at Nabble.com.

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to