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

from string import* def SubStringMatchExact(target,key): "count the number of the key string in the target" targetone=target+"@" answer=find(target,key) if answer<0: print "there is no wanted strings in the given strings" else: i=answer while i<=find(targetone,"@"): answer=find(target,key,i) if answer<0: break else: i=answer+1 print answer this is my code for problem 2 for assignment 3 how to teach me how to do problem 3. i don't know what it means. thanks

OpenStudy (anonymous):

Problem 3 asks you to put your countSubStringMatchExact function from problem 2 to work, by using it to write a new function that does the same thing your last one does, except this one returns values based on a key string with a single character turned into a wildcard. For example: You have the target string 'samus aran likes bananas.' A key string you might use for this would be 'ana'. This would result in a tuple containing the indices 18, 20, as that is the only place they appear. However, now lets take the key string again, 'ana', and say we want to find any instance of the key in the target, but we don't care if the middle character doesn't match. So our new key would essentially be 'a*a', where * is a wildcard that could be literally any character. Basically, your assignment here is to write a function that will do just that. In this case, searching for 'a*a', would return us the following tuple: 6,18, 20, because 'ara' in the target matches 'a*a'. Luckily all you have to do is write a function that handles only a single instance of a wildcard, and not every possible instance (but it has to be able to handle ANY possible instance). Not only that, but the assignment provides you with a formula which basically does the work for you. All you have to do is take that formula and your old function, and turn it into the required new function.

OpenStudy (anonymous):

Before you go on to problem 3 there are a couple problems with your solution to problem 2. First, it doesn't return a value. Problem 3 is going to use the return value of this function, so you have to return a tuple of the indices, not just print them. Second, what happens if I call your function like this SubStringMatchExact("abc@abcabc", "abc") or SubStringMatchExact("@abcabc", "abc") I know the test strings they gave us don't have an @ sign in them, but your code is supposed to work for any strings, not just a select few. You're using the @ as a marker for the end of the string. What do you expect find(targetone, '@') to return? Can you think of another way to produce that value, without using a marker?

OpenStudy (anonymous):

use a code pasting site to post your code. dpaste.com pastebin.com ideone.com codepad.org .........................

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!