Monday, March 3, 2014

Recursion

Recursion is essentially recalling the same method within the method.its a smart way to break down a problem into many small steps however with very little code.This is achieved by first identifying a base case (or multiple base cases) and solving smaller versions of the problem until this case is reached. 
here is the example of how to use recursion:
def nested_depth(L):
    """
    Return nested depth of list L.

    Nested depth is:
    0                                   --- if L is a non-list
    1 + maximum nested of elements of L --- if L is a list

    >>> nested_depth(3)
    0
    >>> nested_depth([1, 2, 3])
    1
    >>> nested_depth([1, [2, 3], 4])
    2
    """
    return (1 +max([nested_depth(x) for x in L] + [0]) if isinstance(L, list)  
       else 0)

Also ,i got it right in the midterm test.It is exactly same as above code.

Sunday, February 2, 2014

test code is important

As the lab last week , we have written some codes for testing.
First of all,we should consider multiple cases for the code and write code for each cases.
Using AssertEqual to determine whether it show the same result as we thought.

Here is an example for the testing.
import unittest
import duplicates

class TestRemoveShared(unittest.TestCase):
    """Tests for function duplicates.remove_shared."""

    def test_general_case(self):
        """
        Test remove_shared where there are items that
        appear in both lists, and items that appear in
        only one or the other list.
        """
        
        list_1 = [1, 2, 3, 4, 5, 6]
        list_2 = [2, 4, 5, 7]
        list_1_expected = [1, 3, 6]
        list_2_expected = [2, 4, 5, 7]

        duplicates.remove_shared(list_1, list_2)

        self.assertEqual(list_1, list_1_expected)
        self.assertEqual(list_2, list_2_expected)
 
if __name__ == '__main__':
    unittest.main(exit=False)

Tuesday, January 21, 2014

object_oriented programming

First time, I heard the word 'oop', it really made my day.( like 'oops')
I am going to take computer science specialist, I know the importance of OOP for me,beside the CSC108 course has taught the first OOP language ---Python .

Nowadays, most of the program is written by using a procedural programming paradigm which is focus on writing function or procedures which operate on data. But in OOP ,the focus is on the   the creation of objects which contain both data and functionality together which mainly called Class in Python.

On the lecture ,we have learned two application of OOP,including two class(Stack and Queue). First one the follows the rule 'input on the last,output on the last'
and Queue follows the rule'input on the last, output on the first'.Comparing with these two class running time, we can  show that the efficiency of the two program.

At the last,OOP focus on object,we should make desire code to apply the question from the  real world.