Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

Hey all, I'm working on problem set 3 problem 1 and I'm having a bit of difficulty. With countSubStringMatch(target, key) I can get it to count, but it just returns the amount of characters in the list and not how many times the sublist shows up. In countSubStringMatchRecursive(target, key) I get this error message: "TypeError: 'int' object is not subscriptable" My code is here: http://dpaste.com/hold/672710/ Any suggestions?

OpenStudy (anonymous):

my code is as follow: def countSubStringMatch(target,key): index = find(target,key) if index == -1: return 0 else: count = 0 while index != -1: print "index = ",index index = index+1 index = find(target,key,index) count=count+1 return count

OpenStudy (anonymous):

def countSubStringMatch(target, key): count = 0 ans = find(smattering, selection) if ans == -1: print "False" else: while find(smattering, selection) >= 0 and count <= len( smattering ): find(smattering, selection, ans + 1) if ans >= -1: count = count + 1 print count i believe the problem is that you should use stick to the variable "target" and "key" rather than smattering and selection, because of the scoop here in this function. secondly, you may consider using "return" rather than "print", since this is a function

OpenStudy (anonymous):

number of problems with the function (which fung mentioned, especially about using return) but here's the source of the error message you are getting: in your code line 33: count = count + countSubStringMatchRecursive( smattering, selection[1[l]] ) at the very right, you are subscripting an integer object: selection[1 [l]] try using Python's slice operators instead if you are trying to get a part of the source string

OpenStudy (anonymous):

to be clear, inside the selection subscript you are subscripting '1' by l (things get quite confusing if you use a single lowercase l as an identifier, since l and 1 look so similar)

OpenStudy (anonymous):

Thanks Fung! I worked on my code and was able to get it to spit out the positions, though I'm still working on figuring out how to have it spit out the actual count. It's definitely working though. Thank you also to you, agdgdgdgwngo, Thats really helpful as well, I'm going to have to try a slice instead, though what I'm trying to do is have sort of a sliding window except that it will only ever test out the first however many characters and then get rid of the first character and carry on (if that makes sense). I'll have to mess around with it.

OpenStudy (anonymous):

Would taking slices of something like that be considered recursive?

OpenStudy (anonymous):

You can perform slice operations as part of your recursive computations.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!