Saturday, 31 January 2015

Week4 Slog

Week 1
In the first week we reviewed some basic knowledge from CSC108. Then we took the class Point and class stack as example to learn more details about class in python.
Notes:

1. We can use loop in a list, e.g. If we what to create a list which contain all square number from integer 1 to 9, we can write:
>>> s = [x**x for x in range(1, 10)]
By this method we can calculate a set of objects but not implement them one by one.We can also use function in this method:
>> def square(x):
    return x**2
>>> s = [square(x) for x in range(1, 10)]

2. The minimum need to create a class in python is the name of the class, e.g.
>>> class Point():
    Pass

3. There are some special methods for class:

__init__ : initialize the class e.g.
 
def __init__(self, coord):
        """ (Point, list-of-floats) -> NoneType

        Create and initialize this point.

        >>> p = Point([3, 4])
        """
        self.coord = [float(x) for x in coord]

__repr__ : Use a str to represent a class instance e.g.

def __repr__(self):
    """ (Point) -> str

    Return a string representation of self that evaluates
    into an equivalent Point.
        
    >>> p = Point([3, 4])
    >>> p
    Point([3.0, 4.0])
    """
    return "Point({})".format(self.coord)

__str__: sometimes we may need more specific words to describe the class, so we may use __str__ method. If we dont state it, python will use __repr__ to represent the class instance.

def __str__(self):
""" (Point) -> str
Return a string version of self.
>>> p = Point([3, 4]) >>> print(p) (3.0, 4.0)
    """
return str(tuple(self.coord))
_eq_ : Being used for check whether two objects are same. e.g.
def __eq__(self, other):
    """ (Point, object) -> bool

    Return whether self is equivalent to other.

    >>> p1 = Point([3, 4, 5])
    >>> p2 = Point([3.0, 4.0, 5.0])
    >>> p1 == p2
    True
    """
    return (isinstance(other, Point) and self.coord == other.coord)


Week2
The topic in week 2 is stacks. We can create a class named Stacks to hold the elements. There are four basic method in Staks:


class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def is_empty(self):
        return (self.items == [])

__init__: initialize an empty stack(an empty list).
pop: remove the top item in the stack.
push(n): add the element n to the top of the stack.
is_empty: check whether the stack is empty.

We can use some method in the stack, for example:

def adding(n):
    '''(str) -> int
       Pre-condition: n contains a math symbol in  '+-*/'.
       Return the sum all numbers in n.
       >>>n = '123456789'
       >>>adding(n)
       579
    '''
    k = Stack()
    for x in n:
        k.push(x)
    for i in k.items:
   

Week3
In the third week we studied how to build a sub class base on a main class. A sub class will inherit all method from the main class also we can add new method to sub class. If same method exist in both sub class and main class (e.g.__repr__, __str__, __init__), python will implement the closer method.

To use the methods from main class:

class A:

def square(x):
    return x ** 2

class B(A):

def double_square:
    return (A.square(x)) * 2

To import a specific class from another file:

from file import A

class B(A):
    Pass

If a method in main class cannot be state but can be stated in sub class, we can raise a Error:

class A:

def double_square(x):
    raise ValueError














Saturday, 24 January 2015

2015/01/24 Week 3 (Why Geeks Should Know How to Write)

Why Geeks Should Know How to Write

Although programmers do not require high writing skills like an author, we still can be benefited by some writing work. At first, programmers should always have a plan before coding. A specific plan or sketch can make our object more clear so we will know what are we want to do then we can reduce the mistakes during the coding and know how to test our codes. Coding can be very complex when we have to combine a lot of modules to finish the program. If we do not have a plan and start coding directly we may produce something that cannot meet our targets. Therefore, write a plan and draw a clear structure for the codes before working can save a lot of time by making sure that we are following the spec file.
Furthermore, the programmers may want to write their ideas on the paper. Because the ideas maybe lost or blurring during the thinking. If I write ideas on the paper and state them in details then I will never forget. Sometimes the ideas maybe bad or cannot work, so if we write down them on the paper we can analysis each of them, fix the problems or choose the best one. When a target can be achieved by different ways, we should display all the ways on the paper then find out advantages and disadvantages of each one to make the final decision.
During testing the codes, programmers can build a list of test case on the paper so we will not ignore any important cases, and delete the useless test cases to save the time. After the testing, we have to record each wrong cases down so we can check them one by one and redesign the codes, then repeat these steps until all tests are passed.
The geeks will never stop learning new knowledge, also include find out ways to solve hard problems. Sort out what we learned every week and every month can help us to maintain the new skills and know how to apply them. We should record the test cases which are not passed and write down the ways to solve; therefore, we can prevent same mistakes next time, or generate new methods from these cases to solve new problems. These are reasons for why writing skills are still important to geeks and help us a lot.