Finally we submitted assignment 2 on the 5th of March. Implementing minimax was a tough job. The coming week we also have term test 2. This week we talked about linked lists, which are linear sequences of nodes. We defined two classes in lecture; LLNode, to represent a single node and a wrapper class, LinkedList, to represent the list a whole. We also talked about traversing a linked list and mutating a linked structure. In the LinkedList class we implemented functions to prepend and a contains function as we did for the binary trees. I found the code for the contains function for linked lists much easier than the one for the binary trees. There were fewer lines of code as well. Here is an example of a linked list and the code for the contains function:
def __contains__(self, value):
''' (LinkedList, object) -> bool
Return whether LinkedList (self) contains value.
>>> lnk = LinkedList()
>>> lnk.prepend(0)
>>> lnk.prepend(1)
>>> lnk.prepend(2)
>>> lnk.__contains__(1)
True
>>> lnk.__contains__(3)
False
'''
current_node = self.front
while current_node:
if value == current_node.value:
return True
current_node = current_node.nxt
return False
Since assignment 2 was due, most of our lecture time was used for discussing the assignment, which was pretty useful, and so we didn't cover much material.
No comments:
Post a Comment