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

from string import * def subStringMatch1(target,key): i=0 c=0 tup=() while c!=-1: c = find(target,key,i) if c!=-1: tup = tup + (c,) # print c i=c+1 return tup def newFunction(target,key): length=len(key) for i in range(0,length): #print i key1=key[0:i] key2=key[i+1:length] starts1=subStringMatch1(target,key1) starts2=subStringMatch1(target,key2) ## m=len(starts1) ## for s in range(0,m): ## for z in range(0,m): # print starts1

OpenStudy (vaboro):

Your subStringMatch1() is great! Yet, what if key == ""? Should the function return an empty tuple or the target?

OpenStudy (anonymous):

Thanks ! you mean I should apply a some defensive programming, right ? one more problem I have is that I would need to write the next function with (target) as a tuple. how do I have a function expect a tuple ?

OpenStudy (vaboro):

I don't think it's defensive programming. I think it's conceptual. I would even call it a bug because I figured this question out when I started implementing further functions. Just imagine: if you are searching for 'nothing' why should the function return 'nothing'? I should probably return 'the whole'. This concept was not obvious to me until I started on the later problems of ps3, and so my initial StringMatch function returned an empty tuple when it searched the target for an empty key. A tuple can be passed to a function as an argument. Did I understand you question correctly? You are confused that you will have to pass a tuple as an argument to a function, right?

OpenStudy (anonymous):

hmm, that makes sense. And yes. The next step in problem 3 was to create a def that would take 2 tuples as as arguments. I'm a little confused here although the answer is probably simple... right .. ?

OpenStudy (vaboro):

you can refer to elements of a tuple the same way you would refer to elements of a list, for example, if tuple = (1,2,3,4) then tuple[0] would equal 1, tuple[1] would equal 2 and do forth.

OpenStudy (anonymous):

how do you use it a function (where you don't know how long the tuple is? def constrainedMatchPair(tuple1,tuple2,len) ? so when I actually try to execute it, would it look like : constrainedMatchPair(1,2,3,4) ? or constrainedMatchPair((1,2),(3.4),5) ?

OpenStudy (anonymous):

I got it ...... easy. Is it possible that they have an error in their function ? I'm gettıng an error here 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) print match1,match2 # 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 print 'match1',match1 print 'match2',match2 print 'possible matches for',key1,key2,'start at',filtered return allAnswers

OpenStudy (anonymous):

nevermind I've got it :P

OpenStudy (vaboro):

I think in order to stop errors you have to make sure your subStringMatchExact() and constrainedMatchPair() work without errors.

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!