Monday 30 March 2015

CSC148 Lecture Week 11

Looking Back
   As I was studying for midterm number 2 I realized that the code I came up with for lab 6 and documented in my SLOG was actually quite redundant.

In lab 6 my solution to the list_longest_path function looked like this:
if node is None:
        return []
 elif is_leaf(node):
        return [node.data]
else:
        if not node.left == None:
            left = list_longest_path(node.left)
        else:
            left = []
       
        if not node.right == None:
            right = list_longest_path(node.right)
        else:
            right = []
       
        return [node.data] + left if len(left) > len(right) else [node.data] + right

I didn't really need all those nested if statements and the second if statement also was not necessary. I managed to come up with some simpler, easier to read code:

if node == None:
        return []
else:
        right = [node.data] + list_longest_path(node.right)
        left = [node.data] + list_longest_path(node.left)
        return right if len(right) > len(left) else left

I think that this SLOG entry and my SLOG overall really shows how I have learned and grown throughout the course. As another slogger has said, practice makes perfect. I have spend a lot of time practicing new recursion techniques and I think that I have a much better grasp of it now. I'm also really missing the labs that were cancelled due to the TA strike. They were a great place to practice and as another slogger points out, they were all about practicing our coding skills, and we never wasted time on pointless name games or team building activities.

No comments:

Post a Comment