Saturday, February 28, 2015

Slog Week 7: Recursion

Recursion


A recursive function is a function that calls itself until it finds the result. The general expression of a recursion should have two cases, which are a base case, the action to take when it has the result; and a recursive call, that calls the function again when the result is not found. If a recursive function is run without a base case, it will generate an infinitive loop and caused the program to crash.

def f(x):
       if statement:
              return or do something
       else:
              do something
              f(x)

The idea of recursion is interesting, and I think having such a method in programming will be really helpful. At first we do exercise on tracing a recursion, which is not a difficult task, and it helps understand the function a lot. But when I began to write a recursive function, I found it confusing and did not know where to begin with. After I wrote the function, sometimes it did not work simply because it didn’t have a base case so it kept running. However, after the general expression is introduced and I implement recursion based on that, everything gets smooth.


In most of the time recursion is an efficient way to solve a problem since it reduces a lot of code and make the function clear and some data structures such as tree and binary tree that use recursion in its concepts. However, there is also a situation that it is an inefficient to use recursion because it may visit more stuff and takes more time than simply using an iterative function.

Sunday, February 15, 2015

Slog Week 6: Summary of Object-Oriented Programming concepts

Summary of Object-Oriented Programming concepts

The main concept of object-oriented programming is that, differed from the traditional concept which people treat the program or machine as a whole and people command the program to work, here every little piece of the program can be seen as a small program contained in the program, which also has the same abilities as the “whole” program such as analyzing data and sending data and they can work on their own.

The relationship between class and subclass is an example of this concept.
When we build a class and a subclass under this class.
The class is the “whole” I mentioned above, it is a program or like a machine, it can function by itself and do things like collecting, analyzing data. However, its subclass can also function in the same way, collecting analyzing data on its own.
All in all, the concept is saying that when programming, people are building a machine, not only the machine can function by itself, so does every tiny piece of the machine, and the machine and its pieces are the objects in programming. Those little pieces, which is called the objects, including a class, a function, an instances and so on.

I didn’t understand this concept very well at first, so I decided to search it online and I also checked other student’s slog to see if they wrote about this, and they both help me understand the concept better.

Sunday, February 8, 2015

Slog Week 5: Impression on tracing recursion from week 4

Impression on tracing recursion from week 4

I feel pretty good about the problems that were given in the lab on tracing recursion. It is not hard to do if we are able to understand the code and I believe the recursion question that we are doing now are still some very basic question and it could get a lot more difficult if we continue on recursion. Although tracing recursion is not a problem for me, there is always error when writing a recursion.

The test on Wednesday is easier than I thought it would be. I was a little worried about it before the test for the reason that I felt like we didn’t learn a lot of things and I worried if I missed any important material. But the test was fine, I knew all the materials that were tested, but I realized I made a mistake just after the test. In the question that I supposed to raise a NotImplementedError, I didn’t read the question carefully enough that I treated it as a function that I could implement. I hope this won’t take down lots of marks.


For this week’s lecture, we were still in recursion. At the same time, there were some new materials, stack and sorts and did some review on testing at the end.