Saturday, 4 April 2015

Another slog for week 11

    Oops, I just saw the slog requirement for this week, so I decided to write another one.
    Here, I am revising the slog of week 8, where I said there can be another way of writing recursion rather than just  (...(c) for all the c in ...).
The other way, as stated in week 8 slog, is like this:
if root is None:
        pass
    else:
        inorder_visit(root.left, perform)
        perform(root)
        inorder_visit(root.right, perform)
In this way, every time we call on this method, if the node, which is the root of the subtree, is not None, the method will be again call on the left and right of that node in order. Well, it has been a long while since I read that slog. I found that the original way of writing recursion has left me with such deep an impression that it is the only way I can think of when dealing with recursion. I need to practice more on the second way. Also the use of eval and perform is also worth looking back on. I am glad I am reminded to revise my slogs, it is a great way to reinforce my knowledge and prepare for my test. 
   
So long, farewell!
  Dark Fox

Slog for week 12

    School is over, but this will not  be my last slog. Over the semester, I changed from someone who was even afraid of hearing the name 'computer science' to a big fan of it. It was of great help writing slogs to keep track of my questions and my progress. It encouraged me to learn more so that I can write more.
    This course is of good pace, and not very hard to follow in general. However, when TAs were on strike, it was a  real challenge for me to work things through without their help. I was kind of slow at getting the ideas taught in class, but the labs were really designed for people like me, to practice and examine my ideas.
    Over the semester, I have learned about class and subclass, stack and queue, nested list and tree(binary tree and best sorted tree), efficiency and most importantly, recursion. The reason why I think recursion is the most important is that it is the base of many other concepts listed above, and to be honest, it is really hard for me. Even though I have managed to use it right for a lot of times, I still need much practice: everytime when I am using it, it will still take me a lot of time to think carefully about it.
    Through this course I also met some good friends, like my assignment partners, who are really lovely. I love the way we worked together:  the brainstorm we had, the debugging process and the way we helped each other out.
    This course has got me interested in computer science every since I saw the professor using the turtle to draw something. I started to have creative ideas and use programming knowledge to design things for other subjects, such as a class of matrices in math and a program to simulate wave interference in physics. The more I learn, the more excited I am seeking for more.
    So I make a commitment here, though this is the last computer science course for my subject requirement, I will still keep learning computer science, either through other courses or on my own. My Slog journey has just begun.


Love
Dark Fox

Monday, 30 March 2015

slog for week 11

  WOW! BIG NEWS! The strike is finally over! I can't wait for the tutorial next week to see my lovely TA.
   This week we learnt about efficiency of sorts. We talked about bubble sort, selection sort, insertion sort and quick sort. Just a reminder, since it always happen to me that I mess these terms up, here are the illustration of these sorts.
 

 Looking directly at the illustrations may be the best way of understanding and memorizing these.
Efficiency is simply how the time to sort a list of n elements varies with n.
In the pictures for selection sort and insertion sort, it is already given the efficiencies and the way to get those.
For bubble sort, which in my opinion is the simplest one, it needs to go through every element after the starting one, so
it is just (n - 1) + (n - 2) +... + 2 + 1 which is 0.5 * n * ( n - 1), the efficiency is n ^ 2.
And for quick sort, it is a little complicated, where we have to think of whether the sort is well-balanced or not. In the well-balanced case, we divide the list log(n) times, and in the worst case, n times.
In both cases, we need to go over the whole list (except the pivot points), which is proportional to n. Thus, the efficiency is nlog(n) in the best case and n^2 in the worst.

Above is my summary of this week. Thank you and goodbye.

P.s. CAN'T WAIT FOR THE TUTORIAL!!!

Dark Fox

Friday, 20 March 2015

slog for week 10

  This week we learnt about assertion. In the slides it is written in the form as below:
      assert <condition> , <error_string>
---> if not condition: raise AssertionError(error_string)
I understand this form but I do not really see the point of this. Is it just a simpler form of if statement? Why and where do we use it? It is disappointing that I did not find any real examples in the course material. And there was no TA in the tutorial either. When I asked my friends, the answer was not clear and I can tell that this is not just my problem.
   Besides the course, I also used computer science elsewhere, which is really cool to me. In physics, we had practicals using python and vpython, to make the diagram and animation of waves. And for linear algebra, my friend and I tried to create classes of matrix, vector and vector spaces. And we even tried to write codes so that it will solve the polynomials we input. I have been focusing on learning how to use Sqlite these days. I think I am slowly falling in love with computer science. This is a great change of mind, since I used to hate it somehow. Now, the feeling that I can actually command the computer to do what I want in a much easier way than doing by hands is of great satisfaction. I hope I can learn more about coding, and use it more. 

   May the passion be always with me. And I will try to figure out the assertion of course.

  Thanks for listening to me.
  
  Tschuss!

  Dark Fox

Thursday, 5 March 2015

slog for week 9

   This week we learnt about linked lists, which we can think of in two ways.

1. as lists made up of an item (value) and a sub-list (rest)
2. as objects (nodes) with a value and a reference to other similar objects
   Then we started to implement the methods. At first, this process is kind of difficult since it is easy to lose track of what I was doing and what I should do next. I have to set an example for myself, and keeping turning to it. Then the professor told us to draw pictures while writing the code. That really helped. When using this method, I would not lose track as much and would not forget anything that I should do.
   It is so sad that we did not have practical this week. Computer science practicals are always the ones I love the best; the TA is really helpful and patient. I learnt a lot from the practicals past few weeks. Practice makes perfect, even if it will not make perfect, at least it can help you on the way to perfection. To get some practice, I went to the lab as usual, even though the TA was not there. I worked out several problems with the help of my friends, and had a better understanding of the topic. From merely imitating the code given by the professor to thinking independently, I learnt a lot.
For example, linked list can not be copied, I figured that out when I was trying to copy a linked list and it raised an error.
   Still have some  problems left undone. Thanks to my friends who have helped me a lot on the practical problems. I am going to work harder on it, and I will have my second term test next week.

Good luck to me and to everybody

Ciao
DarkFox

Wednesday, 25 February 2015

slog for week 8

This week we are learning about binary trees, in which each node only has at most two children.
 class BTNode:
    ’’’Binary Tree node.’’’
    def __init__(self, data, left=None, right=None):
        ’’’ (BTNode, object, BTNode, BTNode) -> NoneType
        Create BTNode (self) with data
        and children left and right.
        ’’’
        self.data, self.left, self.right = data, left, right

The use of the last line is smart!

Once we knew what a binary tree is, we learnt some methods used in this structure, for example, to evaluate it. The use of eval is said to be important, as in this line:
return eval(str(evaluate(b.left)) + b.data + str(evaluate(b.right)))

eval(...)
    eval(source[, globals[, locals]]) -> value
    
    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.

However, I still have problem understanding how to use the 'eval' function. What does global and local refer to? I think I'm going to ask the TA in tomorrow's practical.

Furthermore, I learned the three orders, which are inorder, preorder and postorder.
Inorder is from left to right, as it was given in the class:
if root is None:
        pass
    else:
        inorder_visit(root.left, perform)
        perform(root)
        inorder_visit(root.right, perform)

Preorder is to evaluate from the root to the leaves, which goes like this:
        perform(root) 
        preorder_visit(root.left, perform)
        preorder_visit(root.right, perform)
And postorder is skipping the root and evaluate the leaves first, which I find a little bit hard to keep track of while tracing the order using a picture. However, it gets a lot easier just using the codes. That's to say, well, computer is smarter than me...
        preorder_visit(root.left, perform)
        preorder_visit(root.right, perform)
        perform(root) 

I also find it interesting that a recursion can be written in the way above and not necessarily using the form of return (...(c) for all the c in ...)

Above are the things I've learned this week, and more left to do for tomorrow's practical.
Fighting Dark Fox! 
Ciao!

Dark Fox

Saturday, 21 February 2015

slog for week 7

    This is reading week!
    Looking back on week 6, I have learned some interesting but also difficult things about tree.
 I have learned a lot of terminology. As a reminder, a tree has root, nodes, edges, leaves and subtree.

A path is a sequence of nodes n1, n2,..., nk, where there is an edge from ni to ni+1. The length of a path is the number of edges in it. The path is unique and it has no cycles.

The height is 1 + the maximum path length in a tree. A node also has a height, which is 1 + the maximum path length of the tree rooted at that node.

The depth is the height of the entire tree minus the height of a node is the depth of the node.

The arity or branching factor is the maximum number of children for any node.

After learning the terminology, I was taught to design codes to count leaves, count nodes, calculate the height and so on. An important method we used was recursion. First detect the base case, and then write the code for general case involving recursion. I could understand the code if it was already written, but it was hard for me to write recursion myself. In the practical this week, my partner and I only worked a few out with the help of our TA and piazza, which was so sad. Understanding what we should do was not the hard part, it was how we should do it that mattered. I think I still need some time to think about this carefully and try to catch up. If I could get the access to the answer to the practical after the every practical is done, that would be of great help to my study. After all, we first learn to imitate and then to think independently. I still need some more examples to grasp the clue of doing this.

Also I learned a format:
self.children = children.copy() if children else []
This is a functional 'if'. it means copy children if not none. The reason why we need to copy children 
is that we don't want to make change on the original data so that our counting method will not affect 
anything. 

I will work on recursion more and get a thorough understanding. I believe I can make it!~

Ciao my friend,
Dark Fox

Wednesday, 11 February 2015

Slog for Week 6 on Object-Oriented Programming Concepts(want to be marked on)

    When I first saw this topic, I had totally no clue what I am going to write. Object-oriented programming, what does that mean? I looked it up online, and it turned out that I have been dealing with this for a long time without even realizing what it is called.
     Let's start with class. Class is an object that can be created by ourselves. So far I've learned superclass and subclass, which are like parents and children. The children can inherit, extend or override the parents' methods. It is really convenient to have several subclasses to implement the super one. In this way, the whole program is clear and flexible, we can edit the subclass without changing the superclass and work on more details specific for each subclass. A subclass can literally inherit everything that is in the superclass. Whenever a change is needed, it can just modify the relating method by either extending or overriding. Overriding is just write the method again ignoring the superclass while extending is to cite something from the superclass and extend more codes for that. And don't forget to raise an NotImplementedError when the superclass is not able to define a method without a subclass. Just to take up the space for the subclasses to override.
    So what is a method? According to my understanding, a method is a piece of code associated with the class, while a function is independent of the class. The basic methods I have learned include __init__,  __str__,  __repr__,  __eq__, __contains__...  

For __init__, we have to be careful what variables should we put in the bracket: if the object comes with the parameters, then those parameters should be included as variables in the __init__bracket.
For the parameters we assign to a class, then we can simply include such parameters along the way.
I know my explanation is awful, let's use an example:
 def __init__(self, player):
        ''' (GameState, str) -> NoneType
        
        Initialize new game state self to have state which player plays next
        and a list for legal moves.
        
        >>> g = GameState('computer')
        >>> g.state
        'computer'
        >>> g.legal_moves
        []
        '''
    
        self.state = player
        self.legal_moves = []   
This is from assignment 1. Here the list of legal moves is something the object is not born with, it's gained along the way, so we just include it rather than putting it in the bracket.

Then, to distinguish __str__ and __repr__, below is a useful example:
  import datetime
  today = datetime.datetime.now()
  >>>str(today)
  '2015-02-11 09:56:20'
  >>>repr(today):
  'datetime.datetime(2015, 2, 11, 9, 56, 20)'

In short, __repr__ is to make be unambiguous and __str__ is to be readable.

And also,  for __eq__, it's not just all parameters should be equal to each other, we should also include the comparison of the class of two objects, using isinstance:
   return is instance(self, other) and self.a == other.a...
This need to be remembered, since I have made a mistake on this.

And with __contains__(do not forget the 's' at the end), we can simply use 'a in b' rather than 'b.__contains__(a) ' after defined.

It's important to implement the classes we created with the methods above, they can make your future steps a lot easier.  

Above is all I can think of object-oriented programming concepts. 

Tschüss!

Dark Fox


Monday, 9 February 2015

another slog for week 5

   Well, I just saw on the course website that we need to write our impression on tracing recursion, I hope it's not too late.
    Recursion, an interesting idea, which requires a good thinking and can simplify the loops by a lot. It's much like looking at yourself in mirrors and get repetitive image. The thinking process, namely the tracing, is important in recursion, since the language here is a lot more concise.

below is a brief example of recursion shown in practical





def nested_concat(L):
    ’’’(list or str) -> str
    Return L if it’s a str, if L is a (possibly-nested) str return
    concatenation of str elements of L and (possibly-nested) sublists of L.
    Assume: Elements of L are either str or lists with elements that
            satisfy this same assumption.
    # examples omitted!
    ’’’
    if isinstance(L, str):
        return L
    else: # L is a possibly-nested list of str
        return ’’.join([nested_concat(x) for x in L])
  1. nested_concat(’five’) --> ’five’ # since isinstance(’five’, str)
  2. nested_concat([]) --> ’’.join([nested_concat(x) for x in []])
            --> ’’.join([]) # there are no x in []
            --> ’’          # that’s what join does to empty list
  3. nested_concat([’how’, ’now’, ’brown’])
            --> ’’.join([nested_concat(x) for x in [’how’, ’now’, ’brown’]])
            --> ’’.join([’how’, ’now’, ’brown’]) # traced nested_concat(str) before
            --> ’hownowbrown’ # join does ’how’ + ’now’ + ’brown’
    
  4. nested_concat([[’how’, [’now’, ’brown’], ’cow’]])
            --> ’’.join([nested_concat(x) for x in [’how’, [’now’, ’brown’], ’cow’])
            --> ’’.join([’how’, ’nowbrown’, ’cow’]) # traced nested_concat(list of str)
    
                                                    # and nested_concat(str) before
    
            --> ’hownowbrowncow’
    
  5. nested_concat([[’how’, [’now’, ’brown’, [’cow’]], ’eh?’]])
            --> ’’.join([nested_concat(x) for x in [[’how’, [’now’, ’brown’, [’cow’]], ’eh?’]]
            --> ’’.join([’how’, ’nowbrowncoweh?’]) # believe docstring for str and possibly-nested
    
                                                   # list of str or possibly-nested...
    
            --> ’hownowbrowncoweh?’
    
    
    
    
    When I first saw this example in practical, I raised a question. While tracing the 5th example, is put join([’how’, ’nowbrowncoweh?’]). which I think is wrong, and it turned out that I was right. Our course materials sometimes also make mistakes, eh?
    through the tracing process, it is also important to remember not to trace too much, just simply use the result from before, otherwise the tracing will be so long... well, that's the difference between a computer and a human brain, we don't want to make things complex and that's why we developed computer programs.

    So long, Ciao!
    Dark Fox

    Ps: don't forget tp keep track of the brackets while tracing, and make sure what type of object you are using!

Saturday, 7 February 2015

Slog for week 5

       This is the 5th week of school, and we had our computer science's term test , which worth 10%.
       I was kind of worried at first before the test when I took a look at the past test. There was questions about stack on that, but I've only just learned stack on Monday. I finally realize how important is the practical, it helps me understand and use the concept better. However, the good thing is , stack was not on the test. And after that, in our practical, I got to use stack and queue, which helped me a lot, and those don't seem hard any more. Well, last in first out, first in first out, I could actually use my imagination of what a stack and a queue are like.

As the pictures show, we can only take the last added stone off the stack, but for a queue, the person comes the first should go first, since he has been waiting for so long.

Using the pictures and imagination to learn is really a good idea. Being relating the new knowledge to some everyday stuff make it a lot easier to memorize. No, I don't even need to memorize a thing , whenever I see the same thing, I can easily repeat the understanding process and I'm sure I won't get it wrong.


Back to the test, hmmm, I would say it's not hard. However, a friend of mine told me that we're probably wrong for the first question, to make the dictionary as {'email': 'respond'} rather than {'yes': ['...']} I'm not sure, well, i forgot what was the question exactly. gonna turn back to this later when I have my test back. I'm now crossing my fingers for that. Amen!

Thursday, 29 January 2015

Slog for week 4

        This was a difficult week. The assignment is a huge burden for me, however, I’ve learnt a lot during the process.
From the very beginning, when I first looked at the information sheet, I had no idea how to even begin. We had to design a game? English reading is not that easy for me, a non- English speaker. I questioned myself about my understanding as I went along. But that actually helped me to understand the assignment better though a lot more time was taken. It sounded like mission impossible.

First, I made several attempts. All were unsuccessful.
Then, salvation came. One class, our professor introduced us to subclass. That was such a relief that I started to have a clue about how to do this. Though still some questions remained, I could get started. I tried writing the code by myself. Then, I exchanged ideas with my teammates. We met 3 times for this in total.
The assignment was a great way to put theory into practice. I found I learned more about the topics we learn in class, because here we get to design codes. From this, I learnt what a class should look like, what a parameter should be in the class and what should be added afterwards. I learnt overriding, inheriting and extending the superclass in the subclass.
When I finally finished, the sense of achievement is incredible. I can’t promise that I have a super good mark for this, but at least I can say that I’ve actually tried my best and gained a lot. Special thanks to my teammates; we had a wonderful time together.

Dark Fox

PS: And also, after seeing the example for recursion using a turtle, I think I should try it myself and define a method to draw snowflakes. That must be so cool!





Thursday, 22 January 2015

Week 3, Why geeks should write

   Why geeks should write
    Everyone can be a geek if (s)he has the passion. According to Wikipedia, geek means an expert, enthusiast or a person obsessed with a hobby or intellectual pursuit.
    For me, my friends often call me a culinary geek, which I take as a compliment. After thinking of myself as a geek, I actually started to write a blog on food. Whenever I taste something great, cook something fancy, or have an extraordinary experience, I would write it in the blog. I would use it to share my feelings, mostly of excitement, to keep track of my progress, or even to introduce the things I like to my friends.
    I think geeks share a lot in common.
    Writing is precious to geeks, because we write to express ourselves. For someone so enthusiastic, the feelings we feel can sometimes be so strong that we would want the whole world to know.
    Geeks should write to remind ourselves of all those special moments or event they've gone through. It's like taking selfies from time to time. After, when you look back at the pictures, you are reminded of your own history. They can spark memories. They can be a great treasure trove in the future.
Furthermore, geeks should write to share knowledge. As a geek, one may often know things that most people don't really know. Sharing ideas and knowledge of those subjects can help a lot of people on their way to becoming geeks.
For me, I enjoy writing most when I introduce new dishes to my friends and see their joy. I believe to make more people understand what you like, is to make yourself less lonely.
    And that is why all geeks should write.