Finally a review of the most recent programming problem I was working on.  In this problem you interact with DNA strings and pre-identified keys.  These strands can be of varying sizes, and are given to you in the beginning.  Here is the description of the first problem set. Here I’m asked to create two separate functions that will iteratively and recursively solve the problem.  Just a quick refresher for those not familiar with programming, a function is a definition created within programming code to do a certain thing.  It will be unique to your file.  It’s like creating a new word for one document so you can make sure the same thing happens every time you type that word again.

An iterative function, is a function that will use a loop to process information.  Such as a ‘for’ or ‘while’ loop.  Both of these can create loops within code to process information.  A recursive function is a function that refers to itself in order to process the information.  A recursive function can be more helpful in certain situations, because it can make the amount of code you need to write much smaller.  It can also, sometimes, make it more easily understood.

First I created an iterative function that would solve the problem.  It is shown below. Here I’m using the find keyword in python.  What this keyword does, is it searches through the first item listed (target), and searches for instances of the second (key).  This basically solves almost the entire problem.  Next I just need to make sure my function spits out a list once complete.  To do that I simply identify a variable (“t” in this case), and make it equal to the symbol for a list.  To make a list you say t = [ ].  The brackets signify the list, versus parenthesis which indicate a tuple.

The next step in this function is the while loop.  This loop states that while x is less than -1, you will continue to do the following steps.  Those steps include adding each item string location found to be added to the list “t”.  It also states that once a location is found, you start one after that location during your next time through the loop.  Once completed this function will return a list of all the times the key will show up in the target DNA string.

Next, onto the recursive version of this function.  First off it’s important to note that for some recursive functions to repeat only a certain number of times, it must recognize where to stop.  For this I added an additional variable.  That is why instead of just the “key” and “target” you saw above, you see an additional “x” here.  That “x” represents the current position in the string. I’ve found that recursive functions, while shorter and more concise in coding, can be more complex to understand.  It compacts logic into a smaller space, at least it does in my own brain.  As you can see I’m still using the find keyword as I did above, however this time I have the additional variable at the end “x”.  This “x” represents the starting position where the find keyword is to begin in the list.  Thus if the “x” were noted as “0” when the function is originally called, then it would be starting at the first position.

Next you see that if “x” were to equal -1 (or the end of a list), then it would return an empty list in response.  Now here is the tricky part of recursion.  The “else” aspect of this function is where the function calls itself to solve the problem.  Here the function will add 1 to “x” and then append (add) the current value of x to the list.  Thus the final list is created.