Tuesday, 24 March 2015

Week 9: Binary trees and BST

Binary trees is a Tree that has two children for the node.The tree can be empty too with None. The children are called left and right. It's interesting to see how flexible the Tree class can be. You can use the Tree class to apply to specific designs of the format of the Tree class. Through the tree, you can visit the nodes in different ways.
You can visit the tree inorder:
-visit the left subtree inorder
- visit this node itself
- visit the right subtree inorder
You can visit the tree preorder:
-visit this node itself
- visit the left subtree in preorder
- visit the right subtree in preorder
You can visit the tree postorder:
- visit the left subtree in postorder
- visit the rightsubtree in postorder
- visit this node itself
We can design a class that has more conditions than just a binary tree. There is a type of binary tree called binary search trees (BST). The node.data are comparable. The nodes in left subtree are less than node.data. The nodes in the right subtree are more than node.data. This tree is really efficient if you want to go through a tree without looking at every single node.

Week 8: Linked Lists

We have been learning about linked lists in class. Linked list structures are as objects (nodes) with a value and a reference to other similar objects. You can also picture them as lists made up of an item (value) and a sub-list (rest). There is a class called linked list node that initialize one node and the next node.
class LLNode:
        '''Node to be used in linked list
        nxt: LLNode -- next node
        None iff we're at end of list
        value: object --- data for current node
        '''
        def __init__(self, value, nxt=None):
              ''' (LLNode, object, LLNode) -> NoneType
              Create LLNode (self) with data value and successor nxt.
              '''
              self.value, self.nxt = value, nxt
In linked lists, there is a front and back. There is also size to measure how large the list is. To move through the list, LLNode is used to go on to the next node. In a way. linked lists work like recursion, but it doesn't use recursion in it's functions.
To walk through a linked list:
cur_node = self.front
      while <some condition here...>:
             # do something here...
            cur_node = cur_node.nxt
 Now, you can apply this formula when you need to walk through the linked list using a function.

Friday, 6 March 2015

Week 7: Assignments and Binary trees

For assignment 2, we have to write code for a game called tippy. Tippy uses a similar game view as tic-tac-toe. To win the game, you must form a z shape using x's or o's in any form of rotation. We create a strategy called minimax that chooses the best possible moves to win the game. Minimax is a recursive code which goes lists all the best moves from all the moves available. It assumes the opponent to also use the best possible moves, so minimax must choose the moves that will put the opponent in a losing position.We use recursion so minimax can look at all the game states not matter what game it is and choose the game state that will give the optimal score. In our case, the score will be 1 (for a win), 0 (for a tie), -1 (for a loss).

Lately, in class, we have been learning about trees. What are trees? A tree has set of nodes with directed edges between some pairs of nodes. One node is called a root. In trees, there are children and every child has exactly one parent. A leaf is a node with no children. An internal node is a node with one or more children. Many functions applied to the tree uses recursion to access the children.
















general form of recursion:
if condition to detect a base case:
     do something without recursion
else:  # general case
     do something that involves recursive call(s)
Usually, the base case occurs when there is a leaf and the general case will keep going through the tree until it hits the leaf. Recursion is extremely helpful in trees.

Tuesday, 3 March 2015

Week 6: Object Oriented Programming Summary

For the past few weeks, we have learned a lot of the basic concepts of object oriented programming.
Classes
Classes was a concept also learned in CSC108. A class declaration is creates a new class of creating objects of that type. A class can contain various methods and different attributes. Lets use an example of the class Dog.

class Dog:
     """A class that contains the dog's name, age and species
     """
     def __init__(self, name, age, species ):
         """ (Dog, str, int, str) -> NoneType
         Initialize the dog's name, age in years and the species
         """
         self.name = name    # instance variable unique to each instance
         self.age = age
         self.species = species

We first initialize our class by using the __init__ method.  We initialize the dog's name, age and the species. The parameter self refers to the instance of the class. How does it work?
For example:

>>> dog = Dog('Max', 3, 'Poodle')
>>> dog.age
3

The self refers to the variable dog. We can use the self.name, self.age and self.species in our methods we create.

Methods
What types of properties does the dog class possess?
When it is the dog's birthday, the dog grows a year older. How do we change it's age? We can use a function to indicate that it's the dog's birthday and add a year to it's age.
    def birthday(self):
         """ (Dog) -> NoneType
         Add a year to the dog's age
         """
         self.age += 1

Now if we call birthday on dog, it will add an additional year to the dog.
>>> dog.birthday()
>>> dog.age = 4

A special case of methods are __str__, __eq__, and __repr__. __str__ can help create a string representation for the class. __eq__ can compare if two objects of the same class have the same properties, and __repr__ returns a str that is in Python code form which is easy to read for Python.

Inheritance
Perhaps we want to create a general class not only for dogs, but for all pets. This general class can be used for specific classes on specific animals. All pets have a name, age, and species so we can create a general class called Pet.
class Pet:
     """A class that contains the pet's name, age and species
     """
     def __init__(self, name, age, species ):
         """ (Pet, str, int, str) -> NoneType
         Initialize the pet's name, age in years and the species
         """
         self.name = name    # instance variable unique to each instance
         self.age = age
         self.species = species
The class Dog can inherit all the properties of Pet.

class Dog(Pet):
     """A class that contains the dog's name, age and species
     """
     def __init__(self, name, age, species ):
         """ (Dog, str, int, str) -> NoneType
         Initialize the dog's name, age in years and the species
         """
         Pet.__init__(name, age, species)
Now the class Dog inherits all the attributes of the class Pet. You can do that with various other classes of pets such as cats, fishes, or even create a pet of your own!

Object oriented programming is very flexible and one can create a lot of different programs.

Saturday, 7 February 2015

Week 5: Tracing Recursions





















Recursions is one of the most interesting topics I've learned in computer science so far. The topic is so fascinating and somewhat confusing to follow. The concept of a function calling itself is really amazing.Recursions depth can go infinitely deep, yet the code is a finite statement.
What exactly is recursion?
Well this is an example of recursion function.
def sum_list(L):

    ’’’ (list or int) -> int
    Return L if it’s an int, or sum of the numbers in possibly nested list L
    >>> sum_list(17)
    17
    >>> sum_list([1, 2, 3])
    6
    >>> sum_list([1, [2, 3, [4]], 5])
    15
    ’’’
    # reuse: isinstance, sum, sum_list !
    if isinstance(L, list):
       return sum([sum_list(x) for x in L])
    else: # L is an int
       return L
Basically if the item is an int, it will return the int number. If the item is a list, the function calls itself to check if there is nested lists or if there are more int within the list. Recursion helps the problem of determining how many nested lists are within the list. Recursion keeps calling the function as many times as the depth of the list. This can go on infinitely deep.
How would you trace the lists or int?
Take the example sum_list([1, [2, 3, [4]], 5]). Since L is a list, it will call the function sum_list again, and it will take the sum for every int in that list. Then, it will further check for nested lists within the list until it finds the last nested list in the list. Then, the all the int will be added up to the sum of 15. Keeping track and tracing recursions can be a little tricky and confusing at first. However, with more practice, you will be able to get the hang of it and trace recursive functions of higher complexity.

Week 4: First Impressions

The way CSC148 course runs is very different from CSC108. CSC108 was an inverted classroom teaching style, and it was very convenient for the lecture videos to be online. However, I like that we learn the material in class for CSC148 because I learn the best in classroom where I can listen to the professor directly. Especially since I did not have any previous programming experience except for CSC148, I think it's a lot helpful to learn the material in class and doing lab assignments to help me understand the content better. So far, we have been doing review on classes, but I still got to learn new material such as property and raising exceptions. I was fascinated with the class Turtle because I never knew you could draw in python. It's amazing because I love to draw and I can now incorporate drawing into python. I realized how flexible programming is because programming can be applied to almost everything.
This course is relatively enjoyable so far. I learned a lot about teamwork working on assignments with my group partner. Although working on assignments and keeping up with work is tiring, it was a lot of fun. The assignment was a lot more fun than the one in CSC108 because we actually got to create a simple game. I actually went to office hours a lot this semester compared to last semester, since I never went last semester. Now, I get to learn a lot more about programming from the professor and from my peers. Danny Heap is a great professor with a wealth of knowledge to share, and I also like how he tries to get to know all his students despite the huge classrooms he teaches. Although computer science was not the subject I planned on studying initially, I am really glad I chose this program.

Wednesday, 21 January 2015

Why Geeks Should Write?

English is a part of our everyday language and life. Geeks and especially coders should be able to learn how to write. Learning to write helps geeks communicate with other geeks and people who are not geeks. When given a task or assignment for a project, coders need to understand the project and implement it into a code. The task could be from someone who does not understand code, so the geek needs to understand what the person is saying in English so that they can translate it into code. Computer scientists generally enjoy writing in code rather than writing in English. Although they program well, it is also important to practice writing in English.

From working on assignment 1, I understand why it is so important for geeks, programmers and computer scientists to learn how to write. Reading through the assignment and translating it to code requires one to possess proficient reading and writing skills in order to implement the code. They should know how to translate English words into code. One way to improve your English skills is through writing. Also, working as a team needs proper communication. When working on the assignment, all the members need to understand what the code does. So, putting in comments will be helpful to let all the members know what the code is doing and how the code works. The comments must be straight forward and not ambiguous. Practicing how to write will help the individual communicate effectively to their peers.