Saturday, 8 February 2014

Week 5

Lecture

So this week was just a bit more on recursion. We learnt a bit more on how recursion works and how to trace through recursion functions step by step. Tracing isn’t one of my strongest points so it took a few examples before I got the hang of tracing through recursion code, but it was definitely challenging in a good way and an interesting learning process!
One of our tracing examples was:

def rec_max(L):
"""
Return the maximum number in possibly nested list of numbers.
>>> rec_max([17, 21, 0])
21
>>> rec_max([17, [21, 24], 0])
24
>>> rec_max([17, [21, 24], [18, 37, 16], 0])
37
"""
return max([rec_max(x) if isinstance(x, list) else x for x in L])

The point I started understanding how the tracing for recursion worked was when I figured out when exactly I had to start using the base case and then work backwards from it. It was frustrating at first, but it worked out in the end! Another useful technique was learning when I needed to just plug in values so I wasn’t tracing too much.
We also got a few examples with the Python visualizer, which definitely helped when it came to tracing through the recursion and understanding the step-by-step process of such functions. We even got a bit of help on the move_cheeses function for our Tower of Hanoi assignment which definitely helped as both a recursion example, and understanding how to make the code in our assignment more efficient.
Lastly, we also went through a few unittest examples so we had a better understanding on how it worked and how to use it.

Lab 4

I probably learnt the most in this lab than I have in any other lab, since this time we actually got to practice using recursion for the first time! Our first two exercises were tracing through recursion code with pencil and paper, which is pretty useful since our exam will be the same way. Also we had to make sure we were following one of the programming rules: being lazy! While tracing through code, we had to try and plug in values we’d worked out earlier to avoid tracing too much, a pretty useful trick in the Python programming world!
The rest of our exercises focused more on actually writing our own recursive functions, which was a bit daunting at first since I’d never done it before. Luckily, after figuring out what our base-case was, my partner and I found putting the rest of our function in a lot easier than we thought it would be. So I definitely learnt a lot from this week’s lab!

Assignment 1

Now for an assignment update! My partner and I are making pretty good progress with it so far! We’re almost completely done with step 4 of our Towers of Hanoi assignment. We’ve finished our TOAHModel.py file and it works great with GUIController (It’s actually pretty fun to play). The TOAHModel file seemed pretty challenging to finish at first, but as we went through it and the code got more familiar then it gradually got easier. Hopefully ConsoleController isn’t much harder, but since we’ve got most of our code in, it shouldn’t be too difficult. 
This assignment is definitely a lot more interesting than 108 assignments since it involves trying to work with different files and making sure they work together, and we have to write our own functions instead of them being half-done for us. Since it’s a game, it’s also a lot more fun!


That’s all for this week!

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!