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

Hi I am in problem set 3. The code I wrote for question 1 is def countSubStringMatchRecursive(target,key): k=find(target,key) if k==-1: n=0 return n else: m=target[k+1:] n=1+countSubStringMatchRecursive(m,key) return n It only gives me 0 when there's no match or 1 when there is. But cannot give me those more than one. Anything wrong in the code? Thanks a lot~

OpenStudy (anonymous):

For me the code works just fine

OpenStudy (anonymous):

Thanks~I know the problem now When we do string slicing, it does not change. So instead of having m=target[k+1:] I should have m=m[k+1:]

OpenStudy (anonymous):

@verbit is right, the code works fine just the way it is. If you write m = m[k+1:], you will get an error since m has not been defined before. So, it should be m = target[k+1] as mentioned before.

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!