Saturday, 28 March 2015

Impressions of Week 8

 In week 8, we focused on an entirely new concept: linked lists. These are different than the typical list type in python, which is represented by class List. In order to create a list object in python, one must simply use the class constructor, List(), in order to create an empty list. In csc108, and even in 148, we create lists by using the parentheses (ie. []). We can store the list in a variable, and can even choose to mutate the list if that is the intended purpose. Linked lists, however, are a specific type of list that inherit from the general List class. Each node in a linked list contains a reference to the next node in the linked list. The linked list can also be empty, of course. To link each node, they must contain a reference to each other. For example:

>>> node1 = Node(1)
>>> node 2 = Node(2)
>>> node1.nxt = node2
# node2 will have its nxt as None, and thus it is the end of the list. The nxt attribute was initialized in the __init__ method of the LinkedList class, and is not shown here.

Now that I've explained my understand of a linked list and the LinkedList class, I'd like to talk about the reading on linked lists, as well as methods in the lab that I found confusing, and methods that I had written differently than the posted solutions.

The __str__ method for linked lists in the assigned reading print values in a list without commas in between the list elements. I created my own __str__ method in order to practice recursion, as well as understand why wrappers and helpers are useful. Firstly, I created the wrapper, and realized I would need to create a helper afterwards. Here is the code:

Helper :
def str_rec(self):
    return str(self.data) + ', ' + str_rec(self.nxt)

Wrapper:
def __str__(self):
    return '[' + str_rec(self) + ']'

#it seems as though my __str__ method will still print an extra comma at the end; I have not shown how to get rid of it, but I believe that I could index into the list and remove it.

With regards to the lab, the _set_item(self, index, value) method took me a long time to write, specifically the last few lines of code. The method is supposed to set the current vale at the specified index to the value the user passes in as a parameter. Here is the code I came up with:

if index < 0:
            index = index + self.size
        if index > self.size or index < 0:
            raise Exception('index out of range')
        elif index == 0:
            self.front.value = value
        elif index == self.size - 1:
            self.back.value = value
        else:
            search_node = self.front
            for i in range(0, index):
                search_node = search_node.nxt
            search_node.value = value

To address the reacts component of our grade, I will focus on the "else" block. I had no trouble with the base cases, but I didn't understand how to search through the linked list. I looked online, but nothing was very helpful. Finally, I realized I should think of it in a less computer science specific way. How do you change something at a specific place? You find it, and you change the value. Simple! We've indexed  into lists and changed the value at that index (mutation) so many times before. Linked lists are no different; you just have to be careful not to leave any node disconnected from the rest of the nodes in the list when you're replacing values. Therefore, I created a search node that would assure that it would be the one pointing to the next node in the list, and not the actual current node. Then, I found the index I was looking for, and replaced the cargo at that node with the specified value. Initially, I had trouble understanding why I needed this "search_node"; then, I drew myself a picture with arrows pointing to new locations, and everything made sense.

Finally, I was pretty proud of myself for getting a piece of code right one my first try; not a huge accomplishment for many people probably, but it was one for me! When writing the __add__(self) method, the docstring specified that the method's purpose was to concatenate two linked lists, and  that we were not to mutate the original list. I then thought, "okay, I can created two new LinkedList() objects, and append values from the originals to the new ones." This is what the posted solution was; I wanted to try something different. My code even ended up being shorter in the end! Here is the code:

def __add__(self, other):
    LL1 = copy.deepcopy(self)
    LL2 = copy.deepcopy(other)
    LL1.back.nxt = LL2.front
    LL1.back = LL2.back
    return LL1

By deep copying the originals, anything that is done to the copies does not modify the originals. The, LL1's back's next (which originally pointed to none), now points to LL2's front. LL1's back is now LL2's back, because they are part of the same linked list now. LL1 is returned because LL1's node.front is the head of the list, so every subsequent node in  the list is linked through LL1.front.

Thank you for reading my SLOG! I know it was lengthy; I just wanted to make sure that I covered everything. If anything is incorrect, please let me know!


       

No comments:

Post a Comment