This week I shall be revisiting my week 4 post about recursion and recursion tracing. I still find recursion a very interesting topic and think it's a very important concept to master as recursion is a keystone of efficiency and efficiency is a primary concern among programmers. Although I think I have grasped how to do recursive functions, when it comes to implementation, I have difficulty implementing more advanced recursive functions. For example, the recursive functions that take multiple parameters and return multiple values such as the minimax functions.
I think I have grown since my prior post when it comes to recursive functions. I can imagine how the function will play out in my head and it makes coding easier. I recall having difficulty finishing lab functions and it would make me feel a bit bad about not grasping it in time. However, after reading a couple of slogs regarding recursion, it makes me feel better knowing that I'm not alone in sometimes struggling to grasp things. This post echoes my sentiment, and this post that the previous poster linked does as well. Recursion with trees is another difficult concept to understand that we all seem to share. I do hope that we are all able to fully understand the material as we move forward.
Tuesday, 31 March 2015
Week 10: Testing Code
Ah, testing. I know it's terrible of me but I'm lazy with testing. I know it's a crucial part of creating beautiful and functional code but I'm always tired after creating functional code, especially during a time crunch. Proper testing will see if your code is able to break in the near infinite variations of applications that it's put through so it's very clear why it's important. You definitely want it to work in as many different situations as possible. Testing is doubly important when it comes to actual work submissions!
Unittests in Python are the best way to implement test scenarios. It's built into Python and easily accessible with a bit of code. Unittests were a part of 108 but not a heavy part of 148, which is why I haven't gotten too used to them yet. We learned some new ways to implement unittests this week: the setup and teardown methods. Setup is used to prepare the test case, create any starting variables necessary for the function to work (e.g. setting up gamestates). Teardown is used to destroy the test case after setting it up to start a fresh case.
I shall try my best to get into the habit of testing my code!
Unittests in Python are the best way to implement test scenarios. It's built into Python and easily accessible with a bit of code. Unittests were a part of 108 but not a heavy part of 148, which is why I haven't gotten too used to them yet. We learned some new ways to implement unittests this week: the setup and teardown methods. Setup is used to prepare the test case, create any starting variables necessary for the function to work (e.g. setting up gamestates). Teardown is used to destroy the test case after setting it up to start a fresh case.
I shall try my best to get into the habit of testing my code!
Week 9: More Binary Search Trees
This week we worked on more binary search trees focusing on manipulation of the data within the binary search tree. We had very limited exposure to the material that week due to the test but it certainly is something new. To insert or delete nodes from a binary search tree, which has a rather rigid format, and keeping the structure intact. Whereas the insert method is fairly simple to grasp in terms of coding, the delete method I find is much more difficult.
Here is the solution for this week's exercises. Conceptually, it is easy to grasp. If data satisfies the conditions for left and right nodes, delete those nodes. The difficult part comes when you have to rearrange the binary search tree to maintain the structure. You must check children and make sure that they are in the right spots while maintaining the recursive structure. I find that the more elements you add to a recursive code, the more likely it is to fail or run into errors. I certainly will need to practice more on binary search trees before the final!
def delete(node, data):
''' (BTNode, data) -> BTNode
Delete, if it exists, node with data and return resulting tree.
>>> b = BTNode(8)
>>> b = insert(b, 4)
>>> b = insert(b, 2)
>>> b = insert(b, 6)
>>> b = insert(b, 12)
>>> b = insert(b, 14)
>>> b = insert(b, 10)
>>> b = delete(b, 12)
>>> print(b)
14
10
8
6
4
2
<BLANKLINE>
'''
# Algorithm for delete:
# 1. If this node is None, return that
# 2. If data is less than node.data, delete it from left child and
# return this node
# 3. If data is more than node.data, delete it from right child
# and return this node
# 4. If node with data has fewer than two children,
# and you know one is None, return the other one
# 5. If node with data has two non-None children,
# replace data with that of its largest child in the left subtree,
# and delete that child, and return this node
return_node = node
if node is None:
pass
elif node.data > data:
node.left = delete(node.left, data)
elif node.data < data:
node.right = delete(node.right, data)
elif node.left is None:
return_node = node.right
elif node.right is None:
return_node = node.left
else:
node.data = _find_max(node.left).data
node.left = delete(node.left, node.data)
return return_node
Here is the solution for this week's exercises. Conceptually, it is easy to grasp. If data satisfies the conditions for left and right nodes, delete those nodes. The difficult part comes when you have to rearrange the binary search tree to maintain the structure. You must check children and make sure that they are in the right spots while maintaining the recursive structure. I find that the more elements you add to a recursive code, the more likely it is to fail or run into errors. I certainly will need to practice more on binary search trees before the final!
Week 8: Linked Lists
Linked lists are another data type that is interesting to me. I think of linked lists as a freight train (especially as node data is called cargo!). As you manipulate the linked list, you are still able to keep the order of the list without disrupting other data. If you imagine a freight train, you can add another cargo car to an appropriate section of the train and link them together. The freight train maintains its structure while having additional data. All the nodes ahead of the new data are moved up one space and the head (the locomotive) is moved to the new front position. The nodes behind the new data are moved back one space and the end is moved back similarly. The scenario is similar for the opposite situation if you were to remove a node.
A slog entry that I found helpful was http://148blog1.blogspot.ca/2015/03/linked-lists.html. This slog had succinct bite sized bits of info as to what linked lists are capable of, why they're useful and how to write them. A crucial point to take from it is to create a diagram (which is already very emphasized in the entry). It really helps to stimulate how you think about how a linked list functions and how it reacts when manipulated.
A slog entry that I found helpful was http://148blog1.blogspot.ca/2015/03/linked-lists.html. This slog had succinct bite sized bits of info as to what linked lists are capable of, why they're useful and how to write them. A crucial point to take from it is to create a diagram (which is already very emphasized in the entry). It really helps to stimulate how you think about how a linked list functions and how it reacts when manipulated.
Week 7: Recursion
As I have mentioned in a previous post, I find recursion fascinating, although confusing at the same time. Recursion tracing over functions and methods seems simple, however, creating recursive functions that do exactly what you want is proving a little difficult for me personally. Going through some of the lab functions on recursion, it seems simple, but when you're trying to find something a bit more specific or more advanced it becomes troublesome.
Simple functions like the ones from a prior lab and from lectures are great for general understanding although I would love for some more advanced examples to get a hang of things. The count and contains methods for BTNodes explained in lecture were easy to grasp.
Simple functions like the ones from a prior lab and from lectures are great for general understanding although I would love for some more advanced examples to get a hang of things. The count and contains methods for BTNodes explained in lecture were easy to grasp.
def contains(node, value):
''' (BTNode, object) -> value
Return whether tree rooted at node contains value.
>>> contains(None, 5)
False
>>> contains(BTNode(5, BTNode(7), BTNode(9)), 7)
True
'''
if node is None:
return False
else:
return (node.data == value or
contains(node.left, value) or
contains(node.right, value))
In this method, it's very easy to follow along. If the node is empty/doesn't exist, return False as there is no node that contains value. Otherwise, return whether the current node's data contains value or recursively check every left and right node for the value until the end. I do find help from other sloggers though. One slog that has been a big help has been http://antiaromatic.blogspot.ca/. This blog posted their solutions to labs which really helped me a lot when it came to thinking about how to design functions.
When it comes to more advanced functions that search for specific things and return it in a specific way, it gets a little hectic. For example, for assignment 2, we are suggested to create a helper function that recursively searches through game states and return a minimized value for the opponent while returning a maximized value for self (thus the term minimax). This type of function seems like a far stretch in comparison. To create a list of gamestates, search them, determine the weakest move, return an integer value as well as a move recursively was quite difficult!
However, this is not to say that the challenge wasn't a good thing! Challenging content is the way to create the best thinkers in my opinion.
Subscribe to:
Posts (Atom)