[Tutor] Algorithm

kreglet kreglet at gmail.com
Mon Aug 24 17:48:23 CEST 2009


Wayne,
   
The reason I used print sorted is that using just print throws a syntax
error:

print (lettercount.iteritems(), key=itemgetter(1)) ---> error
print lettercount.iteritems(), key=itemgetter(1) ---> error
print sorted(lettercount.iteritems(), key=itemgetter(1)) ---> works

I don't know why. Seems to me that any of the above should work.

mainword = countletters('batty')
cmpword = countletters('bat')

myfunc(cmpword, mainword)

Generates error:

Traceback (most recent call last):
  File "/home/kreglet/bin/test.py", line 23, in <module>
    myfunc(cmpword, mainword)
  File "/home/kreglet/bin/test.py", line 13, in myfunc
    for letter in cmpword:
TypeError: 'NoneType' object is not iterable

mainword = countletters('batty')
print mainword returns None

cmpword = countletters('bat')
print cmpword returns None

Both mainword and cmpword are passed to the function but since the values of
each are None
the function doesn't work.

Is this correct?

thanx,
kreglet


Wayne-68 wrote:
> 
> On Sun, Aug 23, 2009 at 10:01 PM, kreglet <kreglet at gmail.com> wrote:
> 
> I would actually not bother sorting your return from countletters - keep
> it
> a dictionary.
> 
> Then you can compare like this:
> 
> mainword = countletters('batty')
> cmpword = countletters('bat')
> 
> def myfunc(cmpword, mainword):
>   for letter in cmpword:
>     if mainword.gets(letter):
>         if cmpword[letter] >mainword[letter]:
>              return False
>     else:
>         return False
> 
> I think that should work. First you're looping over each letter in
> cmpword.
> Because mainword is also a dictionary the order isn't terribly important.
> Then you check if the letter is in mainword. If it's not, obviously
> cmpword
> isn't in mainword, so return False. If the letter is, compare the counts.
> If
> cmpword has more letters than mainword, it's not in the word so again
> return
> 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-tp25107922p25118434.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list