Friday, February 28, 2014

What is Recursion

It’s the seventh week of CSC148 and I took the midterm for CSC148 as well as CSC240 and I don't have much time to sit down and think about what is recursion, but since I’ve survived the week, I finally had some time to write down my thoughts on recursion.

Recursion, in my own viewpoint, is to divide a problem into several smaller problems which are easier to solve and the solutions of these smaller problems, when combined together, formed the complete solution to the original problem. Here I take a quite simple example: the Fibonacci sequence (see http://siyangcsc148w3.blogspot.ca/2014/01/some-thoughts-on-recursion.html for detail). Suppose we want to know the nth element in the sequence, then we need to know the (n-1)th and (n-2)th element in the sequence since all elements in the Fibonacci sequence is the sum of two elements that are immediately precede it and to know the two elements, same process will be applied until we reach the first two elements which are defined to both be 1. So the process is actually breaking bigger problems down to smaller problems until a solution is reached and then the solution is passed back and combined to form the complete solution. Here the solution to the smallest problem is called the base case and the process of reaching complete solution is called recursive steps or constructor cases.

But then comes the question: why should we use recursion? I’ll say that the use of recursion could significantly reduce the length and complexity of the program when used properly. (Check my previous post http://siyangcsc148w3.blogspot.ca/2014/01/some-thoughts-on-recursion.html for detail). Like the function we use in assignment1 for moving cheeses among four stools. A recursively defined process only requires 3 lines to reach the goal while it would be quite lengthy to write a procedural function since there are so many cases that one could easily miss something and even one does consider all cases, the code could be quite lengthy and became harder to understand than the 3-line recursive function. However when can we say is a good time for recursion? Certainly, many recursion could be written in for loops like the problem 1 in the extra section of lab 4 – write a recursion function that reverses string s and it could be easily written in for loops as we learned in CSC108, and it only requires 3 lines. So reverse a string might not be a good time for us to use recursion since recursion neither reduces the length of the code nor makes it easier to understand. Nevertheless, there is someplace that recursion could play an important role – when a program requires consideration of many different cases in for loops, recursion could be quite useful as the assignment1 I mentioned earlier. So, when thinking about using recursion, first consider the for-loop version of the program, and if the for-loop version doesn’t require considering too many cases and is not hard to understand, then use for loops instead of recursion. Also, if we want different solutions from the same problem, then recursion is, I’ll say, useless since recursion is based on the assumption that smaller problem only has one unique solution which can be used to construct the solution for bigger problems. One more thing, recursion couldn’t be used on particularly large problems since most programming languages limit the recursion depth to a certain level to avoid crashing of the whole system so when encountering particularly large problems, either use specially designed recursion or find another way without recursion.


In the end, I want to discuss a bit about debugging recursion. Basically a recursion needs a base case and recursive steps that lead to the base case or at least, smaller cases. You can find more on my previous slog http://siyangcsc148w3.blogspot.ca/2014/02/at-end-of-this-slog-httptracycsc148.html.

Monday, February 17, 2014

Debugging recursion in a systematic way

At the end of this slog http://tracycsc148.blogspot.ca/2014/02/csc148-slog-object-oriented-programming.html, the author mentioned “a recursive function will not stop running until the condition in the 'else' part is met” which sounds easy to understand but actually hard to recognize such an error when actually coding. So here I am about to talk about how I debug for recursive functions.
               First of all, the most obvious one – syntax error. It’s obvious doesn’t mean it’s always easy to recognize. Like the preorder function for Trees,

(This is not quite right. Please refer to CSC148 website for the correct version)
When brackets and parenthesis get mixed up, it became much harder to detect where we missed a right parenthesis or a bracket at a glimpse and it’s very likely that we need spend quite a while reading line by line if coding larger projects. Such thing is inevitable but we can always try to avoid it by splitting them into different blocks – instead of writing ternary if, writing in different blocks as we did in CSC108 makes such ‘silly’ error more apparent and may save much time.
               Then comes to the run-time error where under most circumstances problems occur. Most people trying to write recursive function must have experienced error message like ‘Maximum recursion depth exceeded’ which indicated the program falls into an infinite loop. To fix such an error, the very first thing, to me, is to test the base case – an infinite loop always mean the program can’t get to the base case or the base case is in an infinite loop. So it is essential to trace the base case and make sure it’s functioning. For instance of the Fibonacci sequence I wrote in my previous slog (http://siyangcsc148w3.blogspot.ca/2014/01/some-thoughts-on-recursion.html), a functioning base case is essential for keeping the program getting into infinite loop. After the base case, check the constructor cases. Starting from small cases, like the third element in Fibonacci sequence and print out things returned by every recursive step. Still, I will take my fib function as an example.

What I did is to print tuples made up of the fib result and its associated number.
Suppose I type fib(4), it will print (1,2) and (1,1) ,(2,3)and return 2. So I will know fib(2) , fib(1) and fib(3) are all working and if it’s not working, I could quickly find out at which point the recursion failed and fix it.

I used this method in Assignment 1 where Tour module worked for 8,9,10 and 11 cheeses but not for 12 cheeses. As I print out all the recursion steps, I found my initial set up for 2 cheeses is wrong where I set i=0 for 2 cheeses instead of 1. And it saves huge amount of time than tracing by hand or wondering around.

Wednesday, February 5, 2014

Coding style

    This slog http://slogslogslogslog.blogspot.ca/2014/01/week-4-inheritance-exceptions-and.html is quite intriguing to me. It talks mostly about the importance of writing syntactically correct programs, and I believe this plays a huge role in writing programs. Different programming languages have their own syntax so it is essential to learn the correct syntax at the very beginning and then apply the correct syntax. Otherwise, obviously, ‘Syntax Error’ will prevent any program from running and the logic behind it could never be implemented. In addition to syntax, I would like to talk more on coding style. A good coding style is something that makes codes easier to read, easier to follow, and easier to understand. Think about the following example.







    Both function return the power three of x and syntactically correct but the first function is, under most circumstances, prohibited. There are two reasons: First: the function header shows nothing about what the function will do. (Who knows what will ‘dosomething’ do). Second: Writing function body just after the function header does not show the block of the function which makes it harder to read when the function body became longer and longer. So the second function, clearly has a better coding style: clear header, apparent blocks and is taught as a standard way by almost all teachers. So like assignment1, when my group and I start working on that, the very first thing is to set up a consistent coding style, so that everyone could understand what others are writing more efficiently and can add codes without too much modification. Like the initialization of TOAHModel, we set the names of the attributes of the objects as ‘stools, cheeses, etc.’ So when other members of the group and TA wants to read it, it is obviously much more understandable than simply putting ‘a,b,c’ as attributes.
To sum up, a good coding style saves a lot of time and makes both programmer and debugger easier to understand and trace the program, so it is important to have correct syntax but more important to have a good coding style.

Wednesday, January 29, 2014

Some thoughts on recursion

It’s the fourth week of CSC148 and just a brief summary of what I have learnt: First week, object-oriented programming, the second week, inheritance and recursion, the third week, exception, and this week, more on recursion. Also, assignment 1 came out and it’s basically about the Hanoi tower with more than three pegs. Today, I am going to talk something about recursion. The most well-known recursive thinking, I believe, is the Fibonacci sequence – 1, 1, 2, 3, 5, 8…… Starting from the third number, each number is the sum of the two numbers before it.(5=2+3, 8 = 3+5) Every time we want a number, we need to know the two numbers before it, and to know the two numbers, we need to know the two numbers before themselves.  This may sound confusing, but it would be clearer when put into code.





The base case returns 1 when n equals 1 or 2 and if n is greater than 2, the program would firstly calculate the sum of the (n-1)th number and (n-2)th number, and to calculate the (n-1)th number, the program would calculate the sum of the (n-2) and (n-3)th number, and same for all the numbers until the program reaches base cases and return 1. Starting from 1, the program adds these numbers together and produces the nth number we want. As Danny said in class, tracing a recursive function could be too complex to handle, so my way of tracing the code is to use abstraction – Find patterns of the code for the first or two cases, then apply these patterns to more complex cases based on the assumption that the code could work properly.

As seen from the Fibonacci sequence program, through recursion, we can significantly reduce the length of the code. Through abstraction, recursion would be much easier to understand than writing in for loops or other loops. However, recursion, for most of the times, actually lowers the efficiency of the program. Thus, when using recursion, we need to find a balance between efficiency and code length – if the code could be converted into for loops while not extending much length, it is better not to use recursion. So analyze at the very beginning, and decide which way we are about to use.

Saturday, January 18, 2014

What is Object-Oriented Programming

When I started to learn Python a few months ago, I was introduced that Python is an object-oriented programming language and the essence of OOP are ‘encapsulation, polymorphism, and inheritance’. These words used to confuse me. But now, they start to make sense.
    During the first week, when we analyze the statement about an object called Point, we designed the attribute (‘coord’) for a point and a method to calculate the distance from the point to the origin. Instead of directly writing separate functions to set the ‘coord’ for the point and calculate the distance outside the class, we write these functions (methods) inside the class and use the property function to control access to ‘coord’. By creating a class for Point and infesting it with methods as well as attributes, the program is no longer just processing data but creating objects that have their own attributes and methods encapsulated in the class. Only output and input matters and the process are hidden inside. It’s kind of like us, human – inside us, organ’s function to keep us alive. All the metabolism and actions are controlled by things inside us – ‘encapsulated’ and other people could hardly see the process but the input and output
Then comes to polymorphism and inheritance. As Prof. Heap talked during lecture, laziness is the first thing learnt in programming. Here laziness didn’t mean too lazy to give up tasks but avoiding duplicate code. Like the coord argument passed to Point object in its __init__ method, by using list comprehension [float(x) for x in coord], coord with different lengths could be put in and the class will still work. So we can avoid writing different classes for Points with different number of coordinates. Another thing about laziness is inheritance. A class could inherit all the methods from its parent class while still leaves space for programmers to override or modify the methods based on specific needs. For instance of the ‘IntStack’ we created later, by reusing __init__ method from the parent class Stack, it avoids many duplicate codes. Even though inheritance is useful, it’s better to avoid multiple inheritance since it will become harder to trace which method the class is actually using which will cause serious problem when debugging.
When I was testing my code, I found a flaw of python class. Even though we can use property function to control access to attributes, we can never fully protect them from being modified outside the class. Unlike Java where we can use ‘Private’ to hide attributes, in python, ‘_’before the name of attributes and property function are just nice ways of informing programmers not to modify them.

    To conclude, object-oriented programming is, in some sense, creating real stuff and give it real attributes and ability for some actions. Like human, all the process are encapsulated inside the body (encapsulation), different inputs may result in different outputs but not error (polymorphism), and inheritance.