Sunday, 5 April 2015

Week 12: Last post

Before I begin to talk about my last impression about the Slog, I promised you guys with the answers to the functions.  (Hope someone actually tried and has been looking for the answer!)

A) 
def __contains__(self, value):
    ''' (LinkedList, object) -> bool

    Return whether LinkedList contains the value.

    >>> lnk = LinkedList()
    >>> lnk.prepend(0)
    >>> lnk.prepend(1)
    >>> lnk.prepend(2)
    >>> lnk.__contains__(1)
    True
    >>> lnk.__contains__(3)
    False
    '''
    current_node = self.front

   while current_node:
        if value == current_node.value:
            return True
        current_node = current_node.nxt
    
    return False


B) 
def delete_back(lnk):
    ''' (LinkedList) -> NoneType

    Delete back node of lnk, if it exists, otherwise do not change anything .

    >>> lnk = LinkedList()
    >>> lnk.prepend(5)
    >>> lnk.prepend(7)
    >>> print(lnk.front)
    7 -> 5 ->|
    >>> delete_back(lnk)
    >>> lnk.size
    1
    >>> print(lnk.front)
    7 ->|
    >>> delete_back(lnk)
    >>> lnk.size
    0
    >>> print(lnk.front)
    None
    '''
    if lnk.size > 0:
        prev_node, cur_node = None, lnk.front

        while cur_node.nxt:
            prev_node = cur_node
            cur_node = cur_node.nxt
        lnk.back = prev_node

        if lnk.back is None:
            lnk.front = None
        else:
            lnk.back.nxt = None

        lnk.size -= 1


C)
def append(lnk, value):
    ''' (LinkedList, object) -> NoneType

    Insert a new node with value at back of lnk.

    >>> lnk = LinkedList()
    >>> lnk.append(5)
    >>> lnk.size
    1
    >>> print(lnk.front)
    5 ->|
    >>> lnk.append(6)
    >>> lnk.size
    2
    >>> print(lnk.front)
    5 -> 6 ->|
    '''
    new_node = LLNode(value)

    if lnk.back:
        lnk.back.nxt = new_node
        lnk.back = new_node
    else:
        lnk.back = lnk.front = new_node
        lnk.size += 1


D) 
def prepend(self, value):
   ''' (LinkedList, object) -> Nonetype

    Insert value at front of LLNode.

    >>> lnk = LinkedList()
    >>> lnk.prepend(0)
    >>> lnk.prepend(1)
    >>> lnk.prepend(2)
    >>> str(lnk.front)
    '2 -> 1 -> 0 ->|'
    >>> lnk.size
    3
    '''
    self.front = LLNode(value, self.front)

    if self.back is None:
        self.back = self.front
    self.size += 1


E) 
def print_backward(lnk):
    '''(LinkedList) -> str

    Print the Nodes in the LinkedList backwards.

    >>> lnk = LinkedList()
    >>> lnk.prepend(0)
    >>> lnk.prepend(1)
    >>> lnk.prepend(2)
    >>> print_backward(lnk)
    '0 -> 1 -> 2 ->|'
    '''
    if lnk == None :
        return print_backward(lnk.next)

    print(lnk)

----------------------------------------------------------------------------------------------------------------------------------

I can’t believe that this is the last post of the CSC148 Slog. 
There were many events happened throughout the entire year, good and bad. 
I cannot forget the moment my colleagues and I got 95% on first assignment, and the time when I received 88% on my first test. 
I also cannot forget how badly we did on the second assignment because we underestimated how time consuming A2 was. 
However, from the mistakes, I focused on writing slogs in more detail and willing to seek for help in Piazza & TA. 
Then, I received 100%, my first 100% in university career, and I cannot explain how rewarding it felt like. 
After that my colleagues (Amir and Matt) worked hard with me to ace the third and last assignment and I believe we did a great job on it.

CSC148 was an introduction class but some parts were still challenging.  I must say that keeping up with posts is not the easiest thing to do, but if you manage to do so, I guarantee that you will achieve fairly good marks in the course.  It does not have to be computer science course; it can be any courses like math or statistics, anything. 

Comparing myself from last semester when I disliked the concepts of Slogs, my views on Slogs are 180 degree different.  I am planning on sharing my knowledge I consume in class by posting it online and I truly believe that it will improve my understanding in courses in the future.

It was a great journey with myself, Professor Horton, Professor Heap, my colleagues, and the readers, but I believe it is a time to put an end in CSC148 blog.
It has been a great semester with you all and I wish you the best in whatever you do in the future for those who read this.

Cheers,
SeungHyun (Sonny) Kim


P.S Don’t forget to check out these blogs as well, they are very interesting.

http://bloggermcbloggerman.blogspot.ca -> has a very cool design
https://uberweirdesuu.wordpress.com -> very personal, check it out if you want to see personal style of posts!    

Week 11: Revisiting my posts (Reflections)

This week, there are no coding explanations but I will talk about my views on previous posts I have been posting.  Since there is only one week left till the end of this course I feel like this is a good way to sum things up. 

Slogs did not come off very friendly to me at the beginning.  I struggled to create good blog posts last semester with CSC165 course and that was why.  
When Professor mentioned about slog, I started to stress out about it, though I wanted to push myself to do well on it (Challenge accepted).

First slog had a topic about “Why geeks should write” and I talked about why writing (not the code, journals) is extremely important to coders or computer scientists these days.  
These days programming language is easy to learn (many websites have tutorial videos and problem sets with solutions) and knowing programming language is not the most important skill computer science should have anymore.  
Programming language skills are basic requirements and now work forces demand good communication skills.  
Also besides my points in the post, work places hire people who they want to work with.  Programming knowledge can be improved even in the companies but socializing skills cannot really be picked up if people don’t suit in the environment that was asked.  
I wish I talked more about the reasons why communication skill is more important than anytime before.
However, considering it was my first blog post, I am not as disappointed at the length of the post.

Slogs with weekly class impressions were the major topics in this assignment I should say.  These really helped me out reviewing the materials that had been covered in classes.
Starting from Class and Subclass, I had a hard time understanding the concept of it, but as I wrote blog post about it, I got to understand better about class and subclass.  
I know that I mentioned about going on a YouTube but I believe there are more useful sources than that.  
You can ask your colleague for help, or even a professor or teaching assistants.  
The trend I noticed, however, was that the length and the depth of the materials I cover increased every week.  

I believe I did an average job in explaining Objective Orienting Programming but I really could have done better.  

Binary Tree concepts had been covered very nicely and I think this was the start of myself knowing where my blog posts are heading.  
I mainly wanted to focus on explaining the concepts in my words to others to test / express my knowledge on the materials.  That is why I highly doubt that my post about Linked Nodes or Doc test is the masterpiece or the best post in my CSC148 blog (I was even mentioned by one of the other bloggers for Linked Nodes, http://juliansongcsc148slog.blogspot.ca). 


Looking back at my posts, I saw good and bad things I have done in writing slogs over the semester.  
But, I really see the progress in my posts and I am really proud of it.  
Even if class does not require us to write slogs, I am more than happy to continue writing slogs about class materials I cover and continue explaining.  
I am really passionate about teaching, and I believe slogs can be the opportunity for me to do so other than becoming a teaching assistant.

Thank you for reading my posts as usual and hope you guys have a nice day.
Next week, I'll comeback with my last post of this SLog.

Week 10: Doc string and Doc test

"""(late posting) -> str of apology letter

To readers,

[I apologize for the late upload.  This post was on as a draft and I forgot to press the “publish” button after editing it.  Hope you had more time to work on the functions I gave out.]   

Cheers,
Your blogger
"""

In Week 9, we mostly reviewed the second assignment with learning the testing methods.  Coding becomes useless if it cannot be run, and if it does not run properly either, it also becomes useless.  This is why testing method is very important that we were required to write doc test in every assignment we did.  For people who do not know what doc test, doc test is a test example showing how the function/method works written in doc string area.  For example, let’s say we made a function that adds two numbers.  It will be like:

def add(i, j):

    return i + j

Coding part seems right but it feels like something is missing.  To make this function more user friendly to people who will use your function later, employers, clients, or other scientists for example, you may want to add some explanation of how this code works.  You always should keep in mind that sometimes users are not familiar with any computer languages.  So it is the best to think of explaining to someone who does not know anything about programming language.

Therefore, doc string is essential in writing functions/methods.
Doc string has three elements: (Input Type) -> Output type, information, doc test (examples). 
Also, it is important that you put 3 [ " ] or [ ' ] so that you can write multiple lines of comments (this will not affect any codes when you run the program unless you specially demand to).

The layout of doc string will be:

def some_function(parameter1, parameter2):
    """ [(input 1 type, input 2 type) -> output type]

    [This part will simply explain what this function/method does and you can add instructions of the function/method or restrictions for the parameters.]

    >>> parameter1 = something
    >>> parameter2 = something
    >>> some_function(parameter1, parameter2)
    [Where you write output]
    """

    [body of the function goes here]

Before I write an example of full function with doc string, if your function/method returns NoneType a.k.a. returns nothing, you are not required to write doc test for that specific function/method.

Now, going back to our add function, with proper doc string, it will look like:

def add(i, j):
    """ (float, float) -> float

    Returns a sum of two numbers i and j.

    >>> i = 2.7
    >>> j = 3.4
    >>> add(i, j)
    6.1
    """

    return i + j

I wrote inputs’ types as float because we did not specify what the numbers we are adding.  When two float types are added, the output will be float type as well and that is why I wrote float in output type spot.

Let’s say we want to make a function that only adds integers.  Then,

def int_add(i, j):
    """(int, int) -> int

    Return a sum of two integers.
   
    Condition:
    Both i and j should be integer-type.

    >>> i = 3
    >>> j = 4
    >>> int_add(i, j)
    7
    """

    if not isinstance(i, int) or not isinstance(j, int):
        raise TypeError("Needs integers!")
    else:
        return i + j


This function only takes integers as input and if something else has been used as a parameter, it raises a TypeError.  When you implement this TypeError, and test out the function to raise the error, you’ll get something like this on Python Shell:

>>> int_add(3.0, 5)
Traceback (most recent call last):
  File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
    # Used internally for debug sandbox under external interpreter
  File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 19, in int_add
builtins.TypeError: Needs integers!

Now we have covered the raising errors, and doc string with doc test, we should know how to ‘properly’ test the function.

Let’s look at our int_add function.  The function only adds integers and raises an error if numbers are not integers. 

Let’s say two students Ted and Ned want to write a doc test for this function.

Ted’s doc test examples look like this:

int_add(3, 4)
int_add(0, 4)
int_add(3, 0)
int_add(0, 0)

Ned’s doc test examples look like this:

int_add(3, 4)
int_add(3, 0)
int_add(3.0, 4)
int_add(3.0, ‘four’)

Whose doc test examples are better?

The answer is Ned.  Here are reasons:

Ted’s doc test examples do not quite tell us the information of the function with the improper input values.  With Ted’s doc test examples, we won’t be able to find the bugs in the function when improper input values are given.

Ned’s doc tests examples, however, are good enough to test to see if the function runs properly or not.  It has an example with two proper input values, an example with one value as 0, an example with a proper input and an improper input value, and an example with two improper input values.

With Ned’s doc tests, we will be able to find out how function works with improper input values pretty well, which is the point of the whole doc test process.

When you guys make a doc test, remember to try the best to test out all the possibilities.


Thank you for reading and I will see you guys next week with my impression on my CSC148 blog.

P.S I see the lack of participation in my last function tests, so I will post the answer in Week12 when I sum up this course's SLog.