2015年2月9日星期一

Week 5: Tracing recursion

  This week we introduce trace list.

def sum_list(L):
   """
   (int or arbitrarily nested list of int) -> int
   Return the sum of all ints in L.
   >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]])
   36
   """
   if isinstance(L, list):
        return sum([sum_list(x) for x in L])

   else:
        return L

  This is a recursion exercise in class. From the example we trace the list and to see how python works and caculate their sum.
  You may find that recursion is useful when a function calls itself. We could not type it again and give it to be sloved by python itself.

  Based on this we define insert method which use recursion :
def insert_after(L, n1, n2):
    """
    (list, int, int) -> NoneType
    
    After each occurrence of n1 in L, insert n2.
    
    >>> L = [5, 1, 2, 1, 6]
    >>> insert_after(L, 1, 99)
    >>> L
    [5, 1, 99, 2, 1, 99, 6]
    """
    
    i = 0
    while i < len(L):
        if L[i] == n1:
            L.insert(i+1, n2)
            i += 1
        i += 1

if __name__ == "__main__":
    import doctest
    doctest.testmod()
 
  From which we are more clearly know about how trace recursion.
  So far, we are not  how to write these recursion code, but from trace them and exercises in handout we could be more familiar with them.
  Recursion mind is really helpful when writing code. It reduces a lot steps which gives us a new view to look codes.

Week 4: Impression of first few weeks

  It has already one moth passed from we start learning csc148.
  During the first four weeks, we catched many important and interesting method of O-O design.
 
  First, the special method:
    __init__ which construct an instance for the class
    __str__ which return a string decribing the instance
    __repr__ which also return a string but construct an object with same value
    __eq__ which compare two instance with ==
  They are fresh to me and gradually let me know how these methods and classes work.

  Then the proprty mechanism for a class and see how it works.

  We also talk about ADT( Abstract Data Types). We define data and operations on it so that we can generalized complex problems into categories. And to deal with these "different" problems we can easily find similar method to slove them.

   The stack contains various of sorts. New items are pushed into stack and old items are popped from the stack. But it is carefully for us to check whether it is empty before we removed iterm!

  We talked inheritance in the following week. Professor use company-employee-hourly/salary employee examples for us to show how a child class can inhert from its parent class and add new methods which the "shared" parent didn't have.
  These were fabulous! Due to parent classes, we can creat a class more simplely and precisely and easily to give them new features.

2015年1月26日星期一

Week 3: Why geeks need to knw how to write?

  Why geeks need to write? The first time I met this quetion inspires me a lot.
 
  As students always be sperated into two groups: art and science. Art mind people may like writing articles but when they look at things full of numbers and function, they freak out. However, geeks may be the opposite. They like things full of numbers, functions and logic words rather than rhtoric and metaphor. But indeed wrting code is same as wrting articles which both need writers use the least words to express the most beautiful conponents to their readers. Although the audience might be different, the main purpose is to let their audience get essence of what they were wrting.

  Actually the greast scientists are always the good writer. Their essay are like their codes: logical, fluent and easy to understand. That may the reason why geeks need to write. Geeks and writers both writing things begin at blank paper, using logical words to explain what they want to say to their specific listeners.
 
  We all know that when we are wrting, the process may not be smooth as we expected. We might change and change again until the article meets our expectation. Sometimes we even do not know how to express our opinion. It same happens to geeks when they are writing code. Software can not be absolutely completed. They can be impoved and impoved by different people. So geeks need to spend more time on coding improvement, adding new funtions even changing the old part with new ideas. A clear and logical mind helps a lot when they are writing something not just writing articles.

  Moreover, wrting is also important for geeks to memorise ideas. A good habit of recording things helps geeks to remind past thought. Do not look down upon the thought in the past, they may inspire geeks a lot in a specific time. So writing helps to keep recording of these ideas which might come in handy some day. That is why it is so worthing for geeks to write.

2015年1月22日星期四

Week2 : CSC148

  CSC148, a computer course which advises us to creat a blog to share ideas of our daily thought.

  This is the first time I use blog to write things, I am really exciting about what will happen in the furtue. For this blog, it is used for sharing ideas and improving our computer expriences at the following time.

  Hope to share opinion with you and let us work together~