Saturday, 1 February 2014

Week 4

What is recursion?

Hey guys! So this week, our lectures were mainly focused on the basics of recursion. This wasn’t a completely new concept to me since I had gotten glimpses of recursion in CSC16 when we looked at the halting function. However, I was pretty unfamiliar when it came to actually coding my own recursive function.
I learnt that, basically, a recursive function is when you define a function in terms of itself. In other words, you call the function in its own definition so that you can use it to write smaller sub-problems. Most recursive functions are similar to if/else statements.

Recursion examples

For example if you wanted to write a recursive function for the sum of every even number up till a given even number, that woud look like this:
def sum_even_num(n):
                if n == 0:
                                return 0
                else:
                                return n + sum_other_num(n-2)
The most important piece of this function is the base case. This is the part of the function which doesn’t call itself, in this case, this is the “if” part of the code. If entered in sum_even_num(4) into the python shell, the following would happen:
1.       The function would go straight to the “else” part since n != 0.
2.       It would return 4 + sum_other_num(2)
3.       The function would loop over again and go back to the else part
This chain reaction would keep going until n = 0, and the function would return 6. The function would go into a chain reaction until it reaches the base case and has a final answer to return.

This week’s lab

I’d say this week’s lab was pretty fun. I got to practice coding some classes and Inheritance, and I worked well with my partner which is always good! The functions for motorized.py were easy to code, especially since we could relate them to real-life. I was already quite familiar with classes which also helped. The testing part of the lab took a bit more work, especially for me since I hadn’t used unittest that much in CSC108. But once we remembered the basic format and documentation for testing, it got a lot easier to write up the test classes. In general, I’d say that the practice from this lab made me much more confident about coding classes and testing functions.

Starting Assignment 1

So for our first assignment we’re supposed to recreate the game “Towers of Hanoi” which is linked here:
So far my partner and I have read through the given code and added the basic methods to TOAHModel.py, so we’re off to a pretty good start!

My reaction

I really enjoyed the lectures this week, the fact that I was learning a new concept made it even more interesting. It was a bit difficult to understand at first but after I wrapped my head around how the base cases worked to stop the function I found it much easier to trace the programs. So understanding recursion was definitely my main achievement this week.

That’s all I can think of for this week! Good luck on Assignment 1! 

No comments:

Post a Comment