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.
@param Slog self: describes CSC148 experience
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.
Subscribe to:
Posts (Atom)