Saturday 26 October 2013

Week 7

This week we covered something I've been looking forward to for a while - linked lists. It seems a lot simpler after learning about it but there were lots of things to take away from the course this week.

There are two ways of thinking about the linked list, both of which can be summarized in one sentence: either as a sequence of nodes, each of which contains both a value and and an embedded reference/link to the following node in the sequence, or as a tree with an arity of 1. It seems simple enough, but the wording made it a bit confusing to me. In class, the nodes were represented as classes with a value/item attribute, and a next attribute (the link), which was itself a node. In that case, it seems like the nodes in a linked list do not contain references to other nodes but rather just contain the nodes themselves as an attribute. This seems to be contradictory to the picture used to represent linked lists:


which looks like a sequence of nodes. Rather, the code makes the link list seem like a bunch of nested nodes, with the depth as an indicator of the order. Thinking about linked lists this way, or as a tree, makes more sense to me.

Another difficulty I encountered this week was understanding the difference between __repr__ and __str__. In essence, although they are both string representations of an object, __repr__ is called when either the object itself  is called or repr() is called on the object, and __str__ is called when print() is called on the object. The main difference is that __repr__ is a more formal representation of the object, and should return enough information to be able to fully reconstruct the object. In other words object == eval(repr(object)) should return true (although in practice this doesn't always happen because sometimes it leads to an infinite loop). On the other hand, __str__ is more of an informal representation and emphasizes readability rather than unambiguity.

Besides these two concepts, we covered some other methods like __contains__, __len__, and __getitem__ (which we were also able to try and implement ourselves in the lab) which allowed us to get some more practice writing recursive methods for trees.

Also, the links to all of the SLOGs were posted this week so I'll be reading over some of them and talking about what some of the other students are thinking in the remaining weeks. I'll try to comment on things I find interesting and hopefully others will comment on this blog if they feel like it. Overall, I really enjoyed the class this week and was able to learn some new concepts while applying them and practicing older ones.

No comments:

Post a Comment