[Tutor] Algorithm

kreglet kreglet at gmail.com
Tue Aug 25 03:58:18 CEST 2009


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. 

I've been playing with this most of the day and this is what i came up with:


from operator import itemgetter
class letters:
	def __init__(self):
            self.lettercount={}
	    self.inlist=False
	    self.inword=False
	    self.mainword=""
	    self.cmpword=""
				
lc=letters()

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))

lc.mainword="batty"
lc.cmpword="batyy"

countletters(lc.mainword)
mainword = lc.lettercount

countletters(lc.cmpword)
cmpword = lc.lettercount

lst=[]
for key in cmpword.keys():
	for ky in mainword.keys():
		if key<>ky:
			pass 
		else:
			lst.append(key)

for key in cmpword.keys():
	if key not in lst:
		lc.inlist=False
		break
	else:
		lc.inlist=True      

# At this point the program stops if a letter is in cmpword that is not in
mainword
# What I try to do next is compare the values  of the dictionary keys( this
is where I'm getting confused)
# get keys/values from cmpword  
# compare the values of the keys from mainword  that match the keys from
cmpword
# if there is a key in mainword that is not in cmpword, ignore it
# if a key is the same in both compare the values 
#if the value of a key that is in cmpword is greater the the value of the
corresponding key in mainword then cmpword would not be in mainword
----------------------------------------------------------
cmpkeys=[]
cmpvals=[]
if lc.inlist==True:
	for key, val in cmpword.items():
		cmpkeys.append(key)
		cmpvals.append(val)
	for a in range(len(cmpkeys)):
		for ky, vl in mainword.items():
			
			if cmpkeys[a]==ky:  #?????
				if cmpvals[a]>vl:  #????   
					lc.inword=False
				else:
					lc.inword=True
			print cmpkeys[a], cmpvals[a]	, ky, vl
 

print 
if lc.inword==True:
	print lc.cmpword + " is IN: " + lc.mainword
else:
	print  lc.cmpword + " is Not in: " + lc.mainword


Lol, it aint pretty, and it almost works. If you change the letters in
lc.cmpword you'll see what I mean.
There's got to be another way :)



cheers,
kreglet


Wayne-68 wrote:
> 
> 
> 
> That's correct - your function countletters returns None, so you're
> assigning cmpword to None. And since None is not an iterable type, it
> throws
> an error. Try returning a value like I mentioned above and see how that
> works.
> 
> HTH,
> Wayne
> 
> 

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



More information about the Tutor mailing list