Tuesday, 31 March 2015

Week 7: Recursion

As I have mentioned in a previous post, I find recursion fascinating, although confusing at the same time. Recursion tracing over functions and methods seems simple, however, creating recursive functions that do exactly what you want is proving a little difficult for me personally. Going through some of the lab functions on recursion, it seems simple, but when you're trying to find something a bit more specific or more advanced it becomes troublesome.

    Simple functions like the ones from a prior lab and from lectures are great for general understanding although I would love for some more advanced examples to get a hang of things. The count and contains methods for BTNodes explained in lecture were easy to grasp.

def contains(node, value):
    ''' (BTNode, object) -> value

    Return whether tree rooted at node contains value.

    >>> contains(None, 5)
    False
    >>> contains(BTNode(5, BTNode(7), BTNode(9)), 7)
    True
    '''
    if node is None:
        return False
    else:
        return (node.data == value or 
                contains(node.left, value) or 
                contains(node.right, value))


    In this method, it's very easy to follow along. If the node is empty/doesn't exist, return False as there is no node that contains value. Otherwise, return whether the current node's data contains value or recursively check every left and right node for the value until the end. I do find help from other sloggers though. One slog that has been a big help has been http://antiaromatic.blogspot.ca/. This blog posted their solutions to labs which really helped me a lot when it came to thinking about how to design functions.

    When it comes to more advanced functions that search for specific things and return it in a specific way, it gets a little hectic. For example, for assignment 2, we are suggested to create a helper function that recursively searches through game states and return a minimized value for the opponent while returning a maximized value for self (thus the term minimax). This type of function seems like a far stretch in comparison. To create a list of gamestates, search them, determine the weakest move, return an integer value as well as a move recursively was quite difficult!

    However, this is not to say that the challenge wasn't a good thing! Challenging content is the way to create the best thinkers in my opinion.

No comments:

Post a Comment