Monday, February 2, 2015

Recursion

In last week's lecture we talked about recursion.

Recursion, as you can guess from its name, is a function calling itself. The purpose of this is to divide the problem into smaller problems of same kind. And when it comes to solving problems that are composed of same problems but only smaller, recursion becomes extremely useful.

The most famous data structure that bases on recursion is Binary Search Tree.
For BST, each node has a unique key and all of the nodes are in order (e.g. the key value is always bigger than those of its left subtree and smaller than right subtree). Because of this unique attribute, we can use recursion to do search, insert as well as delete.

Thursday, January 29, 2015

First three weeks gain

Hi I am Weicheng and you can just call me Wayne.

This is my first post. Should have come earlier. Too much work on the other courses. Sorry about that. Won't happen again.

Anyway, python is new to me since I didn't take CSC108. But the course materials are pretty much covered in the ECE244 course I took last term, which is c++. Although the syntax and different type of variables kept bugging me since the start of this course, I found it really easy to program in python. I think the reason is that most programming languages, rules and logic are almost the same.

Since I learnt class in c++, I will talk about the differences between classes in python and c++.

1) The __init__ method is like the constructor which initializes the class when an object is created. It can take only self argument (which in c++ is void) or several arguments. But class in c++ can have more than one constructor and the default constructor is provided if you don't write one. I am not sure about the class in python. 

2) The __str__ methods from both languages basically are the same. print(class) = cout << class. The one from python returns the string form of the object and in c++ you write an overloaded operator<< to print an object.

3) The __eq__ methods are the same. In c++ there is an method "operator==" corresponds to this. And it should perform the deep compare.

4) An another major difference is that there is no so-called private member in class of python. A private member can essentially only be altered by a member method which is called mutator. And functions that can access the member variables or methods are called accessor.

5) One thing I am not sure about is that the unique "self" in python. In c++, there always exists a pointer "this" that points to the the current object. And when you write "member_variable = blahblahblah", what actually happened is "(*this).member_variable = blahblahblah" (* is dereference) and usually we omit the (*this). Does the "self" in python function the same way as (*this)?