Hello! How is everyone? This week has been a treat for me unlike the week before.
If you recall, I was stuck on CSC148 assignment 1 last weekend on step 5 because I couldn't figure out what was wrong with my code. Then, on Monday's office hours, Prof. Danny pointed out the SLIGHTEST mistake in my code that completely flew over my head.
I was trying return a tuple and had forgotten to index the value I needed inside the tuple. When my function recursively recalled itself, it was trying to use the tuple instead of the value for simple calculations and thus was returning a TypeError. Bah! Still can't believe I couldn't find this error on my own. After this, I had finished A1 within a few hours. Asides from this, my midterms have been going really well.. I can feel it! So yup, good week!
OK. Pleasantries aside, I want to discuss a couple topics this week so here we go!
Topics:
Trees
Examples of copy.copy and copy.deepcopy
Trees
This is a totally new concept to me but I think it's very interesting with what you can do with trees. Perhaps the most interesting thing for me was learning how to traverse them, and using the data at these nodes with the used traversal method. Currently we learned of three: Post-Order Traversal, In-Order Traversal and Pre-Order Traversal. These three tree traversing methods allows us to manipulate our data in different ways. One example is an expression 1 + 2 * 3 expressed as a Tree.
Think of it like this:
+ is the root.
1 is the leaf child of the root.
* is the node child of the root.
2 and 3 are the leaf children of the * node.
If we use Pre-Order Traversal, we get the expression returned as + 1 * 2 3 which is the pre-fix expression of 1 + 2 * 3.
If we use Post-Order Traversal, we get the expression returned as 1 2 3 * + which is the post-fix expression of 1 + 2 * 3.
Lastly, using the In-Order Traversal, we simply get 1 + 2 * 3 which is an in-fix expression.
Although the expression example is trivial, I learned that we have many different ways of evaluating or storing expressions/data and I think this will become helpful once we have hundreds of different nodes and leaves to keep track of as opposed to just 1, 2 and 3!.
I came across one very helpful video that shows step-by-step binary tree traversals, have a look if you're interested! I especially recommend it if you're having a hard time understanding the concepts.
In-Order Traversals (Binary Tree): http://www.youtube.com/watch?v=gvPOTGGnn8k
Pre-Order Traversals (Binary Tree): http://www.youtube.com/watch?v=S5emQOEMiqc
Post-Order Traversals (Binary Tree): http://www.youtube.com/watch?v=X0pA5JOyI90
copy.copy, copy.deepcopy, assigning variables to values
Here's a tidbit of code that should explain this very well.
If we set x = [1, 2, 3]
and then set y = x
y and x will ALWAYS refer to the same object as long as we don't reassign y or x to new objects. This means, we can APPEND to the list object. So what happens when we do that? Have a look.
>>> x.append(4)
>>> x
[1, 2, 3, 4]
>>> y
[1, 2, 3, 4]
As you can see, there is no mistake, x and y will always be the same until a new value is assigned.
Now then.. rewind a little and we're back to:
x = [1, 2, 3]
Rather than assigning this time, we will import copy and use copy.copy.
>>> y = copy.copy(x)
>>> y
y = [1, 2, 3]
Nothing unusual yet.. but wait!
>>> x.append(4)
>>> x
[1, 2, 3, 4]
>>> y
[1, 2, 3]
Magic. No, just kidding. Now that we use copy.copy, y and x no longer refer to the same object and can be altered without causing an effect on each other. Note: copy.copy is also known as shallow copy.
copy.deepcopy is a little stronger. It actually goes through all the contents of your objects and recreates them individually at different memory addresses. It has a similar effect to copy.copy but goes that extra mile to keep them totally different. I hope this helps!
Check back soon for a another update! I'll be discussing a bit more about OOP (Object Oriented Programming) and for the coming week, recursion.
As always, thanks for reading my SLOG.
Brian
No comments:
Post a Comment