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

ps3c.py... it looks like subStringMatchExact needs to match an empty string to... something... or subStringMatchOneSub doesn't work when substituting the first or last letter. What should it say for the occurrence of an empty string in "ATGACATGCACAAGTATGCAT"?

OpenStudy (anonymous):

Can you rephrase your question I don't understand what you're asking.

OpenStudy (anonymous):

subStringMatchOneSub (the code provided in the lecture) calls the methods we're supposed to write. It calls our method, subStringMatchExact, looking for instances of the string "" in various other strings. This causes my method for subStringMatchExact to go into an infinite loop, and so I got no answer. Changing subStringMatchExact to return an empty tuple (no matches) does not work either. My constrainedMatchPair method looks logically correct but may be wrong, I supppose... here's a dpaste of the code: http://dpaste.com/532155/

OpenStudy (anonymous):

Your substring match exact should only take two arguments. You don't have to change it if you don't want to I don't think it affects your code but what the pset said was to only use 2 arguments. Also I don't know what I am supposed to be checking because I just ran this on my computer and it didn't get stuck in a loop? What version of python are you using?

OpenStudy (anonymous):

It's a recursive solution. Because default values are provided for the last two, only needle and haystack (key and target) need to be provided to invoke the function. I don't believe it would be possible to solve this recursively (which was an option) with just two arguments, but I could be wrong. I added the condition needle == "" to the base case to get around the infinite loop, but it still doesn't return the right answer. If you look at the output, subStringMatchOneSub splits key into "ATG" and "" at the end. Match1 finds ATG at position 0. But because match2 is empty, 0 doesn't make it into the allAnswers tuple. It should, though, because the string starting at 0 (ATGACA...) matches "ATGC" with one substitution. Is there something wrong with my constrainedMatchPair method? I think I implemented it according to the instructions, but it will never return a match if one of the tuples that's passed to it is empty. If you run this with different arguments (like "ATCA", "ATGACATGCACAAGTATGCAT") the code works as it should. There is a match at position 0 because "ATCA" matches "ATGA" except that the C is replaced with G. The problem is, this doesn't happen when the first or last letter of "key" is removed.

OpenStudy (anonymous):

First since there default values there would be no difference if you assigned them in the first 2 lines of your function but like I said you don't have to change it there will be no difference except following the prompt. Give me one sec on checking your constrained matchPair function I'm not the best at recursive functions so it might take me a little while.

OpenStudy (anonymous):

Here this one works the reason you were getting an empty tuple back for it was you were saying if needle=="" return occurences but remember your occurences were originally set to () so it just returned (). so I made the if needle=="" into a separate if and told it to return a tuple with numbers 0 through length of list. I don't know if this was the best way to do this like I said before I'm not good with recursive functions but this was the only way I could think to do it. http://dpaste.com/hold/532230/

OpenStudy (anonymous):

Ah! Thank you. That seems to work. Inelegant and kind of counterintuitive, but I was kind of suspecting that was what I needed. Thanks, Onyx.

OpenStudy (anonymous):

Yea no problem

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!