Thursday, March 31, 2016

Week 11 Algorithm on Linked-list, Recursion and Tree


Today, I want to analyze the algorithm of previous ADT or functions.
1) Linked-list:
Linked-list acts like list. It has the property that we can simply remove or add items at any position without moving other items.
Therefore, doing sorting may cost less time or work. For example, if we apply the bubble sort on the Linked-list, compare with the case on the list, we don't need to move other items when we put the max or min at the first position. However, the Big-O of bubble sorting on linked-list is still n^2 with slightly better improvement, because of the same sorting strategy.

2) Recursion (Nested list):
In my opinion, recursion actually takes extra work to check whether the current case is basic or general. Consequently, the Big-0 of recursive function is at least n. I think recursion is a good strategy to solve problems with self-similar sub-problems, but recursion could not help improve the algorithm.

3) Tree:
General tree:
General tree is the same as recursion (nested list).
BST:
Binary search tree acts like ascending list. Compared with non-ascending list, it truly has advantages that the big-0 of searching should be lg(n) ~ n. However, compared with ascending list, it is hard to determine whether BST is better because the parent node could be more than or less than the average of two children which will cause the searching more difficult and increase the Big-O from lg(n) to n.

Friday, March 25, 2016

Week 10 Algorithm


We encountered algorithm this week. Here are some types of Big-O:

1) lg(n):
First of all, in computer science, the favorite base is 2, not e or 10.
Therefore, lg(n) could represent the number of times which we divide n by 2 until we reach 1. This idea is the same as the number of times which we double 1 until we reach n.
2) n:
This is such a case that we have dealt with many times before. It is simply linearly increasing. For example, when we try to check whether a certain integer is in a non-ascending list, we are doing the n algorithm.
3) n^2:
Many sort strategies we have learned in CSC108 belong to this type. For example, the bubble sort is to search for the maximum or minimum and put it at the first position and keeps sorting on the unsorted parts. The step of searching will take n times, and this step will also be applied n times. Therefore, we finally get n * n times, which is n^2

Friday, March 18, 2016

Week 9 Test 2


On Wednesday, we took a test, which covered the linked list, recursion and the algorithm. Having practiced lots of recursive functions, I summarize a strategy to write a correct recursive function. The first step is to check the special condition, such as None type or inappropriate type. The second step is to consider the most basic case. Finally, for those recursive parts, apply the function to each item.

Thursday, March 3, 2016

Week 7 More about Recursion



Last week, we encountered recursion, a useful technique to solve self-contained problems. We mainly discussed how recursive functions, such as sum_list and rec_max, work on the nested list. By tracing the repeating progress from simple to complex, we first got the idea of the recursion.

This week, rather than following given functions, we tried to design recursion. However, it’s hard for me to write a correct recursive function in the beginning. My problem is every time I started to write my function, I assumed my object is a nested list. Therefore, I chose to use for-loop to deal with each element of the original list.

This strategy is wrong, because it will stop and return 1 when the first element in “t” is not a list. I tried to figure out the right way to solve the problem by comparing my codes with the professor’s and I found that the most basic case is when t is an integer, not the first element in t is an integer.


Through practicing, we came up with a general way to design recursion:
1.         Figure out what the basic case, special case, and the general case.
2.         Using if statement to deal with each case, for the general case, use recursion.


Also, I found it is really important and useful for me to write comments under each case to help me figure out whether I need to use recursion or not.

Saturday, February 27, 2016

Week 6 Recursion



This week we mainly discussed the concept of the recursion. In my opinion, recursion is a strategy to deal with a problem set, which includes self-similar problems.

A good way to learn about the idea of the recursion is to trace the function and to figure out how the recursive function works on different depth.
For the number "17", it has depth 0 and doesn't meet the if condition. Therefore, it is a basic case without any recursion, so we get 17.
For "[1, 2, 3]", it has depth 1 and meets the if condition. Therefore, we apply the recursive function on each item in the list, and finally get 6.
For "[1, [2, 3, [4]], 5], it has depth 3. By applying the recursive function three times, we finally get 15.

Friday, February 12, 2016

Week 5 More on Stack, Queue, and Linked List


We have discussed linked-list, stack, and queue last week. For queue and stack, we completed classes based on python built-in ADT, list. However, when popping the first item from the queue, because of the shortage of the list, we have to move other items one position ahead, which will cost extra work. The same situation also occurs when we try to add one item at the beginning of the queue.

Therefore, after studying Linked-list, this week we use the linked-list to represent the Stack or Queue.
For Stack, the prepend could be represented as:

For Queue, the pop front could be represented as:

Saturday, February 6, 2016

Week 4 Stack, Queue, and Liked List

In CSC108, we had learned many basic Abstract Data Types, such as list, dictionary, tuples and so on. This week, we learned some ADTs which act like list, but own unique properties.

Stack & Queue
Stack is a LIFO (last-in, first-out) list, which means it is hard to add or delete in the middle of the stack, but you have the access to the most recently added item.
stack
Queue, unlike stack, is a FIFO (first-in, first-out) list. You are able to add new items at the end of the queue, but can only remove the earliest item.



queue

Linked-List
Linked-list is the most complex list. It is actually a group of nodes.

Wednesday, January 27, 2016

class Slog:

Welcome to my series of courSe bLOGs about CSC148 (Introduction to Programming). Today, in my first SLOG, I want to discuss my encounter with PyCharm and how it helped me solve problems in the beginning of my course.

Although both CSC148 and CSC108 are using the Python programming language, they actually followed different code styles. For example, in CSC108, when designing a function, I need to write types of parameters immediately after triple quotation marks:

def func(param):
        """ (int) --> int

However, in CSC148, I should use a different way, which requires me to restate the names of the parameters and their types after the description of the function:

def func(a1, a2):
       """
       Return a1 plus a2.

       @param int a1 : a numer
       @param int a2 : a numer
       @rtype : int

It is hard for me to change a habit, but PyCharm, by pointing out some "weak warnings" or "typo", helped me figure out what the right way is when I type my codes. Moreover, PyCharm also suggests some efficient ways to rewrite my codes or clarify what I confounded. This behavior really shows me how a professional programmer thinks about codes and the way he did.

PyCharm acts like a teacher. With its help, I believe I could get used to the new style and become more professional!