Sunday, 3 November 2013

Exercise 6

Exercise 6 was really easy for me, though I'm not sure I solved it the "intended" way.

For Part A, I used a while loop to append the item of each node to a temporary list, and then iterated over the list to see if there were any matches.

Part B was also quite straightforward, I tried to do the same thing except I ran into a problem. Initially, I thought that we were to return the ITEM of the maximum value of all nodes, i.e. in a BST([1,2,44,3]), I had mistakenly assumed the output to be 44. However,we were actually supposed to return a reference to the object that held the value of the item. This was an unfamiliar problem to me. How was I supposed to find the reference to the object after finding the max value?

I ended up brute forcing the problem, and just appended all the objects of the tree into a list and sorted it by the item; then I returned index 0 of that list.

2 comments:

  1. I talked to quite a few people about part b and they all made the same mistake at first. All you needed to do was return the node.

    i.e. 'return node' instead of 'return node.value'

    That's it! But no sweat, I made this mistake too.

    ReplyDelete
    Replies
    1. Yeah, haha. I just instinctively thought they wanted the value rather than the object, as it somehow seemed like the more useful piece of information. :p

      Delete