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

Ps3 problem 2 I want to know why I am getting same answer in the tuple more then once, def countSubStringMatch(target,key): ans=() message='Do not give me empty string!!' length= len(target) if length==0:return message for i in range(0,length): n=find(target,key,i) if n==-1:return ans ans = ans + (n,) return ans

OpenStudy (anonymous):

When i == 0, n == 2. When i == 1, n, once again, == 2. find(target, key, 0) and find(target, key, 1) both return 2. There are other problems with your code, too. This function needs to be compatible with empty strings, or else it won't work with the functions in the later problems (not your fault, as the assignment isn't clear at all about this). Your function is also supposed to be named countSubStringMatchExact. Not really a big deal for the purpose of this assignment, but practicing consistency is very important for a programmer. There's also a slight bit of redundancy in the code, so I've demonstrated a fix to your problem that should work (but I have NOT tested). http://pastebin.com/1vkC7Sm7 - This is a really quick and inefficient way to modify your code to remove duplicates. There are probably thousands of ways my example here could be rewritten so that a duplicate check isn't even necessary (like for example, starting your search at the index after a find match). I would encourage you to think about some of the ways you could perform that.

OpenStudy (anonymous):

Er, I should clarify that in my example at the top of my post, it was with the assumption that the first index key would appear at was 2.

OpenStudy (anonymous):

Thanks for the reply. Now I've got the idea I've done the same thing using while loop and it is working totally fine now.Instead of using for loop against the length of the target instead I am making the base case as (while n != -1) and then also before putting the result in answer. n is index and answer is tuple in which I am storing the result.

OpenStudy (anonymous):

Now I am working on problem 3 of this problem set somehow it is not clear for me this n+m+1=k thing .. can you give me some example so I can understand and start implementing it.

OpenStudy (anonymous):

I've got the idea now if someone else has problem here is the example, Suppose, target= atgacatgca and key = atg.. which we are breaking as key1=a and key2=tg; this will give us result as .. start1=(0,3,5,9) and start2=(1,6).. remember if we use key=atg directly if will give us ans=(0,5).. now how to check the equation in our case, from start1 n=0 then n=3,n=5 and n=9 as we will carry on checking. m= 0 because we took key1=a ( this part is confusing for me also ) k= elements of start2 which are 1 and 6 respectively. the equation is n+m+1=k when n=0 ,m=0 and k=1 equation satisfies so ans(0)= n which is 0 when n=5,m=0 and k=6 equation again satisfies so ans(1)=n which is 5 hence the answer will be ans=(0,5). Also for those who have done it already please correct me if I am wrong. Thanks

OpenStudy (anonymous):

Yeah, you're doing it wrong. You don't just split the key into two pieces, you also remove a character entirely. So atg would separate into '' and 'tg', 'a' and 'g', and 'at', and ''. That's why you had to use an incorrect length for m to get the formula to work for you.

OpenStudy (anonymous):

So this means that I am doing the formula right but was splitting in a wrong way.. Thanks :)

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!