Sunday, March 23, 2014

Week10 Slog

This week we have studied about sorting list by some ways such as Selection Sort, Insertion Sort, and Quick sort. The process of sorting rearranges the order of elements in a sample into a particular set. We compare the four ways of sorting below:

The selection sort takes one element at a time and places the element in the list according to the specific ordering rule. It places each element in its respective order in the list for example, 
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64

The code in python:
def find_min(L, i):
  ’’’(list, int) -> int
  Return the index of the smallest item in L[i:].
  ’’’
  smallest_index = i
  for j in range(i + 1, len(L)):
      if L[j] < L[smallest_index]:
        smallest_index = j
  return smallest_index
def selection_sort(L):
  ’’’(list) -> NoneType
  Sort the elements of L in non-descending order.
  ’’’
  for i in range(len(L) - 1):
    smallest_index = find_min(L, i)
L[smallest_index], L[i] = L[i], L[smallest_index]

The insertion sort is similar to the selection sort in the sense that it takes one element at a time and put it in the list. However, the insertion sort maintains a sublist, which is an incomplete list, at the beginning of the list. Each new element is then inserted into the sublist by comparing it with the elements already in the sublist according to the ordering rule. For example: 
Sort the list : 12 3 6 2 1  
12 3 6 2 1
3 12 6 2 1
3 6 12 2 1
2 3 6 12 1
1 2 3 6 12

Code in python:

def insert(L, i):
  ’’’(list, int) -> NoneType
  Move L[i] to where it belongs in L[:i].
  ’’’
  v = L[i]
  while i > 0 and L[i - 1] > v:
L[i] = L[i - 1]
i -= 1 L[i] = v
def insertion_sort(L):
  ’’’(list) -> NoneType
  Sort the elements of L in non-descending order.
  ’’’
  for i in range(1, len(L)):
insert(L, i)

The quicksort is the fastest method in sorting elements into a list because it has a lower amount of comparisons. This method arranges the elements by finding a mean and sorting the elements as bigger or smaller than the mean. Then we take each section of the list and find another mean to compare the elements to. We do so until the list is fully arranged. 

The  Mergesort divides all elements into groups of two. It can orders each group according to the ordering rule. Then, it compare two groups by comparing the first element in each group and placing it in a new merged group. It does so until the elements have been merged into one group. 

Code:
def merge(list1, list2):
  lst = []
  i=0
  j=0
  while i < len(list1) and j < len(list2):
      if list1[i] < list2[j]:
          lst.append(list1[i])
          i=i+1

      else:
          lst.append(list2[j])
          j=j+1
  lst.extend(list1[i:])
  lst.extend(list2[j:])
  return lst
def mergesort(lst):
  if len(lst) <= 1:
    return lst
  mid = len(lst) // 2
  left = lst[:mid]
  right = lst[mid:]
  left_s = mergesort(left)
  right_s = mergesort(right)

  return merge(left_s, right_s)

Sunday, March 16, 2014

Week 9 Slog

 This week we have studied the Anagrams, which means letters of one word can be rearranged to be become another word. For example, horse and shore are anagrams. If a word is not in the English wordlist, it is not an anagram, such as seohr is not an anagram of horse. In this week, we have tried to find a easy way to find all Anagrams by using a short amount of time. We used the Timing runtime to get a sense of an algorithm's efficiency. Firstly, we should know what are the permutations of one word. For instance,  the word 'dog', the permutations are: dog, dgo, odg, ogd, gdo, god. So 3 letter, 6 permutations,  4 letter, 4*3*2*1 =  24 permutations , 5 letter, 5! permutations ... 120 permutations. As you can see  that is a huge workload. We have also proved this formula. The proof is a very complicated and long process but I found it very interesting. 

Sunday, March 9, 2014

Week 8 Slog

This week we study the BST what is Binary Search Tree. BST is different with Binary Tree(BT). The BST must is a BT, but the BT is not BST. A BST's node must has a value and every node value is greater than the values of all nodes in its left subtree, and less than the values of all nodes in its right subtree.There is a example to show how do the BST work:

BST:
                  8
        2                10
                      5         11
                  3      6

if we research the number 12, 
then we follow this order:  8  is less than 12, then turn right it is 10, and it also less than 12, then turn right it is 11 , but it is less than 12, and 11 has no right ,so this BST has no number 12.

As you can see, the BST is easy to research any numbers.

Sunday, March 2, 2014

Week 7 Slog

First of all, I want to say that this week's midterm is not fair because each question is worth 25% of our mark. I think there should be more questions that can test our knowledge on different levels. I felt like I was going to fail the test the moment I saw the first question. I have studied very hard for this test but those questions are just too hard for me to understand. And as my intuition told me, I did fail this midterm. I love computer science but this course is draining every single drop of love I still hold for this subject. Why do the things I love not love me back? I am very sad. I might drop this course.

This week we have studied the Tracing Recursion and Linked List.  The Linked List is the same as the Queues because we need to add or remove heads. I don't understand why we need to learn this.  It is useful ? It seems very easy and we would be able to figure this code out by ourselves. I didn't even want to go to class this week. However, despite not liking the seemingly easy material, I motivated myself to attend class. After I sat in class, I thought I could just breeze through the problems. However, after listening to the professor and reading some of the problems, I have realized how hard this material actually is. Indeed, it is very useful to link lists together. It is also very hard to code that link. Because of the midterm this week, I have newly found confidence in this course. I have a little bit overconfident when we had our friday class. Hence, I think the lesson this week is to never underestimate the difficulty of programming.

Sunday, February 16, 2014

Week 6 Slog

This week I have studied the Python Motivating Trees coding. This method is a fun way to show the relationship between things. In this week’s lab I have studied the list comprehensions that are easier than normal ways. Indeed, this method can allow us to compress document size through less inputs. We can express the same things through less codes, and this would effectively increase programmers’ efficiency. I think that this method is very useful for research purposes. For example, I can create a timeline using the motivating trees method and place alternative events that happened or could have happened in history. Then I can run a textual analysis on the tree to filter the course of history. This way is a good calculation of the parallel dimensions of our world. 

Friday, February 7, 2014

Week 5 Slog


I have watched a game show these days, the game requires a player to remember the shape of 20 keys and their respective locks, the MC then picks a lock randomly, and the player needs to find the key that will open the lock. I have thought of a way to quickly find the matching key using tracing recursion. We can set the trough as 0 and the crest as 1. Then each key will have a particular set of binary series, which can be transformed into a real number. We can use this number to match the key to its lock. Through this method, we can easily win this game. Therefore, the tracing recursion that we have learned this week is very useful. It can help me win this game if I participate in this show.

Friday, January 24, 2014

Week 3 Slog


    This week, I spent a lot of time doing my exercises for the course. I feel that I did a very good job because I have seriously thought about every question. I have also tested all my answer and they were right. However, when I got my grade, I was very disappointed because it was way below the grade that I have expected. Nevertheless, I have learned valuable knowledge through these exercises. In class, I think that exceptions in programming languages are very interesting. I did not know that there were so many different ways to handle exceptions. Overall, this week I had a great experience learning computer science and exceptions.