[Tutor] Algorithm
kreglet
kreglet at gmail.com
Tue Aug 25 21:14:11 CEST 2009
Wayne,
I appreciate your patience with me. I still can't get this to work:
from operator import itemgetter
class testwords:
def __init__(self):
self.lettercount={}
self.inword=False
self.mainword=""
self.cmpword=""
def countletters(word):
lc.lettercount = {}
for letter in word:
lc.lettercount[letter] =lc.lettercount.get(letter,0) + 1
print sorted(lc.lettercount.iteritems(), key=itemgetter(1))
def comparewords(cmpword, mainword):
for letter in cmpword:
if mainword.get(letter):
print letter, cmpword[letter], mainword[letter]
if cmpword[letter] >mainword[letter]:
lc.inword=False
else:
if cmpword[letter] <=mainword[letter]:
lc.inword=True
lc=testwords()
lc.mainword="batty"
lc.cmpword="bat"
countletters(lc.mainword)
mainword = lc.lettercount
countletters(lc.cmpword)
cmpword = lc.lettercount
comparewords(cmpword, mainword)
if lc.inword==True:
print lc.cmpword + " IS in: " + lc.mainword
if lc.inword==False:
print lc.cmpword + " IS NOT in: " + lc.mainword
This is confusing me:
lc.mainword="batty"
lc.cmpword="bat"
[('a', 1), ('y', 1), ('b', 1), ('t', 2)]
[('a', 1), ('b', 1), ('t', 1)]
a 1 1
b 1 1
t 1 2
bat IS in: batty
lc.mainword="batty"
lc.cmpword="byyt"
[('a', 1), ('y', 1), ('b', 1), ('t', 2)]
[('b', 1), ('t', 1), ('y', 2)]
y 2 1
b 1 1
t 1 2
byyt IS in: batty
if I put : if cmpword[letter] <=mainword[letter]:
lc.inword=True
on the same level as the else statement:
lc.mainword="batty"
lc.cmpword="bat"
[('a', 1), ('y', 1), ('b', 1), ('t', 2)]
[('a', 1), ('b', 1), ('t', 1)]
a 1 1
b 1 1
t 1 2
bat IS Not in: batty
Neither is: byyt
Isn't what comes after the else statment to catch if a letter is in the
cmpword that is not in the mainword?
lc.mainword="batty"
lc.cmpword="bst"
KeyError: 's'
Wayne-68 wrote:
>
> On Mon, Aug 24, 2009 at 8:58 PM, kreglet <kreglet at gmail.com> wrote:
>
>>
>> Wayne,
>>
>> > def myfunc(cmpword, mainword):
>> > for letter in cmpword:
>> > if mainword.gets(letter):
>> > if cmpword[letter] >mainword[letter]:
>> > return False
>> > else:
>> > return False
>>
>> I tried your function and couldn't get it to work. It threw an error in
>> the
>> line "if mainword.gets(letter):" saying that "gets" was not an attribute
>> of
>> dictionary. I tried it with "if mainword.get(letter):" -- no s but that
>> would't work either.
>
>
> sorry, 'get' is what I meant. You also need to add "return True" on the
> same
> level as the else.
>
> In [5]: word1 = {'d':1, 'o':1, 'g':1}
>
> In [6]: word2 = {'g':1, 'o':1}
>
> In [7]: in_word(word2, word1)
> Out[7]: True
>
> In [24]: word2 = {'b':1, 'a':1, 'r':1}
>
> In [25]: in_word(word2, word1)
> Out[25]: False
>
> HTH,
> Wayne
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
View this message in context: http://www.nabble.com/Algorithm-tp25107922p25140474.html
Sent from the Python - tutor mailing list archive at Nabble.com.
More information about the Tutor
mailing list