Wednesday, 30 March 2016

Week 11 - A Reflection of First Few Weeks

Time went by really fast since the beginning of this semester. Looking back at what I wrote on my first weeks’ impressions, I couldn’t agree more on what I wrote. With more and more exposure to Python, I definitely feel it is the most programmer friendly language I have encountered. In the first few weeks, I had just got familiar with the special methods like __str__, __eq__, etc. Now, I have come to the point of really appreciating them. It is really useful to be able to define what it means for objects that we create to be equal, greater or less than one another as well as give it a string representation of our choice. I wonder if any other language has this sort of feature. I also learned more Python tools like conditional if, any and list comprehensions which help cut down the amount of code we have to write and improve readability. I have continued to enjoy the simplicity of Python’s syntax. For example, using the words ‘and’, ‘or’, ‘is’, ‘not’ and so on,  which are everyday English words, help one’s thinking process and makes reading code like reading a shorthand version of English sentences.  From learning about docstrings, class design and other features of object oriented programming, we have progressed to many other advanced and important topics (not that class design and OOP concepts are not) like abstract data types, recursion and learning how to write efficient programs. I am eager to know what topics we will be covering in the last weeks of the course.

Friday, 25 March 2016

Week 10 - Efficiency

Getting the code to work the way you want and get the job done is just half way down the road to accomplishing a task. The rest of the way is making it efficient. This is because we don't have an unlimited supply of memory or time. As our hard drive capacity increases, so does our usage, so it becomes vital to create a program that does a task in such a way that it uses only as much resources as is absolutely necessary and does the task quickly. This is precisely what I learned this week, how to make my code more efficient.

This week, in our second assignment as well as in lectures, the need for a code to be efficient was stressed. In my assignment on solving puzzles, initially my program was running forever to solve a 9x9 sudoku puzzle. After attending this week's lectures, I thought through my algorithm again to see where I could make it more efficient. One thing we learned is to avoid duplication. For example, when doing recursion, there is a lot of room for the same value to be computed more than once. This is a waste of time and resources. To avoid this, one can use a data structure to store the values already computed and make the program check to see if the value has already been found before proceeding to compute it (whether recursively or not).  This saved me a lot of time and made my program solve the same 9x9 sudoku puzzle in 15 seconds!

A simple task such as storing information in a data structure can go a long way in saving a lot of time and energy. This really amazes me and makes me eager to learn more such techniques to make my code more efficient.

Friday, 18 March 2016

Week 9 - Binary Trees and Binary Search Trees

This week, we learned Binary Trees (BT)  and Binary Search Trees (BST).

A Binary tree is a tree with only two children. Binary Search tree is a special kind of Binary Tree in that it has an order in which it arranges data which makes it easy to perform search operations. The BST is arranged in a way that there is a root node with a comparable object and all the values that are less than this object are placed in the left subtree and all the values greater than it are placed in the right subtree. This is also true of all the subtrees in the BST. A comparable type is a type in which the objects can be compared. For example, int, float and string. BST is useful for search operations because it is ordered. So if we want to find a value in a tree, we don't have to search the whole tree because a value smaller than the root node will be searched only on the left subtree and a value larger than the root node will only be searched for in the right subtree. This way, I think that the worst case will have to do as many steps as the longest path in the tree, which is certainly better than searching the entire tree.

In the coming weeks, I hope to see applications of trees and binary trees. I think their unique structure sets them apart from structures like stacks and queues and would really like to know how they are used in practical situations. I especially wonder if, in practice, data that is prone to be searched often is placed inside Binary Search Trees instead of lists. That would be a very interesting topic to explore.

Friday, 11 March 2016

Week 8 - A Reflection on Trees and Recursion

This week, we continued with recursion and trees and dealt with more difficult examples. I think I am starting to get the hang of recursion now. It is a really nice tool to use especially when the depth of the data structure one is using can vary. With regards to trees, they are easy to visualize and deal with recursively. The only thing I am working on right now is figuring out how to visit a tree level by level. I have a good idea on how to write code to access the node then its children and then its grandchildren but, I am trying to figure out how I can check all the children before checking the grand-children. Although the solution is posted, I am going to persevere to try to solve it myself before looking at the solution.

Wednesday, 2 March 2016

Week 7 - Abstract Data Types

This week, I thought it would be nice to dedicate a post to the abstract data types I have learned so far. Some of them, like stacks and queues, I have already discussed about and some, like trees, are new. Nevertheless, this post will be a reflection summarizing the structure of all the data types I have encountered in CS148, starting from the most recent.

Trees

This data type has a recursive structure since each of the sub-units of a tree are trees. A tree can be visualized as the upside down version of a tree we see in nature, that is, as a tree that starts with the root, splits into branches and ends with the leaves (fruits and flowers in the tree are not considered). The root or starting point of the ADT, tree, is the root node. The branches, which are like sub-trees, are called children. Finally, the parts of the tree which have no sub-units or no children, just like the leaves of a tree in nature, are called leaves.  Each unit of the tree, like roots, children and leaves, can be called nodes. A node is called an internal node if it has children and is called a leaf otherwise. The root is a leaf if it has no children and it is an internal node if it has at least one child. Each node in a tree is referred to exactly once. Just like in linked lists, the root node can be viewed as the whole tree or as the main node of the tree (or the tree’s starting node). Each tree may or may not have a value and may or may not have children. When the root is viewed as the starting node, it can be thought of like a linked list except where each node can have references to more than one nodes. This concept is best described with a diagram and hence, I have summarized this discussion in the diagram below.





Linked Lists

Linked lists are a list of nodes, where each node refers to exactly one other node or none. Each node has a value and may or may not have a reference to another node. They can be thought of as a nested list where a node has a reference to another node which in turn has a reference to another node, etc. In this approach, each node represents itself and all the other nodes after it. The second and more common approach is to think of them as a node pointing to another node. In this approach, each node only represents itself and what it refers to. In the second point of view, the linked list is implemented with a wrapper class which keeps track of the front and back nodes and the size of the list. The application of a linked list is the same as a list except linked list proves more efficient in most cases.

Stacks and Queues

Stacks can be thought of as a stack of objects. Each object gets added to the top (or end) of the list and the object in the top (or end) is removed first. Queues can be thought of as a line (or queue) of objects. In Queues, objects get added to the end of the list and objects are removed from the front of the list. Both of them can be implemented with the built-in list or a linked list but linked lists are more efficient in the case of Queues because when an object is removed from a list, the rest of the objects need to be moved over by one. This is not required in linked lists because in a linked list, one just needs to change the references of front and maybe back.

These are all the data types we have learned so far. Looking forward to learning more.