Wednesday, March 18, 2015

Week 10: Unit Test



One of the more rigorous ways to test if your code does its job is using the built in unittest module. In order to test code rigorously, it is important to pick good test cases. Things to consider when choosing test cases is interesting cases, size, mixture, opposites, and order. Many of the things listed are not applicable to every function, and some functions require additional tests not mentioned here. The tests used completely depend on the function you are trying to test.

Interesting Cases
Interesting cases are legal inputs that you think might break your code.
Size
If your function has a collection of objects (list, dict) as one of its parameters, sometimes it is useful to test by varying the number of objects because the function may work for only a range of sizes.
Mixture
Mixing the values in the collections is also sometimes useful, especially when your function tries to isolate certain values.
Opposites
If your functions plays around with opposites (positive or negative, left or right, True or False) testing each one out is essential because they are boundary cases.
Order
Playing around with the order (sorted, unsorted) can also break certain functions that don’t know how to handle objects when given in random order.

For a function that has a string parameter, some general tests could be
“” (Interesting case, size of 0)
“a” (size of 1)
“aaa” (size of 3, same character, order)
“ab c=3” (size of 6, mixture, order)
Notice that multiple things were tested in each case to minimize the amount of tests needed. Can you think of any additional tests that could be useful?

Wednesday, March 11, 2015

Week 9: Binary Tree and Binary Seach Tree



A Binary Tree is a special type of Tree where each node can have a maximum of two children, where each child is assigned a name (usually left and right). I think the only benefit of using a Binary Tree instead of a regular tree is that each node is named.

A Binary Search Tree is a special kind of Binary tree where each node’s value is greater than all values in left subtree and less than all values in right subtree. Binary Search Tree’s that have balanced branching on the left and right of each node allow for a faster time complexity for certain algorithms. When I say balanced branching, I mean that the tree is “saturated”, and is condensed with the smallest height possible. If you are searching for a certain value, the number of steps needed to be taken will be Log2 n rounded to the highest integer where n is the number of nodes. It’s logarithmic with a base of two is because if you are searching for a number, at every node you can ignore either everything on the left or right depending on if your value is bigger or lower than the current node. Halving the number at every step in a balanced tree means that you are saving the computer from doing a lot of computation! This can be worth it, but it takes time to maintain the balance in a tree if nodes are constantly being added and removed. In the worst case scenario in an unbalanced Binary Search Tree, it can take n computations where n is the number of nodes. This is the case if each node has one or less children.

Wednesday, March 4, 2015

Week 8: Linked List



Linked Lists (LL) are fun tool to use in programming because their algorithms are interesting to traverse. It is helpful to view a LL as a rope with knots at the beginning, throughout the rope, and at the ending. Imagine picking up the rope from the beginning (we will call this the front of the Linked List) and follow the rope to the next node. The first node refers to the second node, the second node refers to the third node and this process repeats until you reach the last node (back) which refers to nothing. This is considered a singlely LL as opposed to a doubly linked list where each node refers to the node before and after it if they exist. One advantage to using a LL is that it can be added to dynamically. Some common methods used in LL are inserting or deleting a node, finding the number of nodes, and searching for a node. The time complexity for these methods in the worst case scenario of a singly LL is O(n) where n is the number of nodes. The slow look up time is because in order to get to the last item in the LL, you have to start at the front and follow the references until you reach the last item in the Linked List.