Sunday, 16 February 2014

Recursion

The key to writing recursive code is understanding that recursion is "defining something in terms of itself." So the best way to approach recursive code is by finding the base case of what you're trying to do and writing code for that. From then on, the trick is to just have the function call itself repeatedly until it reaches the base case; therefore the only thing the code actually does is whatever is in the base case.

Throughout the weeks of learning recursion it seemed simple enough. Tracing through codes was tedious but eventually a pattern could be found that convinced me that the code was just repeating the base case multiple times. But actually writing recursive code was a whole different ball game!

While doing A1, and reading through the notes on recursion, I learned that there are two ways of viewing recursion. The low-level operational view, and the high-level view. For example, step 5 of the assignment, writing a recursive method of solving the tours of anne hoy game for n number of cheeses on 4 stools, can be represented by both views.
The low level view can be represented by breaking it into steps thereby eliminating recursion. This would involve writing code for moving 1 cheese, and then calling that to move 2 cheeses in a new function, then calling 2 cheeses to move 3 cheeses etc. This method would be impossible to write for an infinite amount of cheeses though. So the objective was to use the high-level view which involved "Taking a leap of faith." This is where you write code for 1 cheese and convince yourself it works for 1 cheese, within the main function "tour_of_four_stools." And then you can call this function within itself for any number of cheeses larger than 1. For n number of cheeses, by calling this function within itself on n-1, whenever n > 1, eventually n =1, and at that point the code you wrote for n=1 is executed. This happens repeatedly until n amount of cheeses are moved, and the puzzle is solved!

Although recursion seems complicated it is actually a very effective and useful way of simplifying a situation into smaller parts and makes solving coding problems much easier.

Monday, 3 February 2014

Import Statements

After spending an entire week buried in my psychology textbook, cramming 4 weeks of work into 6 days, I realized on Wednesday night that Exercise 2 was due on Thursday..good thing I realized when I did. Anyways, I took a break from psych and tackled the assignment. I finished it pretty quickly and felt confident about my code, but after looking at the autotester results the next day, I was in a state of panic.

I had no clue what was wrong with the code so I went to office hours for guidance. In my attempt to finish the exercise as fast as possible, I missed out on important details in the handout, the key one being to import e2a using the format from e2a import xyz. I had no idea the format of an import statement had such a big impact on code, not to mention I'd never seen that format before. That format also came with other small changes such as no longer having to call functions from e2a using e2a.x, they can just be called x. 

If only I had learned all these useful coding strategies before the deadline....
lesson: never leave things until the last minute...speaking of which I should probably start A1 hahaha

Object Oriented Programming continued...

Regarding my question from last week, after re-analyzing the code we looked at in lecture, I realized the problem I had was more with exceptions than classes. When using the format try and except, when an exception is raised in the try clause, the exception clause goes in order as its written. So if certain exceptions are subclasses of other exceptions, they get caught as the superclass exception first and then stop checking the rest of the except clauses. For example, in the code below the special exception would be caught as an Exception and the code that catches it as a SpecialException would never be reached.

class SpecialException (Exception):
     pass

if __name__ == "__main__":
     try: 
          raise SpecialException
     except Exception as e:
          print("caught as Exception")
     except SpecialException as se:
          print("caught as SpecialException")

This is why its very important to keep in mind the order of exceptions and write them in the order of most specific to most general (where specific refers to a subclass and general is a superclass).