Thursday, February 27, 2014

Week 5 - More Recursion and Namespaces

In our lectures this week, we wrote out code for the recursive solution to the Tower of Hanoi problem, for three stools and any amount of cheeses.  This in-class exercise was great for making sure that I understood how to approach a problem recursively, as well as being an important piece of the Tour.py file we needed to write for Assignment 1.

We also covered Namespaces and Scope this week.  Basically, when an object is created, the name assigned to that object is only valid in the namespace in which it was created.  For example, say we create a variable x within a function test().  Variable x can be used anywhere inside the scope of function test(), but outside of that function, x does not exist.  As a matter of fact, the outside of test() can have its own variable called x, which is completely independent of the x in test().

There are multiple levels of scopes that Python handles, from innermost to outermost:
  • The local scope, which is the "current" function
  • The nonlocal scope, which is the outer functions, if any, of a function.
  • The global scope, which is the module, __main__
  • The outermost scope, which has all of Python's built-in names.
In order to rebind an object, the "nonlocal" or "global" statements can be used.  However, using global variables is not recommended, as they can be difficult to deal with and may easily lead to unexpected behaviour, and from the examples I have looked at, it is pretty easy to understand why they aren't the most intuitive thing to work with.

Week 4 - Recursion

This week of class focused on learning to solve problems using recursive functions.  That is to say, a function that calls itself under specific circumstances.  Deciding when to use a recursive function, as well as how to write it, was pretty tricky at first, especially since it was a very minor part of my high school course, but I was able to get a good grasp on how to work with them.  In a recursive function, you need a general case, and at least one base case.  Recursive functions necessarily take the form of conditional statements, since if there was no condition for the recursive call, the function would repeat itself indefinitely.  The base cases serve as the stopping points for the function, and returning a value instead of repeating the recursion.  The first assignment was talked about, with it being noted that recursion would be necessary in writing the program, so it was good that I had the chance to practice recursion to a level I was comfortable with before I started.

I didn't have any real difficulties with the work this week.  The lab was pretty simple for me and my partner, and the exercise on raising exceptions was straightforward.  There was one thing that was in the lecture that would have been useful in my code for Assignment 1:  The fact that a set of tuples is a valid argument for min.  I ended up using a different approach to the problem that may not have been ideal, but I'll just have to remember that information for the future.