2014年2月22日土曜日

Week 6
Recursive Structures.
In this week, I learned some terminologies and structures of Trees. 
Tree is constructed by nodes. One node is distinguished as root, which is the biggest parent, and each non_root node has exactly one parent. 
Node with no children is called as leaf, and node with one or more children is called as internal node.

There are three ways to visit root and subtrees.
Pre-order traversal - visit root, then pre-order left subtree, then preorder right subtree.
In-order traversal - visit in-order left subtree, then root, the in-order right subtree.
Post-order traversal - visit post-order left subtree, then post-order right subtree, then root.

After the lecture, I played with the examples of Tree which the professor posted on the website and I understood the structures of the above three methods. Also I found the best method to move cheeses from first stool to the fourth stool with minimum moves and completed the Assignment 1.

Week 5 
Recursive continued
 In this week, professor explained us more recursions and gave us hints of Assignment 1. The concept of A1 is based on Tower of Hanoi, it is an interesting topic of mathematics. He provided us a function that shows moves of n cheeses from stoo1 to stool 3, meaning , and it was really helpful for me because that function included recursion and I utilized the function in A1!.


And he taught us name and value this week. Every name contains a value(object) in python and value is a reference to the address of an object. And there are different types of scopes in the function body. Innermost scope is local, enclosing scopes are nonlocal, and the largest scope is global. During the lecture, we practiced to find the names in different scopes.

In this week, I spent most time in A1. I utilized the technique which the professor taught us during the lecture to complete the Tower of Hanoi of four stools.

Week4 
The main purpose of this week is to learn recursion. The recursion is defined as "defining something in terms of itself" multiple times to achieve our objectives. 

'Nesting depth of list' is an example of the recursion. The nesting depth of list is the number of nested list in the list. For example:
nesting-depth of [1, 2, 3] -> 1
: because a list doesn't include any list. Therefore the depth is 1.
nesting-depth of [1, 3, 5, [3, 4, 3]] -> 2
: because a list include one list. Therefore the depth is 2.

The recursion is difficult for me because this is a new topic for me. The recursion function calls itself in its function body, and all recursion include the base case which tells me when/how to stop the function. Tracing the each step of recursive calls increases complexity therefore it is hard for me. 



 

2014年1月27日月曜日


Week 3 -csc148-

Object oriented programming
Python is an object oriented programming. This means that a powerful style pf programming in which data and the operations that are relevant for that kind of data. In object oriented programming the focus in on the creation of objects which contain  both data and functionality together. Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real world objects interact. 

When programmer represents numeric value by python, he can defines a new class. Class definitions are near the beginning after the import statements. The header begins with the keyword, class, followed by the name of the class, and ending with a colon. Every class must have a special method __init__, called initializer. Object instances have attribute, it means that one of the named data items that makes up and instance. 

Subclass and Superclass
Stack - It is an example used in Week 2.
  • We decided to extend the features of Stack. eg. Create new Stack that only accepts integers. This means that modifying the existing class.
  1. We must break the existing clients to create new methods.
  2. Cut and Paste to modify Stack. This process is hard to maintain. 
  3. New class has attributes of Stack. 
  We can avoid recursion, such as writing same codes twice, by creating a subclass. A subclass inherits all the features of a Superclass. This means that the Superclass is a parent and the Subclass is an offspring. >class Newclass(Oldclass):
  In this case, Newclass is a subclass and Oldclass is a superclass.
  We create subclasses to specialize the features of a new class by adding and modifying behaviors. We don't need to create __init__ method in subclass.
  We can access to Superclass methods by:
>OldClass.method(self,,,) 

New method isinstance(object, classinfo)
          Return true if object argument is an instance of the classinfo argument.
          Example : >isinstance(4, int)
                          True
                        >isinstance('Hello', str)
                          True

Exception 
Whenever a runtime error occurs, it creates and exception object. The program stops running if it occurs and Python describes the exception. Python programmer can create Exception class to raise exception. 

example:

def pop(self: 'IntStack') -> int:
    if self.is_empty():
        raise PopEmptyStackException   
    else:
         Stack.pop(self)

class PopNonIntegerException(Exception):
    pass

If pop(self) causes an error, then PopNonIntegerException appears. 

There are superclass and subclass of Exeptions. For example:

class SpecialException(Exception):
    pass
class ExtremeException(SpecialException):
    pass

In this case, Exception is a superclass and ExtremeException is a subclass of Exception. ExtremeException is a superclass and ExtremeException is a superclass of ExtremeException. And ExtremeException is a subclass of ExtremeException. 

__eq__
When some objects are created by initializer methods, they are different objects and their memories are different. For example:

>>>S1 = Stack()
>>>S2 = Stack()
>>>S1 == S2
     False
>>>S1.push(3)
>>>S2.push(3)
>>>S1 == S2
     False

S1 and S2 are created by class methods and their memories are different. S1 and S2 includes same numerical value. However, their memories are different. 
Then a special method is required. After I make __eq__, memories are still different. 

def __eq__(self, other):
    return is_instance(object, Stack) and self._data = other._data

__repr__
__repr__ is a special methods to represent class as a string. 
def __repr__(self: 'Stack') -> str:
    return 'Stack({ })'.format(self._data)