Monday, 9 March 2015

Week 9: The Impressions of Week 8

This week, we learned a brand new thing -- LinkedList and LLNode. 

Personal speaking, I feel LLNode is easy to understand, since it is kind of a tree with single child. However, for LinkedList, I was little confuse because of the structures and usabilities. First, why does  not the lab have lot of functions about lnk.front but not so much about lnk.back? Secondly, I do not know where we can use LinkedList in coding programs. 

Except as these two questions, my solutions of functions are totally different compared with the sample solutions, although it can run very well. I guess there are multiple ways to write a same function in computer science. The main concern is all about running time. For example, one of my my function needs 10 seconds to run a result, but the corresponding sample solution just needs 1 second. I should learn how to write function like sample solutions and understand the better ideas of coding. 

Let me take self.__setitem__ for example to show the different of sample solutions and mine: 
sample solution:
    def __setitem__(self, index, value):
        ''' (LinkedList, int|slice, object) -> NoneType

        Set the value at index to value, if index is in range, otherwise
        raise an IndexError.  Indexs are counted from 0. Note that negative
        integers can be adjusted by adding self.size, to get a index in
        range.

        >>> lnk = LinkedList()
        >>> lnk.prepend(5)
        >>> lnk.prepend(7)
        >>> lnk.__setitem__(1, 9)
        >>> print(lnk.front)
        7 -> 9 ->|
        >>> lnk[0] = 3
        >>> print(lnk.front)
        3 -> 9 ->|
        >>> lnk[-1] = 8
        >>> print(lnk.front)
        3 -> 8 ->|
        '''
        if index >= self.size or self.size == 0:
            raise IndexError('Out of range')
        elif index < 0:
            return self.__setitem__(self.size + index, value)
        else:
            cur_node = self.front
            assert 0 <= index < self.size, ('Index {} too big for node {}'.
                                            format(cur_node))
        for step in range(0, index):
            cur_node = cur_node.nxt
        cur_node.value = value
Mine:
    def __setitem__(self, index, value):
        ''' (LinkedList, int|slice, object) -> NoneType

        Set the value at index to value, if index is in range, otherwise
        raise an IndexError.  Indexs are counted from 0. Note that negative
        integers can be adjusted by adding self.size, to get a index in
        range.

        >>> lnk = LinkedList()
        >>> lnk.prepend(5)
        >>> lnk.prepend(7)
        >>> lnk.__setitem__(1, 9)
        >>> print(lnk.front)
        7 -> 9 ->|
        >>> lnk[0] = 3
        >>> print(lnk.front)
        3 -> 9 ->|
        >>> lnk[-1] = 8
        >>> print(lnk.front)
        3 -> 8 ->|
        '''
        length = self.size
        l = []
        for i in range(0, length):
            l.append(self.__getitem__(i))
        for i in range(0,length):
            self.delete_front()
        
        if index < 0 :
            index = index + length
         
        if index > length - 1:
            raise Exception('out of range')
        else:
            for i in range(0, length):
                if i != (length - index - 1):
                    self.prepend(l[length - i - 1])
                else:
                    self.prepend(value)
I used a complex way to solve this, I m still not very understand the sample solutions' idea to write it, I hope TA can come back!

4 comments:

  1. Finally, I know how the sample solution works. The reason why I could not code it before is that I did not really understand the relation between cur_node and lnk.front when I assign: cur_node = lnk.front in the body of my definition.

    ReplyDelete
  2. I also approach this question from different way

    My algorithm for __setitem__(self, index, value):
    1. If the lnk has no item or index we want to insert is too large, then we return error
    2.If the given index is negative, then we should looking for the proper position for value from right side. In the sample solution, they first transform the negative index into its corresponding positive index and then applying __setitem__ recursively. So that we only dealing with positive index.


    So far, very similar to the sample solution.

    The difference is in the last part.
    else:
    cur_node = self.front
    for step in range(0, index):
    cur_node = cur_node.nxt
    cur_node.value = value

    ReplyDelete
    Replies
    1. you are so smart that address the question in quick and correct way!

      Delete
  3. Your solution is not necessarily more complex than the sample solution. It may help if you draw pictures to illustrate what your algorithm does at each step.

    ReplyDelete