Sunday, 23 March 2014
Week 10
This week in CSC148, we learned about big-oh notation and some various sorting algorithms. In addition to this, the second part of assignment II was due. Going to class was very helpful because the professor give us lots of really good hints as to how to approach the problems. For me, the toughest function in the assignment was regex_match(r, s), which returns True if the string s matches the RegexTree rooted at r, and False otherwise. It seemed like the more I tested, the more bugs I kept finding in my program. For example, even when I had '.' working and '*' working, putting them together in the form (r1.r2)* required even more thinking and coding. I think I got a solid program submitted by the deadline, though, so now it's on to studying for the midterm!
Sunday, 16 March 2014
Week 9
This week, we were assigned our third exercise in the course. In the first part, we had to make a function that takes the pre-order and in-order traversals of a binary search tree and reconstructs the tree. I found that this was kind of tricky at first, but after playing around with it a bit, I was able to figure it out. Recursion is my friend. Recursion is my friend. Recursion is my friend. In the second part, we had to make a function that takes a BST and returns the sum of the nodes along the tree's deepest branch. I am working on this right now - haven't finished yet. I'm trying to figure out how to record and compare each of the tree's branches. Hopefully I'll be able to figure it out by the due date at the end of the day!
Friday, 7 March 2014
Week 8
This week in CSC148, we focused mainly on linked structures. In the lab, we had to make methods for a LinkedList class including __len__, __setitem__, __delitem__, and insert. The trouble was that the only two helpful methods already built were prepend and decapitate, so we were required to use recursion. This was a bit different than the recursion I have been exposed to so far. We had to kind of count down through the indices i of the LinkedList until 0 was reached, calling the function on i - 1 each time, in order to reach the recursion depth that we wanted to insert at, or set, or delete. Here is the method __getitem__, which demonstrates what I'm trying to say:
def __getitem__(self: 'LinkedList', ind: int) -> object: """Return the item at position ind. Raise exception if ind is out-of-range >>> lnk = LinkedList(5, LinkedList(7, LinkedList(11, LinkedList()))) >>> lnk[1] 7 """ if self.empty or ind < 0: raise IndexError elif ind > 0: return self.rest.__getitem__(ind - 1) else: return self.item
Subscribe to:
Posts (Atom)