## btbessaay 4 years ago Can anyone guide me on problem set 3 problem 1? I have completely no idea how to code this...

1. bwCA

A good place to start is to write down, on a piece of paper, what it is you are trying to accomplish and the steps you need to take to do that - don't be thinking about a computer, just the process. look at what you wrote, let it marinate, think about how you can turn it into a program. you might end up reading the programming language documentation a lot to try and find the kinds of things you need to solve your problem in code. for Python the Tutorial in the docs is a great place to start - in windows/idle F1 will bring up the docs. the online docs are here http://docs.python.org/index.html. I've read the whole tutorial at least once and parts of it many times. Every time i read it i see something i didn't see before. what specifically has you stumped at the moment? please use dpaste.com to post your code - paste it at dpaste and post the link here.

2. Joz

The only thing I could add to what BwCA has already said, is what the relevant skill sets are. The question is in 2 parts. The first asks you to iteratively count the amount of times string x is in string y. That is not so different to other counts we have done, except we are now using strings. You will need to understand how to manipulate strings to complete. The second part asks you to do the same but in recursion. You have practised strings now, so the skill set is recursion. I personally had an immense challenge with this. I am still working on this Problem Set...

3. btbessaay

ok, with some help, I was able to do the recursive part (alot of help) but I do realize how much more simple the recursive answer is. bwCA is right, I just need to understand exactly what I'm trying to do. I suppose I should have done the iterative route first, but now that I understand exactly what was happening and what needed to be done, I think I can do it. For the iterative part of the program, I have my function and my while loop. Then I want to define some variables for keeping track of my progress (numtimes is the number of times the key is found in the target) and start (what I used in my recursive answer as x + len(key) x being my find() function, starting after the previous instance that the key was found). Im going to attach my equation of dpaste.com to show you guys what I mean, then I'll try to go from there. thanks for the help

4. btbessaay

http://dpaste.com/557481/ that was easier than I thought it would be.

5. btbessaay

Ok, so my question is, what variables should I use and how do I define them? If I use 'numtimes' as the number of time the key is found in the target, numtimes will begin as 0 in the beginning of the function, but after the while loop, (while str.find(target,key) > -1) numtimes = numtimes + 1 everytime it finds the key within the target. How do I do that?

6. Nessman

personally i like the while true: loops while True: if test: #you don't have to enter "== True" if the test function returns true on its own return output using the return ends the entire function, you can also use Break to exit out of a single loop, sometimes you don't need a while condition, you just need a loop to run until you find the exit also keep in mind that the find method has optional arguments, you can tell it where to start and when to stop looking

7. btbessaay

http://dpaste.com/557607/ so this is what I've got so far, I'm pretty sure neither is complete. Any help is greatly appreciated. Nessman, I tried with the while loops but I feel like I'm missing something. I'm just looking for a starting point and something to help me comprehend what I'm trying to do. I'm so confused I can't even think of the right wording for my question.

8. Joz

The 2nd try function you have written above is another attempt at recursion. By iterative, it simply means count the loops through, (I think), there is no need to call the function onto itself. Having said that, there is no reason why you can't go ahead and write multiple versions of the function in recursion. I did, in fact, write 2 recursive functions for this. So long as you know that is not what is meant by iterative. If you read my posts about this topic a short while back, what I was trying to do was all about recursions. I had already written the iterative one and didn't post anything about it. I just add that in case you were confused by my request for help with 'iterating' recursions. In the first try, x(key) is an issue, try just x, and the loop is relying on x changing in size, but x doesn't change in size inside the loop.

9. bwCA

looks like your first try isn't working - but it can be made to work pretty easily. at line 6 what do you expect x to be if: 1) key is found?, 2) key is not found? also, if key is found at line 6 what do you want to do?

10. Nessman

Try adding if statements into your loop, keep in mind that str.find has an few optional arguments, including changing the start of the search somewhere that isn't the start. a single while loop can't accomplish much on its own, or that is all programming would take, while x if y do z, if not... try moving your x = str.find to inside the loop, and x simply starts out as 0 and it looks like BwCA beat me to the punch here

11. btbessaay

thanks for the help everyone. Nessman, I got the answer before I saw your suggestion, but thanks anyways. Here's my final answer for part 1. (I think part 2 should be fairly easy as its the same thing as part 1, except starting with an empty list and adding to it.) http://dpaste.com/557709/

12. Nessman

that is one way to get her done, i didn't think it could be done with a single if statement, its always interesting how everyone does things slightly differently

13. btbessaay

I try to use as simple code as I can.

14. Arbiter

@btbessaay One small problem I see with your solution is that when it is called with values like countSubStringMatch("abcabcab", "abcab") it will print 1 even though the string "abcab" occurs 2 times.

15. btbessaay

No, actually, the variable "count" increases by one when find("abcabcabc","abc") is greater than or equal to zero (when key is not found in target, default return is -1). count increases by one, and then start is given a new index to start at in the target and proceeds back through the while loop until the end of the target.

16. bwCA

'fraid Arbiter is correct. I tried his arguments and both your functions return 1 instead of 2. print 'cSSM', countSubStringMatch("abcabcab","abcab") print 'cSSMR', countSubStringMatchRecursive("abcabcab","abcab")

17. btbessaay

For your keys used there, 1 should be returned. I use 'abc' not abcab... I'll take a look into it, but I know my code works for me... I got the second part to ps3 no problem but I'm a little confused with what to do on part 3. im gonna dive into it tomorrow but just to let you guys know, I'll probably need help...

18. btbessaay

Actually, I didn't sync my targets and keys with the two parts. I'll post it with revisions, but if you paste my code exactly, it works.

19. btbessaay

http://dpaste.com/558204/ perfect. Run it and I get 3 for both.

20. bwCA

these still only return 1 when they should return two: print 'cSSM', countSubStringMatch("abcabcab","abcab") print 'cSSMR', countSubStringMatchRecursive("abcabcab","abcab") it seems like the functions should work for any target, key letter sequence, not just those given in the problem set. but if you are satisfied... from the problem set handout: NOTE: the solutions you are going to write will be tested on examples from DNA strings, but you should not assume that your solutions will only apply to DNA strings, i.e. do not assume that the strings consist of only 4 different characters, but rather that the strings could contain an arbitrary number of different characters.

21. btbessaay

abcab only occurs once in your target...

22. Arbiter

The substring "abcab" occurs at position 0 and 3.

23. btbessaay

ohhhh... i didnt understand completely then. i made the new start after the substring ends so it didnt count both of those, just whole ones, splitting the target in pieces

24. bwCA

interesting that the python string count method only counts non-overlapping occurrences. lots of hits with a google search of ppl wanting to count overlapping occurrences. "abcabcab".count("abcab") returns 1 :)

25. btbessaay

yeah that is actually... I feel like an idiot for disagreeing with you guys haha. Thanks for all your help though