I just started Ps3 and I am sort of confused by the first part. It says to count the number of instances of the key in the target. The reason I find this confusing is that you can simply place "target.count(key)" in the function and it returns the number of instances. So am I to assume that we are to recreate that string function ourselves using an iterative and recursive method?
http://pastebin.com/2TQLNUfx Well I just went off the assumption that using the string.count() function would be cheating lol so I recreated the function using iteration. If anyone has any comments on my code for improvements or maybe simplifications I'd love to hear them. I'm now trying to figure out how I'm going to implement it recursively lol
I had a terrible time with the recursive function. If you scroll down on the previous questions asked in this group, you'll find a long discussion about it. That may help you. I also added a link to the question: http://openstudy.com/groups/mit+6.0+intro+computer+science+%28ocw%29/updates/4e5d9a6f0b8b1f45b49524cf#/groups/mit+6.0+intro+computer+science+%28ocw%29/updates/4e6a55580b8b4a2b95d4d5c7
awesome! thanks
from the problem instruction: "... count the number of times that a key string appears in a target string" the function you wrote will fail that criteria in certain instances. Your function does not allow for 'overlap', just like the python str.count method. two examples: http://pastebin.com/fLPfvhsu in the second example, your function returns 2 when in fact the key string 'appears' three times in the target string.
You are absolutely correct! I changed the added length to 1 so that it will now detect overlapping key values. As you mentioned, the string.count() funct doesn't allow for overlap so I decided to do the same thing (though for this assignment that probably isn't what is desired). Thanks!
I also wanted to add that I found another bug in my program. If you enter a target string such as "aaaa" and a key value of "a" the for loop will not iterate enough times to return the count, so simply add +1 to the end of the for loop range, i.e. for i in range(0,len(target)+1). I doubt anyone is using or looking at my code, but just in case!
Join our real-time social learning platform and learn together with your friends!