This week, the last before exams officially begin, we discussed hashing. This is a method which converts lists or other data types so that we may access items in that structure with quick and efficient time, doing so by storing the information of the object in a list-like structure.
Some of the major nuances which must be dealt with when hashing are the encounters of possible collisions, that is when two keys of items end up pointing to the same position. One way to resolve this is through probing, which is exploring until a new index is available - i.e., not occupied by another item.
We know that a great advantage of hashing - present in Python dictionaries, has very fast running time, O(1). The faster time comes at the expense of putting in extra work to order the keys of the dictionary.
Currently, I'm not very comfortable with hashing, so I will have to run through the examples once more as well as implementing some of the practice code that we have visited in class.
Friday, 8 April 2016
Wednesday, 30 March 2016
week 11 -- reflecting on recursion
Only one more week to go after this and then it’s officially
exam season. It’s been a good semester for my advance in the field of Computer
Science and the mastery of the Python language. If there’s one important
concept where I can see progress having been made, it has to be on recursion.
At first, recursion simply seemed nonsensical. A function
calling itself and then practically starting over from the beginning of the
code? That just didn’t make any sense. However, after lots of practice in
class, labs and parts of Assignment 2, it really isn’t as hard as it once
seemed. Tracing was especially helpful to get to this point, without being able
to trace a recursive function, one simply cannot know what the recursion does
every time, it is critical to understand what’s happening. Now, I am able to
write a recursive function or method given a docstring, figuring out what
special cases need to be considered (if any), and what the general case is
where recursion is to be called. By understanding recursion, it also allowed me
to understand and implement functions and methods on Trees and Binary Trees,
both important data structures. It really simplifies the alternative of having
to write loops in order to find the depth, height, leafs, or any other properties
of Trees (be it the general Tree or a Binary Tree). Of course, there’s more to
recursion than just the general structure which was often worked on in
lectures; notably in Assignment 2, recursion was extremely helpful for
depth-first search, a tedious task yet it would seem more complicated to write
the same working code through various loops and conditions within it.
All in all, I’ve become much more proficient writing code and
using different techniques on different types of data structures – stacks,
Trees, queues, Linked Lists – but like discussed above one significant tool is
the ability to use recursion – potentially on these same data structures – and
understand its implementation through tracing.
Saturday, 26 March 2016
week 10 -- class A2
This previous week was that of a typical Computer Science student for me. There was a lot of looking at code for hours, figuring out a strategy to write a program which worked efficiently, and most of all, spending considerable time trying to understand why said program was giving errors. In the end, I was able to prevail and finish A2 on time, getting running times that were very reasonable.
For the majority of the assignment, the hardest part was figuring out where to start and how to progress efficiently. Sometimes I would finish a method or function which worked as desired, but redundant and duplicate code was rather noticeable, so a new approach had to be taken. This was especially true for the peg solitaire and mn puzzles, whose extensions had code which were very similar but differed by indexes and the like. For these, helper functions proved to be extremely helpful.
As previously mentioned, errors were very much present in the early stages of code implementation. By far the most frustrating part was when the program would run and give an error that was nearly impossible to track down even after debugging. In the end, after over an hour of searching and much frustration, I was able to pinpoint the error to a misplaced "+1" which was resulting in an index error for certain puzzle configurations. Despite the apparent futility of the initial error-tracking efforts, the feeling that you get once the mistake is found and fixed is indescribable, truly a realization that this why I chose this career path.
The semester winds down and the final exam approaches, thus begins the most stressful time of the year.
For the majority of the assignment, the hardest part was figuring out where to start and how to progress efficiently. Sometimes I would finish a method or function which worked as desired, but redundant and duplicate code was rather noticeable, so a new approach had to be taken. This was especially true for the peg solitaire and mn puzzles, whose extensions had code which were very similar but differed by indexes and the like. For these, helper functions proved to be extremely helpful.
As previously mentioned, errors were very much present in the early stages of code implementation. By far the most frustrating part was when the program would run and give an error that was nearly impossible to track down even after debugging. In the end, after over an hour of searching and much frustration, I was able to pinpoint the error to a misplaced "+1" which was resulting in an index error for certain puzzle configurations. Despite the apparent futility of the initial error-tracking efforts, the feeling that you get once the mistake is found and fixed is indescribable, truly a realization that this why I chose this career path.
The semester winds down and the final exam approaches, thus begins the most stressful time of the year.
Sunday, 20 March 2016
week 9 -- class MidtermTwo
As the title indicates, this week the second midterm of the course was held. On the other two sections, we worked with mutability of trees.
Luckily for all of us, this midterm was much more fair in terms of time. There was plenty of time to read every question, analyze the problem, and then double check solutions after they had been implemented. As expected, LinkedLists, Trees and nested list recursion all showed up. I believe I understood what I was doing for the majority, although thinking back I may have missed checking for a specific case on the first question, which is currently eating away at me. Overall however, there is not much to complain about, it was a perfectly fair test.
As previously mentioned, this week in class we discussed Tree and BinaryTree functions which mutated their respective data types. One example of this was the delete method in a Binary Search Tree, in which different steps had to be taken to assure the desired deletion of a node. We further implemented some more of these functions in the week's lab, and something noteworthy to me was working on a function, recommended by my TA, which would merge two binary trees according to some specific criteria. This was challenging at first but very rewarding once I figured out a method which covered all of the cases. I used the method that we are familiar with currently, which is starting from the root node and moving down to the children, in the future I hope to be able to do the same but working backwards instead.
The next pressing task is finishing all of Assignment 2, which is due the following week. On to the next time.
Luckily for all of us, this midterm was much more fair in terms of time. There was plenty of time to read every question, analyze the problem, and then double check solutions after they had been implemented. As expected, LinkedLists, Trees and nested list recursion all showed up. I believe I understood what I was doing for the majority, although thinking back I may have missed checking for a specific case on the first question, which is currently eating away at me. Overall however, there is not much to complain about, it was a perfectly fair test.
As previously mentioned, this week in class we discussed Tree and BinaryTree functions which mutated their respective data types. One example of this was the delete method in a Binary Search Tree, in which different steps had to be taken to assure the desired deletion of a node. We further implemented some more of these functions in the week's lab, and something noteworthy to me was working on a function, recommended by my TA, which would merge two binary trees according to some specific criteria. This was challenging at first but very rewarding once I figured out a method which covered all of the cases. I used the method that we are familiar with currently, which is starting from the root node and moving down to the children, in the future I hope to be able to do the same but working backwards instead.
The next pressing task is finishing all of Assignment 2, which is due the following week. On to the next time.
Sunday, 13 March 2016
Week 7-8 -- class Tree
Over the previous two weeks, we have been working on a new data structure: trees. These, vaguely similar to real-world trees, have a root and "children". Trees are an important structure to know because many of its methods and functions which use a tree as a parameter require recursion, something we've been getting more experienced with.
First, we started with the basics, defining what a tree was and some of its attributes, such as the value of the root node, and the nodes which make up its children; a node with no children is considered a "leaf" while the rest are considered internal nodes. A general tree has a certain arity or branching factor, determined by the maximum amount of children that one of its nodes has; trees also have depth and height, determined by the paths from the root node to the last child. As the week went on, we started discussing and implementing some methods for the general tree. This included leaf-count, which as explained above counts the number of nodes which have no children. Then, other methods were written to determine height, internal nodes, order of the nodes in a tree and some more properties. While implementing the methods, it was apparent that they used the general recursion structure, a condition which checks for the base case, and then a general case where recursion is to be used.
Another more specific type of tree that we learned and worked with in week 8 was binary trees. These are trees in which the branching factor is 2, and every non-leaf node has exactly two children. Similar to the general tree, we implemented some methods and functions which used recursion. We also worked on different ways to order and search through the nodes in binary trees, which would then make it easier to understand and implement the same for the general tree. One order method checks the left node, then the parent and lastly the right node, while another checks the root node, then its children, its grandchildren, etc. Lastly, we saw a special type of binary tree: a binary search tree (BST), a tree where the value of the right child is always greater than its parent's, and the left child's value is less than the parent's. The BST makes searching through the tree substantially faster than the general type; for example, to check if a value was in the tree, it checks whether the value is contained in each node, and accordingly checks either the left or the right child depending on whether this value is less than or greater to the parent, which is greatly efficient.
Next week is the second midterm in this course. Surely a lot of recursion is to be expected, as well as going back to review data types like linked lists. I wish the best of luck to my fellow students, and hopefully this time we will have less constricting time limitations.
First, we started with the basics, defining what a tree was and some of its attributes, such as the value of the root node, and the nodes which make up its children; a node with no children is considered a "leaf" while the rest are considered internal nodes. A general tree has a certain arity or branching factor, determined by the maximum amount of children that one of its nodes has; trees also have depth and height, determined by the paths from the root node to the last child. As the week went on, we started discussing and implementing some methods for the general tree. This included leaf-count, which as explained above counts the number of nodes which have no children. Then, other methods were written to determine height, internal nodes, order of the nodes in a tree and some more properties. While implementing the methods, it was apparent that they used the general recursion structure, a condition which checks for the base case, and then a general case where recursion is to be used.
Another more specific type of tree that we learned and worked with in week 8 was binary trees. These are trees in which the branching factor is 2, and every non-leaf node has exactly two children. Similar to the general tree, we implemented some methods and functions which used recursion. We also worked on different ways to order and search through the nodes in binary trees, which would then make it easier to understand and implement the same for the general tree. One order method checks the left node, then the parent and lastly the right node, while another checks the root node, then its children, its grandchildren, etc. Lastly, we saw a special type of binary tree: a binary search tree (BST), a tree where the value of the right child is always greater than its parent's, and the left child's value is less than the parent's. The BST makes searching through the tree substantially faster than the general type; for example, to check if a value was in the tree, it checks whether the value is contained in each node, and accordingly checks either the left or the right child depending on whether this value is less than or greater to the parent, which is greatly efficient.
Next week is the second midterm in this course. Surely a lot of recursion is to be expected, as well as going back to review data types like linked lists. I wish the best of luck to my fellow students, and hopefully this time we will have less constricting time limitations.
Thursday, 3 March 2016
Week 7 -- def recursion(value):
In this week's post, I'll be giving some insight into recursion, including how it works, initial thoughts and difficulties, and how to build a recursive function from the knowledge that we currently have.
First of all, the principal characteristic of recursion and a recursive function is that the function will at some point call itself in the body. This can and has been tricky to understand at first, especially the early stages of tracing. Now, generally speaking from what we've seen so far, there is usually one "base" case in which recursion isn't necessary, this can be useful and helpful when tracing through an example call. To give an example from the lectures, imagine a function which aims to determine the depth of a list and takes an object as a parameter - this may or may not be a list; the base case would be if the given argument is not a list, in which case a depth of 0 would be returned; however, if the argument is in fact a list, one could use recursion to go through this list (assuming it is finitely nested), calling the function on every object in the list, until recursion is no longer necessary and the required output would be given.
One of the principal concepts that we were made to understand while tracing through a recursive function, is to remember that it is not necessary to expand for arguments with examples with characteristics that you have seen before. To go back to the example of the previous paragraph, while tracing through a list of depth 2, it would not be necessary to expand any time you encounter an inner list of depth 1, given that you would already know how to do such a thing - otherwise the tracing process becomes extremely tedious and can result in practically unreadable text.
Now on to a brief explanation of the way we've structured our recursive functions to this point: it is perhaps important to note that we have mostly been working with list objects. Like before, there is generally a base case in which recursion isn't necessary; in our list examples, this has usually occurred when the given argument is not a list, and something is returned - this is done by checking with an if condition. Then, there is another condition, indicating that we in fact have a list that we must go through. Generally speaking, we make use of Python built-in functions such as sum, max, any, etc., then these functions are used on a list that we create through list comprehension, which uses recursion as it checks through every item in the list. Obviously, this is the general case up until now and not all functions will follow this structure, but it gives us a good idea about how such functions work, and their use compared to similar functions which may use a for loop or other methods to do the same thing.
As we learn more about recursion, I hope to get more comfortable in creating functions which require it, and also understanding how to efficiently trace through a recursive function call.
First of all, the principal characteristic of recursion and a recursive function is that the function will at some point call itself in the body. This can and has been tricky to understand at first, especially the early stages of tracing. Now, generally speaking from what we've seen so far, there is usually one "base" case in which recursion isn't necessary, this can be useful and helpful when tracing through an example call. To give an example from the lectures, imagine a function which aims to determine the depth of a list and takes an object as a parameter - this may or may not be a list; the base case would be if the given argument is not a list, in which case a depth of 0 would be returned; however, if the argument is in fact a list, one could use recursion to go through this list (assuming it is finitely nested), calling the function on every object in the list, until recursion is no longer necessary and the required output would be given.
One of the principal concepts that we were made to understand while tracing through a recursive function, is to remember that it is not necessary to expand for arguments with examples with characteristics that you have seen before. To go back to the example of the previous paragraph, while tracing through a list of depth 2, it would not be necessary to expand any time you encounter an inner list of depth 1, given that you would already know how to do such a thing - otherwise the tracing process becomes extremely tedious and can result in practically unreadable text.
Now on to a brief explanation of the way we've structured our recursive functions to this point: it is perhaps important to note that we have mostly been working with list objects. Like before, there is generally a base case in which recursion isn't necessary; in our list examples, this has usually occurred when the given argument is not a list, and something is returned - this is done by checking with an if condition. Then, there is another condition, indicating that we in fact have a list that we must go through. Generally speaking, we make use of Python built-in functions such as sum, max, any, etc., then these functions are used on a list that we create through list comprehension, which uses recursion as it checks through every item in the list. Obviously, this is the general case up until now and not all functions will follow this structure, but it gives us a good idea about how such functions work, and their use compared to similar functions which may use a for loop or other methods to do the same thing.
As we learn more about recursion, I hope to get more comfortable in creating functions which require it, and also understanding how to efficiently trace through a recursive function call.
Monday, 29 February 2016
Week 6 -- def recursion_intro(x):
This previous week was a very eventful and gratifying one, that's mainly because Assignment 1 was due and the finishing touches had to be made and mistakes had to be fixed. The course also introduced recursion, an important tool in coding which we will surely see again in the future.
Assignment 1 was long, it took some time to read through the given API's, client code to figure out what our classes, methods and functions had to do once we implemented them. After implementing easier classes such as Rider, Driver, Container and Dispatcher came some more difficult ones which used the former classes. Event, Monitor and Simulation were easily the most difficult classes to implement because together they formed the end goal of the assignment: to run a simulation and report the statistics. Perhaps the most frustrating aspect of the entire aasignment was figuring out what certain events were supposed to do, and then properly adding them in the correct order so that statistics would be displayed as expected. Many small changes were made throughout this part. Luckily I was able to get some much needed clarification from the instructor, helping me not only to complete the assignment on time but also to the point where I myself was able to help out fellow students with similar issues as mine.
In this past week, we were introduced to recursion. We mainly worked with some simpler functions which followed the basic recursion design, mostly working with list objects and started getting comfortable with tracing the function given an argument. Surely it will get more complicated later on and I will report as such when that time comes.
Assignment 1 was long, it took some time to read through the given API's, client code to figure out what our classes, methods and functions had to do once we implemented them. After implementing easier classes such as Rider, Driver, Container and Dispatcher came some more difficult ones which used the former classes. Event, Monitor and Simulation were easily the most difficult classes to implement because together they formed the end goal of the assignment: to run a simulation and report the statistics. Perhaps the most frustrating aspect of the entire aasignment was figuring out what certain events were supposed to do, and then properly adding them in the correct order so that statistics would be displayed as expected. Many small changes were made throughout this part. Luckily I was able to get some much needed clarification from the instructor, helping me not only to complete the assignment on time but also to the point where I myself was able to help out fellow students with similar issues as mine.
In this past week, we were introduced to recursion. We mainly worked with some simpler functions which followed the basic recursion design, mostly working with list objects and started getting comfortable with tracing the function given an argument. Surely it will get more complicated later on and I will report as such when that time comes.
Sunday, 14 February 2016
week 5 -- class MidtermOne
Continuing on from the previous week, we discussed linked lists to a greater extent, implementing many special and other methods. On Wednesday the course held its first midterm, which I will discuss later in this post.
First, to expand on the linked lists topic, we saw API's and later implemented many different methods. Some of these included the __getitem__ and __setitem__ special methods, both of which revolved around indexes of the linked list. The former method would search through the list with a given index, if it was valid then it would return the value of the node at that index; on the other hand the latter method did a similar thing in that it searched through the list up until the given (valid) index, but instead of returning the value at that index it would replace it with a given value. Some other methods involved deleting nodes from the linked lists, many of these methods required drawing pictures so that it would be easier to visualize what was happening when a node got deleted. Many of these new methods were implemented in the week's lab section.
Moving on to the midterm, I have to say that I was quite upset after it was over. This isn't because it was very hard - on the difficulty scale I would say that it was rather fair. My biggest problem with the test was the time limitation. The test simply had too many classes and API's to implement in 50 minutes. There was no time to even think about the questions and what they were expecting of us to achieve; I made the mistake of really trying to understand what the first question was asking and by the time that I had finished it - feeling confident that what I had done was correct - half of the time allocated had already passed. This led to me and I'm assuming many other students rushing through the test and likely messing up on parts of the test which we would normally implement correctly with regular time limitations. I won't discuss any further on this test, my final thoughts stand that it was fair in difficulty but quite the opposite in terms of time limitations.
Next comes reading week, where I hope to finish most, if not all, of assignment 1, that way I'll be able to have plenty of time during the week that follows to ask any nagging questions that I had with it.
First, to expand on the linked lists topic, we saw API's and later implemented many different methods. Some of these included the __getitem__ and __setitem__ special methods, both of which revolved around indexes of the linked list. The former method would search through the list with a given index, if it was valid then it would return the value of the node at that index; on the other hand the latter method did a similar thing in that it searched through the list up until the given (valid) index, but instead of returning the value at that index it would replace it with a given value. Some other methods involved deleting nodes from the linked lists, many of these methods required drawing pictures so that it would be easier to visualize what was happening when a node got deleted. Many of these new methods were implemented in the week's lab section.
Moving on to the midterm, I have to say that I was quite upset after it was over. This isn't because it was very hard - on the difficulty scale I would say that it was rather fair. My biggest problem with the test was the time limitation. The test simply had too many classes and API's to implement in 50 minutes. There was no time to even think about the questions and what they were expecting of us to achieve; I made the mistake of really trying to understand what the first question was asking and by the time that I had finished it - feeling confident that what I had done was correct - half of the time allocated had already passed. This led to me and I'm assuming many other students rushing through the test and likely messing up on parts of the test which we would normally implement correctly with regular time limitations. I won't discuss any further on this test, my final thoughts stand that it was fair in difficulty but quite the opposite in terms of time limitations.
Next comes reading week, where I hope to finish most, if not all, of assignment 1, that way I'll be able to have plenty of time during the week that follows to ask any nagging questions that I had with it.
Saturday, 6 February 2016
Week 4 -- class LinkedList
In this most recent week of CSC148, we worked on creating API's for and implementing different classes that had similar functions such as other Python types like lists. Some of the classes covered include Stack, Sack and LinkedList.
We continued our discussion from the week before on class Stack, which would simulate a real world stack where objects were placed one on top of the other, methods to remove items from the stack, and an important feature of them in that they had no particular order, rather just the item at the top being placed followed by the next. This class was later referred to in the week's lab, where students were to implement a function that utilized the Stack class, as well as implementing a new class Queue which followed a similar design recipe as the aforementioned class.
Another concept that we spent the majority of the week discussing was linked lists. These were similar to Python lists, except that every item that would be present in a regular list appeared in this new class as nodes, each holding a value. This is very helpful in that it saves a lot of memory when many items are inside the linked list - something that was touched upon a few times during lectures is the idea of imagining that you're working with 3 billion objects, and how in these situations efficiency could be critical towards saving memory and allowing the computer to run smoothly. We discussed and later implemented methods of class LinkedListNode and LinkedList, the latter of which was a collection of nodes created from the first class; the methods themselves were very similar to those of Python list types. We were told that drawing images was easier to visualize linked lists and that it would become a good practice to do so, as we had the advantage over computer in that our brains could lay out information in front of us and look at multiple items at the same time - a simplistic explanation but hopefully it gets the point across.
Next week is the midterm, of course some stress comes with writing any test, but I feel that by going over labs, implementing some classes on my own and reading over lecture slides should get me confident and prepared for Wednesday. Stay tuned next week where I'll discuss how it went.
We continued our discussion from the week before on class Stack, which would simulate a real world stack where objects were placed one on top of the other, methods to remove items from the stack, and an important feature of them in that they had no particular order, rather just the item at the top being placed followed by the next. This class was later referred to in the week's lab, where students were to implement a function that utilized the Stack class, as well as implementing a new class Queue which followed a similar design recipe as the aforementioned class.
Another concept that we spent the majority of the week discussing was linked lists. These were similar to Python lists, except that every item that would be present in a regular list appeared in this new class as nodes, each holding a value. This is very helpful in that it saves a lot of memory when many items are inside the linked list - something that was touched upon a few times during lectures is the idea of imagining that you're working with 3 billion objects, and how in these situations efficiency could be critical towards saving memory and allowing the computer to run smoothly. We discussed and later implemented methods of class LinkedListNode and LinkedList, the latter of which was a collection of nodes created from the first class; the methods themselves were very similar to those of Python list types. We were told that drawing images was easier to visualize linked lists and that it would become a good practice to do so, as we had the advantage over computer in that our brains could lay out information in front of us and look at multiple items at the same time - a simplistic explanation but hopefully it gets the point across.
Next week is the midterm, of course some stress comes with writing any test, but I feel that by going over labs, implementing some classes on my own and reading over lecture slides should get me confident and prepared for Wednesday. Stay tuned next week where I'll discuss how it went.
Wednesday, 27 January 2016
Week 1-3 -- class Slog:
Another semester kicks off, and with it comes a new level in the pursuit for a computer science degree - CSC148. Thus far, in comparison with 108 not much is new, except that up to now we've focused much more on the implementation of classes; these were only briefly discussed in the previous course - though they did appear on the final exam, so it was evident that they had great significance in programming. Of course, classes were not the only topic covered in lectures - which I should mention, have been very pleasant, there is a lot of interaction between the instructor and students, and the class (I feel obliged to point out that there's no pun intended) in large part thanks to the instructor is highly engaging.
Along with the aforementioned class implementation, we've also discussed some Python conventions which differ slightly to 108. One of these include list comprehension, a useful tool in creating new lists. This is not something that stuck with me originally, but after enough practice I'm sure I'll be proficient enough to use them to make more compact and efficient code. During these first few weeks, the professor has emphasized on following proper procedure when designing API's, and efficient coding, which includes avoiding duplicate code and utilizing the established conventions, is of great importance.
Much more is to come, and these weekly posts will attempt to summarize my experience in the course, discuss difficult concepts and provide a nice read to look back on as I chuckle at my past struggles with ideas that by that pointwill should be ingrained in my mind.
Along with the aforementioned class implementation, we've also discussed some Python conventions which differ slightly to 108. One of these include list comprehension, a useful tool in creating new lists. This is not something that stuck with me originally, but after enough practice I'm sure I'll be proficient enough to use them to make more compact and efficient code. During these first few weeks, the professor has emphasized on following proper procedure when designing API's, and efficient coding, which includes avoiding duplicate code and utilizing the established conventions, is of great importance.
Much more is to come, and these weekly posts will attempt to summarize my experience in the course, discuss difficult concepts and provide a nice read to look back on as I chuckle at my past struggles with ideas that by that point
Subscribe to:
Posts (Atom)