Friday, 28 February 2014

Week 7: Recursion

Let's start with what recursion is...

Recursion is a method of solving a problem that is made up of smaller problems that are all of the same format as the big problem.

At first glance, this seems both confusing and somewhat useless. How am I supposed to solve a problem that is made up of essentially itself? For me, the key part to understanding recursion was the idea of having a base case and a recursive case. The base case is simple and easily solved and the recursive case is made up of the smaller problems that look like the main problem. So the idea is that all the recursive cases will eventually boil down to the base case (which we can solve) and then we can solve the main problem.

The structure of a (very basic) recursively defined function can look something like this:

       def big_problem(n):
           """ Solve this problem recursively. """
      
           # Base case
           if n == 0:
                  do something
           # Recursive case
           elif n != 0:
                  do big_problem(n-1)


Now if we think about this, for smaller recursive cases (i.e. 'n' is small) we can trace by hand what to do and eventually get down to a solution in terms of the base case which we can solve. But what if 'n' is very large or the body of the recursive case is more complicated than the function above? These are situations where recursion shows its true value. It can take a complicated solution to a problem and turn it into a very simple solution to code, provided of course, you can identify when to use it. And the easiest way to do this is by checking the definition given at the start.

So it's two thumbs up for recursion from me, adieu!

Sunday, 16 February 2014

Week 6: Growing tree knowledge

As a follow up to the Tour of Anne Hoy, I (oddly enough) managed to figure out a solution to the tour_of_four_stools right after I published my post by refining my shakier ideas so I was pretty happy. :D The time module gave me a bit of trouble but hopefully what I submitted was good enough to get me a decent grade.

As for this week, we looked at the definition of trees, how we could represent them as nested lists  and we also spent some time on binary trees. It's quite similar to graphs (of graph theory) which I'm familiar with and I've also seen a recursive definition of binary trees in CSC240 so it wasn't too much new terminology. We also looked at  the three different kind of traversals that we can do: pre-order, in-order and post-order. Pre-order traversals are pretty easy for me to think about but for the other two I have to keep reminding myself of the definition when I'm going over the examples.

Reading week is here! :D ...so time to get started on studying for my CS midterms.

Until next post, ¡adiós!

Sunday, 9 February 2014

Week 5: Towers of Anne Hoy

As a follow up to last week's blog, I did a little reading on property. I found Tamara's post to be helpful, so many thanks!

In this week's news, the Assignment 1 which is due on Thursday has been my focus. I've ploughed through Steps 1-4 and coded (perhaps not as elegantly as I could have) the pieces needed for the 2D and console versions of the game to work. But, (why is there always a but?) now I'm stuck in a rut for Step 5. I've managed to recursively code the M(n) function but I have no clear ideas for how to code the tour_of_four_stools. Hopefully some sleep and messing around with some of my shakier ideas will get me to where I need to be.

Until next time, enjoy this stack of cheese.

Photo credit to amelstrange of wordpress.