Thursday, February 26, 2015

Week7:Recursion

Recursion happens in programming when the function  itself  at least once. So, a recursive function call itself until they find the base case( the value that function knows the result) and then start to close the recursive call until return something meaningful. If a function do not have at least one base, it will generate a infinitive loop, then the program will crash. So, when programmers are creating the structure of a function, they need to be aware of the advantages and consequences of using recursion.  One pro is to reduce the code. When programmers write a recursive function, they compact their code, and make it clearly . Another advantages is there are some data structure, such as binary tree or tree that use the recursion in their own concept, so to design the methods to deals with this data structure, for example, find a node in an binary tree. There are, however, some consequences, one of them is to allocate more memory to trace the code. This happens because the recursive function need to call itself until they get into the step that they know the result. Another consequence is many cases, the recursion can take more time to process than a iterative function. There are some program which takes the recursion as a solution, for example, the sum of a list, or the Fibonacci sequence, the binary search. The conclusion of recursion is that every case the programmers must decide which method is better to apply, in many cases, the best solution is recursion.

Wednesday, February 11, 2015

Week6:Object-Oriented Programming

  Last term in CSC108, I believed objected-oriented programming to be the most useless, tedious and annoying part of the course. I did not understand the purpose of classes, as they seemed to be a much more complex and inconvenient way of creating functions. While I acknowledge that I do not have a thorough understanding of object-oriented programming, I believe I have made strides in understanding its purpose. An instance of a class has specific data and attributes that are unique to that class. These features assist greatly in keeping everything organized. For example, I would imagine that Facebook performs well because of its usage of classes. A profile, for example, could possibly be a class that Facebook uses to create profiles for people that sign up for it. It would have attributes such as number of friends, age and date of birth and methods such as adding another user as a friend. I could not imagine how this would be accomplished without the usage of classes since the code would be much more complex and disorganized. This example is just to show that I have a new perspective on classes and their usefulness. I find it interesting that classes can inherit properties of other classes, although I do not understand the purpose of this since we could just have one class with all the properties of similar classes. With all the great benefits of classes, I still do believe that they are somewhat complex to develop at times. For example, I find that having to create __str__ and __eq__ methods very inconvenient and somewhat time consuming. I am also moderately uncomfortable with implementing such methods because I do not understand how they work. For example, I don't quite understand why using the == operator on two instances of a class will check if their memory addresses are equal rather than their values. This is one of the several aspects of classes that I have trouble comprehending.

      I believe I can solve my troubles with object-oriented programming by consistent practice with developing them, and by analyzing its properties thoroughly from top to bottom so that I will understand why they work the way they do. I believe that this solution is working so far because I have a thorough understanding of how some of the features in object-oriented programming works. For example, I know that the __init__ method is used to give an instance of a class its specific attributes and data, depending on the class of course. This was an example of a concept that I was struggling with before but now I am very comfortable with. I suspect that the reasons for my problems with object oriented programming are because I started working with them before really understanding why and how they work. With this post, I hope to have conveyed my thoughts on object-oriented programming rather than reiterating its several features. 

Thursday, February 5, 2015

Week5:Tracing recursion from week4

Recursion is a kind of very computational programming style that is recalling the function itself. For example it is like dividing 10 by 3, and each digit is 3 because we are dividing 10 by 3 recursively. However, in a recursion there is a base case which  tells when the recursion should stop. Since we can use only one recursion function, which is always short, to deal with huge algorithm, recursion makes our coding easier. Once we think it recursively, we can finish it in fewer lines of codes. Otherwise, I believe it should be several "for loop" with a lot of if statements.
    To master recursion, I have two steps and one strategy. At first, you should always find out the base case. The base case does not only ensure the ending of the recursion, but also give you a primary understanding of the algorithm. After that, normally, you can just move one step up, and think about the situation which has only one recursive step. If you can deal with that, you are done with your recursion problem. The strategy I really love to use is list comprehension. I believe this is a very useful way to right the recursion function. For instance, when I come up with a recursion idea, I will describe it in  English. Since python is a pretty readable programming language, my description can always be easily transfer to a list comprehension format. For example,  my description is like do something when balabla, otherwise, I will do something else, for every item in the list.  SEE! This is almost a list comprehension format, and I am almost done with the recursion problem.