コード例 #1
0
class Scoreboard(object):
    """ Fixed-length sequence of high scores in nondecreasing order."""
    def __init__(self, capacity=10):
        """ Initialize scoreboard with given maximum capacity."""
        self._board = SinglyLinkedList()
        self._cap = capacity

    def __len__(self):
        return len(self._board)

    def __getitem__(self, k):
        """ Return entry index k."""
        if k == 0:
            return self._board.head()
        if k == len(self) - 1:
            return self._board.tail()
        if k > len(self) - 1 or k < 0:
            raise IndexError('Index is out of range')
        else:
            walk = self._board._head
            for _ in range(k):
                walk = walk._next
            return walk._element

    def add(self, entry):
        """ Consider adding entry to high scores."""
        score = entry.get_score()
        # insert first element
        if len(self) == 0:
            self._board.add_first(entry)
            return
        good = score > self._board.head().get_score() or len(self) < self._cap
        if good:
            # insert an element to self._board
            prev = walk = self._board._head
            while walk is not None:
                if walk._element.get_score() > score:
                    break
                else:
                    prev = walk
                    walk = walk._next
            prev._next = self._board._Node(entry, walk)
            self._board._size += 1
            if walk is None:
                self._board._tail = prev._next
            # make the lenght of self._board to capacity
            if len(self) > self._cap:
                self._board.remove_first()
        return

    def __repr__(self):
        """ String representation of the scoreboard."""
        s = ''
        walk = self._board._head
        while walk is not None:
            s += str(walk._element.get_score()) + ','
            walk = walk._next
        return s[:-1]
コード例 #2
0
class LeakyStack(object):
    """ Implementation of a leaky stack based on singly linked list.

    When push is invoked with the stack at full capacity, accept the pushed
    element at the top while “leaking” the oldest element from the bottom of
    the stack to make room.
    """
    def __init__(self, maxlen):
        super(LeakyStack, self).__init__()
        self._maxlen = maxlen
        self._data = SinglyLinkedList()
        self._size = 0

    def __len__(self):
        """Return the number of elements in the stack."""
        return self._size

    def is_empty(self):
        """Return True if the stack is empty."""
        return self._size == 0

    def push(self, e):
        """Add element e to the top of the stack.

        When stack is full, drop the bottom element to make room for new
        element.
        """
        self._data.add_first(e)
        if self._size < self._maxlen:
            self._size += 1
        elif self._size <= len(self._data) // 2:
            self._resize()

    def _resize(self):
        """ Delete the elements that not in the stack."""
        self._data.cut(self._size)

    def pop(self):
        """Remove and return the element from the top of the stack (i.e.,
        LIFO).
        Raise Empty exception if the stack is empty.
        """
        if self.is_empty():
            raise Empty('Leaky stack is empty!')
        rlt = self._data.remove_first()
        self._size -= 1
        return rlt

    def top(self):
        """ Return (but do not remove) the element at the top of the stack.
        Raise Empty exception if the stack is empty.
        """
        if self.is_empty():
            raise Empty('Leaky stack is empty!')
        return self._data.head()