Sunday, March 2, 2014

Recursion

Although it seems a bit complicated at first, Recursion is one of the most useful methods in computer science. It took me a while to understand Recursion, but at the end it's worth it. Doing the Assignment 1 helped me a lot in understanding it.  Basically, Recursion is running the same function over and over again till a condition is met. There's a example of recursion(finding the nested depth of a list):

def nested_depth(L):
    return (1 + max([nested_depth(x) for x in L] + [0]) if isinstance(L, list) else 0)

So what this function does is, it loops over all of the elements of list L and adds 1 every time the element is list, if the element is not list it adds 0.
Overall Recursion is a great method that makes hard tasks easier to solve and save tens of lines of code.

No comments:

Post a Comment