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

i am working on homework 3 problem 1. I wrote this code: from string import * def countSubStringMatch(target,key): limit=len(target) index=-1 count=0 while index <(limit): count=count+1 index=find(target,key,index+1) if index==-1: break print index,count, indexer countSubStringMatch('gsctsg7gcatsg7gcatgg7','7')

OpenStudy (anonymous):

the code works, but I am having trouble with the recursive version of the problem. What should the difference be?

OpenStudy (anonymous):

here is my code , take a look at it .

OpenStudy (anonymous):

or if u dont want to look the code, i give you a hint: you need to call the same function(as you anyway do in recursive function) with now sliced target and same key.

OpenStudy (anonymous):

b3n, i took a look at your recursive function and I cant get it to work.

OpenStudy (anonymous):

If you just copy-pasted the recursive function instead of the whole thing, you also need to import the string functions, from string import * It worked fine for me.

OpenStudy (anonymous):

Also, B3N, in this part of your recursive script (target[i+1:len(target)], key) you can leave out the len(target), it will slice just the same. Not sure if you knew that already or if you left it in for jpkita.

OpenStudy (anonymous):

i took string import with it. Iterative one works. Recursive doesn't produce output.

OpenStudy (anonymous):

@jpkita I checked it again, it is working fine. Is there any error you getting? @Disco thanks for pointing it out. I knew it but since new to python i tend to forget it. thanks once again.

OpenStudy (anonymous):

i don't get an error. it loads i just don't get output for the recursive one.

OpenStudy (anonymous):

I can't figure out why it wouldn't be working for you. I have attached what I get when I use his code. It doesn't do this?

OpenStudy (anonymous):

ok. i got it to work. How come I can only get it to print when I call it from the Python Shell. On the other ones I can just embed a call function in the code and it printed when I run the program.

OpenStudy (anonymous):

I think it handles print and return commands differently. A print command will print something in the shell regardless of where the function is called from, while a return command will only try to give an output to whatever called it. Since it was called from whatever .py file it was saved in, it was only sending an output back to the .py file, not the shell. At least, that's what I think is happening.

OpenStudy (anonymous):

ok. It was confusing cause I always test inside the code. Thanks for the help

OpenStudy (anonymous):

Finished up problem 3.

OpenStudy (anonymous):

OpenStudy (anonymous):

## screw attachments from string import* def subStringMatchExactlyOneSub(target,key): exactMatch=subStringMatchExact(target,key) shiftedMatch=subStringMatchOneSub(target,key) answers=list(shiftedMatch) for x in shiftedMatch: for y in exactMatch: if x==y: answers.remove(x) return tuple(answers) def subStringMatchExact(target,key): limit=len(target) index=-1 count=0 answerholder=[] while index <(limit): index=find(target,key,index+1) if index!=-1: answerholder.append(index) count=count+1 else:break output=tuple(answerholder) return output def constrainedMatchPair(firstMatch,secondMatch,length): m=length keeper=[] for n in firstMatch: for k in secondMatch: if n+m+1==k: keeper.append(n) return tuple(keeper) ### the following procedure you will use in Problem 3 def subStringMatchOneSub(target,key): """search for all locations of key in target, with one substitution""" allAnswers = () for miss in range(0,len(key)): # miss picks location for missing element # key1 and key2 are substrings to match key1 = key[:miss] key2 = key[miss+1:] print 'breaking key',key,'into',key1,key2 # match1 and match2 are tuples of locations of start of matches # for each substring in target match1 = subStringMatchExact(target,key1) match2 = subStringMatchExact(target,key2) # when we get here, we have two tuples of start points # need to filter pairs to decide which are correct filtered = constrainedMatchPair(match1,match2,len(key1)) allAnswers = allAnswers + filtered return allAnswers

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!