Sunday, 1 March 2015

Recursive code and advice on how to write it.

What is Recursion?
Recursion (in computer science) is a way to solve a complex problem by breaking the problem down into smaller and smaller problem until you reach a point where its small enough and you can solve it easily.

How recursive function works:
In computer science, recursive function is a function that calls it self. as such:

def sum_list(L):
    """
    (int or arbitrarily nested list of int) -> int
    
    Return the sum of all ints in L.
    
    >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]])
    36
    """
    
    if isinstance(L, list):
        return sum([sum_list(x) for x in L])
    
    else:
        return L

Here is a problem (which i learned in class). Before looking at the if statement look at the else statement. the else statement returns the most simplest value. As explained above in the definition, recursive code breaks down the problem into smallest possible problem where the problem can easily be solved. 
In this case, if the smallest problem is not a list then return whatever the value for L is.
Otherwise, if L is a list then break the list down into smaller lists until there is no list in the object L. that is where the else statement comes into play to return the simplest value. 

Understanding recursion:
  • here is how sum_list function would work in the above example:
  • >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]]) # first the function is called.
  • Then the items in the object L are identified. in this case, since the highlighted item is a list the if statement will execute the the following would happen:
  • sum([sum_list(1) for x in L]) # since the for-loop iterates through each item, the first item that goes is 1. Now we know that if the item is not a list, it would just return the item so the sum() would look like this:
  • sum([1, sum_list([2,3]), sum_list([4, 5, [6, 7], 8]])
  • sum ([1, 2, 3, 4, 5, 6, 7, 8]) # all the items would be returned from the recursion.
  • 36 #value 36 is returned from the sum() method.
How to write recursive code?
  1. Figure out what the simplest value should be returned. This is the easiest part, figure out the part of the problem that is the easiest to solve and write an if statement about that, leave the else statement for now.
  2. Figure out some simple examples that would need to use recursion the break down the problem into smaller pars.
  3. Now write an else statement to breakdown the problem to get to the simplest problem and feed it to the if statement written in step 1.
  • The hardest part is to write the recursive else statement, because sometime the problem is complex enough such that the else statement may need inner if/else statements for recursion to take place.
Recursive code allows for complex problems to be solved fast, where iterating through the problem (w/out recursion) and solving it would be complex and time consuming.
Recursion is used almost everywhere in computer science, sorting out items in a list (merge_sort()), finding a specific item in a Tree() (where Tree is a class which contains data in a tree formation) etc. This allows for problems to be solved more quickly and efficiently!

No comments:

Post a Comment