Saturday, February 22, 2014
Week 7 --- Reading Week
Time flies fast, and the reading week is over. During the reading week, I went to Robarts Library every two days in order to have a quiet environment to prepare for my upcoming midterm, and I have read the materials that we have learnt so far, and also the other study guides of class, stack, recursion, and trees which are written in Chinese to help me understand. I have wrote the two past tests, I thought I should study more since I could not finish them on time, because I thought I lacked of thinking up a program in a faster way. To solve this problem, I should learn more useful examples, and think why the author wrote the code like that. I hope I can do a good job in the midterm.
Week 6
This week I finished the assignment #1 successfully by myself, and I thought piazza is a good forum for students to discuss questions, and during the process of doing the assignment, I logged into piazza several times every day to check out the new posts of the other classmates, and I could learn from their questions, and came up with my own idea. The lab of this week was about the iteration which we have learnt a lot from CSC108, the for loop, and it was easy to deal with, because I have watched some CSC108 video lectures once more. However, after the reading week, the first term time will be held, thus, I decided to review the lecture slides, reading materials, labs, exercises, and also the assignment during the reading week.
Saturday, February 8, 2014
Week 5
This week, I have learnt more deeper knowledge of recursion in python both in our lectures and lab#4. At first, I think recursion is kind of annoying, because it is a little difficult for me to understand, but when I got used to it, I think it can solve many problems much easier by calling the function itself. CSC148 is quite different with CSC108, because in CSC108, we can write a program by learning from examples, however, in CSC148, we should think carefully to design a program. Therefore, we need to read more notes and examples to enrich our thought. As for myself, I think it is easier to read a code, and understand how it works, but it is difficult for me to think up a code that a question asks me, because so far, we have learnt numerous methods of python, and I do not know how to choose the correct the method to solve the problem. As a result, I know that I should review the knowledge very often both in the current lectures and the past old ones (including CSC108). This weekend, I would like to review the oriented-programming, classes, exceptions, stacks, and recursions to get well-prepared for finishing my assignment 1, and the upcoming midterm test. Also, during the reading week, I think it is necessary to watch the video lectures of CSC108 once more to consolidate the basic but important knowledge.
Saturday, February 1, 2014
Python Recursion
This week we have learned the recursion in Python language, and recursion is a process of repeating subjects in a self-similar way.
For example, factorial(N)=N!=N * factorial(N-1)
Then we can write the following code:
def factorial(n):
if n==0 or n==1:
return 1
else:
return (n*factorial (n-1))
print factorial(6)
Then we will get factorial(5) is 120. However, there is a maximum limit for the value of the parameter which is related with the memory of the computer. If n is greater than the limit, then there will be an error indicated. RuntimeError: maximum recursion depth exceeded.
Another example is that if we want to calculate the sum of 1,2,3,...,100. We can write the code like this:
def add(n):
if n<=0:
return 0
if n > 0 :
return n+add(n-1)
Then if we write add(100), we will get 5050 easily.
Every program of recursion always follows the same basic steps:
1) Initial algorithm.--Recursion always needs a seed value to start with.
2) Checking the correctness of the base case.
3) Using smaller or easier subproblems to re-define the answer.
4) Running the algorithm for the sybproblems.
5) Combine the results into the expression of the answer.
6) Return the results.
Subscribe to:
Posts (Atom)