class FavoritesList:
    """ List of elements from most accessed to list """

    class _Item:
        __slots__ = '_value', '_count'

        def __init__(self, e):
            self._value = e
            self._count = 0

    def __init__(self):
        self._data = PositionalList()

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

    def is_empty(self):
        return len(self._data) == 0

    def _find_position(self, e):
        walk = self._data.first()
        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        """ Based on access count
        """
        if p != self._data.first():
            cnt = p.element()._count
            walk = self._data.before(p)
            if cnt > walk.element()._count:
                while (walk != self._data.first()
                       and cnt > self._data.before(walk).element()._count):
                    walk = self._data.before(walk)
                self._data.add_before(walk, self._data.delete(p))

    def access(self, e):
        p = self._find_position(e)
        if p is None:
            p = self._data.add_last(self._Item(e))
        p.element()._count += 1
        self._move_up(p)

    def remove(self, e):
        p = self._find_position(e)
        if p is not None:
            self._data.delete(p)

    def top(self, k):
        """
        :return: sequence of top k elements
        """
        if not 1 <= k <= len(self):
            raise ValueError('Illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element()
            yield item._value
            walk = self._data.after(walk)
Beispiel #2
0
class SortedPQ(PriorityQueueBase):
    """ Sorted priority queue"""

    def __init__(self):
        self._data = PositionalList()

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

    def add(self, k, v):
        newest = self._Item(k, v)
        walk = self._data.last()
        while walk is not None and newest < walk.item():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(newest)
        else:
            self._data.add_after(newest, walk)

    def min(self):
        item = self._data.first().item()
        return item._key, item._val

    def remove_min(self):
        p = self._data.first()
        item = p.item()
        self._data.delete(p)
        return item._key, item._val

    def __iter__(self):
        for item in self._data:
            yield item
Beispiel #3
0
class SortedPriorityQueue(PriorityQueueBase):
    """A min-oriented priority queue implemented with a sorted list"""
    def __init__(self):
        self._data = PositionalList()

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

    def min(self):
        if self.is_empty():
            raise ValueError("Empty Priority Queue")
        item = self._data.first().element()
        return item._key, item._value

    def remove_min(self):
        if self.is_empty():
            raise ValueError("Empty Priority Queue")
        item = self._data.delete(self._data.first())
        return item._key, item._value

    def add(self, key, value):
        new_item = self._Item(key, value)
        walk = self._data.last()
        # walk backwards for smaller key
        while walk != None and new_item < walk.element():
            walk = self._data.before(walk)
        if walk:
            self._data.add_after(new_item, walk)
        else:  # walk is the header
            self._data.add_first(new_item)
Beispiel #4
0
class SortedPriorityQueue(PriorityQueueBase):
    def __init__(self) -> None:
        self._data = PositionalList()

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

    def __iter__(self):
        return iter(self._data)

    def add(self, key, value):
        newest: self._Item = self._Item(key, value)
        walk = self._data.last()
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(newest)
        else:
            self._data.add_after(walk, newest)

    def min(self):
        if self.is_empty():
            raise Exception("Priority queue is empty")
        p = self._data.first()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        if self.is_empty():
            raise Exception('Priority queue is empty.')
        item = self._data.delete(self._data.first())
        return (item._key, item._value)
Beispiel #5
0
class SortedPriorityQueue(PriorityQueueBase):
    """
    a min-oriented priority queue implemented with a sorted list
    """

    # base class defines _Item

    def __init__(self):
        """
        create a new empty Priority queue
        """
        self._data = PositionalList()

    def __len__(self):
        """
        return the number of items in the priority queue
        :return:
        """
        return len(self._data)

    def add(self, key, value):
        """
        add a key-value pair
        :param key:
        :param value:
        :return:
        """
        newest = self._Item(key, value)  # make new item instance
        walk = self._data.last(
        )  # walk backward looking for smaller key, [last() return a position]
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(newest)  # new key is smallest
        else:
            self._data.add_after(walk, newest)  # newest goes after walk

    def min(self):
        """
        return but do not remove (k,v) tuple with minimum key
        :return:
        """
        if self.is_empty():
            raise Empty('priority queue is empty')
        p = self._data.first()  # return a position
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """
        remove and return (k,v) tuple with minimum key
        :return:
        """
        if self.is_empty():
            raise Empty('priority queue is empty')
        item = self._data.delete(
            self._data.first())  # delete() return the element
        return (item._key, item._value)
class SortedPriorityQueue(PriorityQueueBase):
    """
    A min-oriented priority queue implemented with an sorted list.
    """
    def __init__(self):
        """
        Create a new empty Priority Queue.
        :return:
        """
        self._data = PositionalList()

    def __len__(self):
        """
        Return the number of items in the priority queue.
        :return:
        """
        return len(self._data)

    def add(self, key, value):
        """
        Add a key-value pair.
        :param key:
        :param value:
        :return:
        """
        newest = self._Item(key, value)
        walk = self._data.last()
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:
            self._data._add_first(newest)
        else:
            self._data.add_before(walk, newest)

    def min(self):
        """
        Return but do not remove(k,v) tuple with minimum key.
        :return:
        """
        if self.is_empty():
            raise Empty("Priority queue is empty.")
        p = self._data.first()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """
        Remove and return (k,v) tuple with minimun key.
        :return:
        """
        if self.is_empty():
            raise Empty("Priority queue is empty.")
        item = self._data.delete(self._data.first())
        return (item._key, item._value)
class UnsortedPriorityQueue(PriorityQueueBase):
    """ A min-oriented priority queue implemented with an unsorted list
        min() : O(n)
        remove_min : O(n)
        add() : O(1)
    """

    def __init__(self):
        self._data = PositionalList()

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

    def add(self, key, value):
        self._data.add_last(self._Item(key, value))

    def min(self):
        """
        :return: (k,v) tuple with minimum key (not remove)
        """
        p = self._find_min()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """
        :return: remove and return (k,v) with minimum key k
        """
        p = self._find_min()
        item = self._data.delete(p)
        return (item._key, item._value)

    def _find_min(self):
        """
        :return: Position of item with minimum key
        """
        if self.is_empty():
            raise EmptyException('Priority Queue is empty')
        small = self._data.first()
        walk = self._data.after(small)
        while walk is not None:
            if walk.element() < small.element():
                small = walk
            walk = self._data.after(walk)
        return small

    def __str__(self):
        res = []
        walk = self._data.first()
        while walk is not None:
            item = walk.element()
            res.append((item._key, item._value))
            walk = self._data.after(walk)
        return str(res)
Beispiel #8
0
def main():
    global screen,SCREEN_WIDTH,SCREEN_SIZE,deltaX,deltaY
    vertex=PositionalList()
    running=True
    done=False
    clickTrue=False
    getPrev=False
    deltaX=0
    deltaY=0
    x=0
    y=0
    screen.fill((255,255,255))
    while running:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                running=False
            elif event.type==pygame.MOUSEBUTTONDOWN:
                (posx,posy)=pygame.mouse.get_pos()
                if done==False and len(vertex)>=0:
                    if (x-posx)**2+(y-posy)**2<=25:
                        vertex.add_last(vertex.first().element())
                    else:
                        vertex.add_last((posx,posy))
                    if len(vertex)>1:
                        (x,y)=vertex.first().element()
                        if (x-posx)**2+(y-posy)**2<=25:
                            done=True
                            (posx,posy)=vertex.first().element()
                        p=vertex.before(vertex.last())
                        pygame.draw.line(screen,(0,0,0),p.element(),(posx,posy))
                    pygame.draw.circle(screen,(0,0,0),(posx,posy),5)
                else:
                    if click(vertex,(posx,posy)):
                        clickTrue=not clickTrue
                        getPrev=False

            elif event.type==pygame.MOUSEMOTION and clickTrue:
                    if done==True:
                        (posx,posy)=pygame.mouse.get_pos()
                        screen.fill((255,255,255))
                        if getPrev:
                            deltaX=(posx-pPosx)
                            deltaY=(posy-pPosy)
                        updateVertex(vertex,deltaX,deltaY)
                        drawPolygon(vertex)
                        getPrev=True
                        (pPosx, pPosy) = (posx,posy)
            elif event.type==pygame.MOUSEBUTTONUP:
                clickTrue=False
        pygame.display.flip()
class SortedPriorityQueue(PriorityQueueBase):
    """ A min-oriented priority queue implementation with sorted list
        min() : O(1)
        remove_min : O(1)
        add() : O(n)
    """
    def __init__(self):
        self._data = PositionalList()

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

    def add(self, key, value):
        """  Add a key-value pair at a correct position
        """
        newest = self._Item(key, value)
        # walk backward
        walk = self._data.last()
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:        # new key is smallest
            self._data.add_first(newest)
        else:
            self._data.add_after(walk, newest)

    def min(self):
        """
        :return: tuple (key, value) with minimum key
        """
        if self.is_empty():
            raise EmptyException('Priority Queue is empty')
        p = self._data.first()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        if self.is_empty():
            raise EmptyException('Priority Queue is Empty')
        item = self._data.delete(self._data.first())
        return (item._key, item._value)

    def __str__(self):
        res = []
        walk = self._data.first()
        while walk is not None:
            item = walk.element()
            res.append((item._key, item._value))
            walk = self._data.after(walk)
        return str(res)
Beispiel #10
0
    def top(self, k):
        """
        Generate sequence of top k elements in terms of access count.
        :param k:
        :return:
        """
        if not 1 <= k <= len(self):
            raise ValueError("Illegal value for k.")

        temp = PositionalList()
        for item in self._data:
            temp.add_last(item)

        for j in range(k):
            #find and report next highest form temp
            ## O(kn)
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)

            yield highPos.element()._value
            temp.delete(highPos)
Beispiel #11
0
class UnsortedPriorityQueue(PriorityQueueBase):
    '''a min-oriented priority queue implemented with an unsorted list'''
    def __init__(self) -> None:
        self._data = PositionalList()

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

    def __iter__(self):
        return iter(self._data)

    def _find_min(self):
        if self.is_empty():
            raise Exception("Priority Queue is empty.")
        small = self._data.first()
        walk = self._data.after(small)
        while walk is not None:
            if walk.element() < small.element():
                small = walk
            walk = self._data.after(walk)
        return small

    def add(self, key, value):
        self._data.add_last(self._Item(key, value))

    def min(self):
        p = self._find_min()
        item: self._Item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        p = self._find_min()
        item: self._Item = self._data.delete(p)
        return (item._key, item._value)
    def top(self, k):
        """
        Generate sequence of top k elements in terms of access count.
        :param k:
        :return:
        """
        if not 1 <= k <= len(self):
            raise ValueError("Illegal value for k.")

        temp = PositionalList()
        for item in self._data:
            temp.add_last(item)

        for j in range(k):
            #find and report next highest form temp
            ## O(kn)
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)

            yield highPos.element()._value
            temp.delete(highPos)
Beispiel #13
0
    def top(self, k):
        """
        generate sequence of top k elements in terms of access count
        :param k:
        :return:
        """
        if not 1 <= k <= len(self):
            raise ValueError('illegal value for k')

        # making a copy of the original list
        temp = PositionalList()
        for item in self._data:  # positional lists support iteration
            temp.add_last(item)

        # repeatedly find, report and remove element with largest count
        for j in range(k):
            # find and report next highest from temp
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)
            # found the element with highest count
            yield highPos.element()._value  # report element to user
            temp.delete(highPos)  # remove from temp list
Beispiel #14
0
class UnsortedPriorityQueue(PriorityQueueBase):
    """
    A min-oriented priority queue implemented with an unsorted list.
    """

    def __init__(self):
        """
        Create an empty Priority Queue.
        :return:
        """
        self._data = PositionalList()

    def _find_min(self):
        """
        Return Position of item with minimum key.
        :return:
        """
        if self.is_empty():
            raise Empty("Priority queue is empty.")
        small = self._data.first()
        walk = self._data.after(small)
        while walk is not None:
            if walk.element() < small.element():
                small = walk
            walk = self._data.after(walk)
        return small

    def min(self):
        """
        Return but do not remove (k,v) tuple with minimum key.
        :return:
        """
        p = self._find_min()
        item = p.element()
        return (item._key, item._value)

    def __len__(self):
        """
        Return the number of items in the priority queue.
        :return:
        """
        return len(self._data)

    def add(self, key, value):
        """
        Add a key-value pair.
        :param key:
        :param value:
        :return:
        """
        self._data.add_last(self._Item(key, value))

    def remove_min(self):
        """
        Remove and return (k,v) tuple with minimum key.
        :return:
        """
        p = self._find_min()
        item = self._data.delete(p)
        return (item._key, item._value)
Beispiel #15
0
class FavoritesList:
    class _Item:
        __slots__ = '_value', '_count'

        def __init__(self, v):
            self._value = v
            self._count = 0

    def _find_position(self, e):
        """ Search for element e and return its Position """
        walk = self._data.first()
        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        """ Move item at Position p earlier in the list based on access count """
        if p != self._data.first():
            walk = self._data.before(p)
            cnt = p.element()._count
            if cnt > walk.element()._count:
                while walk != self._data.first() and cnt > self._data.before(
                        walk).element()._count:
                    walk = self._data.before(walk)
                self._data.add_before(walk, self._data.delete(p))

    """ ==================================Public Methods==================================== """

    def __init__(self):
        self._data = PositionalList()

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

    def is_empty(self):
        return self._data.is_empty()

    def access(self, e):
        """ Access element e, thereby increasing its access count """
        p = self._find_position(e)

        if p is None:
            p = self._data.add_last(self._Item(e))
        p.element()._count += 1
        self._move_up(p)

    def remove(self, e):
        p = self._find_position(e)
        if p is not None:
            self._data.delete(p)

    def top(self, k):
        if not 1 <= k <= len(self):
            raise ValueError("Illegal value for k")
        walk = self._data.first()
        for _ in range(k):
            item = walk.element()._value
            yield item
            walk = self._data.after(walk)
Beispiel #16
0
class TextEditor:
    def __init__(self):
        self._data = PositionalList()
        self._data.add_first(None)
        self._cursor = self._data.first()
        self._size = 0

    def left(self):
        prev = self._data.before(self._cursor)
        if prev is not None:
            self._cursor = prev

    def right(self):
        nex = self._data.after(self._cursor)
        if nex is not None:
            self._cursor = nex

    def insert(self, char):
        self._size += 1
        self._data.add_after(self._cursor, char)
        self._cursor = self._data.after(self._cursor)

    def delete(self):
        nex = self._data.after(self._cursor)
        if nex is not None:
            self._data.delete(nex)
            self._size -= 1

    def __str__(self):
        end = ''
        node = self._data.first()

        for x in range(self._size + 1):
            if node == self._cursor:
                cursor = x
            node = self._data.after(node)
            if node is not None:
                end += node.element()
        end += '\n'
        end += ' ' * cursor + '^'
        return end
    def top(self, k):
        if not 1 <= k <= len(self):
            raise ValueError('Bad k')

        temp = PositionalList()
        for item in self._data:
            temp.add_last(item)

        # we repeatedly find, report, and remove element with largest count
        for j in range(k):
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)
            yield highPos.element()._value
            temp.delete(highPos)
Beispiel #18
0
    def top(self, k):
        """Generate sequnce of top k elements in terms of access count"""
        if not 1 <= k <= len(self):
            raise ValueError("Illegal value for k")

        # we begin by making a copy of the original list
        temp = PositionalList()
        for item in self._data:
            temp.add_last(item)

        # we repeatedly find, report, and remove element with largest count
        for j in range(k):
            # find and report next highest from temp
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)
            # we have found the element with highest count
            yield highPos.element()._value
            temp.delete(highPos)
Beispiel #19
0
    def top(self, k):
        """Generate sequence of top k elements in terms of access count."""
        if not 1 <= k <= len(self):
            raise ValueError('Illegal value for k')

        # we begin by making a copy of the original list
        temp = PositionalList()
        for item in self._data: # positional lists support iteration
            temp.add_last(item)

        # we repeatedly find, report, and remove element with largest count
        for j in range(k):
            # find and report next highest from temp
            highPos = temp.first()
            walk = temp.after(highPos)
            while walk is not None:
                if walk.element()._count > highPos.element()._count:
                    highPos = walk
                walk = temp.after(walk)
            # we have found the element with highest count
            yield highPos.element()._value # report element to user
            temp.delete(highPos) # remove from temp list
class UnsortedPQ(PriorityQueueBase):
    """ Unsorted priority queue """
    def __init__(self):
        self._data = PositionalList()

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

    def _find_min(self):

        if self.is_empty():
            raise Empty("Queue is empty!")
        min = self._data.first()
        walker = self._data.after(min)
        while walker is not None:
            if min.item() > walker.item():
                min = walker
            walker = self._data.after(walker)
        return min

    def min(self):
        p = self._find_min()
        return p.item()._key, p.item()._val

    def remove_min(self):
        p = self._find_min()
        item = p.item()
        self._data.delete(p)
        return item._key, item._val

    def add(self, k, v):
        self._data.add_last(self._Item(k, v))

    def __iter__(self):
        for item in self._data:
            yield item
class UnsortedPriorityQueue(PriorityQueueBase):
    """A min-oriented priority queue implemented with an unsorted list"""
    def __init__(self):
        self._data = PositionalList()

    def _find_min(self):
        """Return Position of item with minimum key"""
        if self._data.is_empty():  # empty queue
            raise ValueError("Empty Priority Queue")
        small = self._data.first()
        walk = self._data.after(small)
        while walk != None:
            # find a item with smaller key or higher priority
            if small.element() > walk.element():
                small = walk
            walk = self._data.after(walk)
        return small

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

    def add(self, key, value):
        """Add a key-value pair"""
        self._data.add_last(self._Item(key, value))

    def min(self):
        """Return but do not remove (k,v) tuple with minimum key"""
        item = self._find_min().element()
        return item._key, item._value

    def remove_min(self):
        """Remove and return (k,v) tuple with minimum key"""
        p = self._find_min()  # find the position
        item = self._data.delete(
            p)  # delete the position and return the element
        return item._key, item._value
class FavoritesList:
    """List of elements ordered from most frequently accessed to least."""

    # ------------------------------ nested _Item class ------------------------------
    class _Item:
        __slots__ = '_value', '_count'  # streamline memory usage

        def __init__(self, e):
            self._value = e  # the user's element
            self._count = 0  # access count initially zero

    # ------------------------------- nonpublic utilities -------------------------------
    def _find_position(self, e):
        """Search for element e and return its Position (or None if not found)."""
        walk = self._data.first()

        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        """Move item at Position p earlier in the list based on access count."""
        if p != self._data.first():  # consider moving...
            cnt = p.element()._count
            walk = self._data.before(p)
            if cnt > walk.element()._count:  # must shift forward
                while (walk != self._data.first()
                       and cnt > self._data.before(walk).element()._count):
                    walk = self._data.before(walk)
                self._data.add_before(walk,
                                      self._data.delete(p))  # delete/reinsert

    # ------------------------------- public methods -------------------------------
    def __init__(self):
        """Create an empty list of favorites."""
        self._data = PositionalList()  # will be list of _Item instances

    def __len__(self):
        """Return number of entries on favorites list."""
        return len(self._data)

    def is_empty(self):
        """Return True if list is empty."""
        return len(self._data) == 0

    def access(self, e):
        """Access element e, thereby increasing its access count."""
        p = self._find_position(e)  # try to locate existing element
        if p is None:
            p = self._data.add_last(self._Item(e))  # if new, place at end
        p.element()._count += 1  # always increment count
        self._move_up(p)  # consider moving forward

    def remove(self, e):
        """Remove element e from the list of favorites."""
        p = self._find_position(e)  # try to locate existing element
        if p is not None:
            self._data.delete(p)  # delete, if found

    def top(self, k):
        """Generate sequence of top k elements in terms of access count."""
        if not 1 <= k <= len(self):
            raise ValueError('Illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element()  # element of list is _Item
            yield item._value  # report user's element
            walk = self._data.after(walk)

    def __repr__(self):
        """Create string representation of the favorites list."""
        return ', '.join('({0}:{1})'.format(i._value, i._count)
                         for i in self._data)
Beispiel #23
0
class FavoritesList:
    """
    List of elements ordered from most frequently accessed to least.
    """

    #------------------------------ nested Item class ------------------------------
    class _Item:
        __slots__ = '_value', '_count'

        def __init__(self, e):
            self._value = e
            self._count = 0

    #------------------------------- nonpublic utilities -------------------------------
    def _find_position(self, e):
        """
        Search for element e and return its Position (or None if not found).
        :param e:
        :return:
        """
        walk = self._data.first()
        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        """
        Move item at Position p earlier in the list based on access count.
        :param p:
        :return:
        """
        if p != self._data.first():
            cnt = p.element()._count
            walk = self._data.before(p)
            if cnt > walk.element()._count:
                while (walk != self._data.first()
                       and cnt > self._data.before(walk)._count):
                    walk = self._data.before(walk)
                self._data.add_before(walk, self._data.delete(p))

    #------------------------------- public methods -------------------------------

    def __init__(self):
        """
        Create an empty list of favorites.
        :return:
        """
        self._data = PositionalList()

    def __len__(self):
        """
        Return number of entries on favorites list.
        :return:
        """
        return len(self._data)

    def is_empty(self):
        """
        Return True is list is empty.
        :return:
        """
        return len(self._data) == 0

    def access(self, e):
        """
        Access element e,thereby increasing its access count.
        :param e:
        :return:
        """
        p = self._find_position(e)
        if p is None:
            p = self._data.add_last(self._Item(e))
        p.element()._count += 1
        self._move_up(p)

    def remove(self, e):
        """
        Remove element e from the list of favorites
        :param e:
        :return:
        """
        p = self._find_position(e)
        if p is not None:
            self._data.delete(p)

    def top(self, k):
        """
        Generate sequence of top k elements in terms of access count.
        :param e:
        :return:
        """
        if not 1 <= len(self):
            raise ValueError('Illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element()
            yield item._value
            walk = self._data.after(walk)
Beispiel #24
0
            item = walk.element()
            yield item._value
            walk = self._data.after(walk)


if __name__ == "__main__":
    pl = PositionalList()
    pl.add_first(1)
    pl.add_first(34)
    p25 = pl.add_first(25)
    pl.add_first(35)
    pl.add_first(40)

    print([el for el in iter(pl)])
    print "the last element in list is %s" % pl.last().element()
    print "the first element in list is %s" % pl.first().element()
    print "the element before "
    print("the element before 25 is %s") % pl.before(p25).element()
    print("the element after 25 is %s") % pl.after(p25).element()
    print("Delete the element %s " % pl.delete(p25))
    print([el for el in iter(pl)])
    print("now Sort the list ..")
    pl.insertion_sort()
    print([el for el in iter(pl)])

    fl = FavoritesList()
    fl.access("12")
    fl.access("11")
    fl.access("3")
    fl.access("12")
    fl.access("4")
Beispiel #25
0
class FavoritesList:
    """
    list of elements ordered from most frequently accessed to lest
    """

    # ------------ nested _Item class ---------------------------
    class _Item:
        __slots__ = '_value', '_count'  # streamline memory usage

        def __init__(self, element):
            self._value = element  # the user's element
            self._count = 0  # access count initially zero

    # ------------ non-public utilities --------------------------
    def _find_position(self, element):
        """
        search for element and return its Position (or None if not found)
        :param element:
        :return:
        """
        walk = self._data.first()
        while walk is not None and walk.element()._value != element:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, position):
        """
        move item at position earlier in the list based on access count
        :param position:
        :return:
        """
        if position != self._data.first():  # consider moving
            cnt = position.element()._count
            walk = self._data.before(position)
            if cnt > walk.element()._count:  # must shift forward
                while (walk != self._data.first() and cnt > self._data.before(walk).element()._count):
                    walk = self._data.before(walk)
                self._data.add_before(walk, self._data.delete(position))  # delete / reinsert

    # ------------ public methods ---------------------------------
    def __init__(self):
        """
        create an empty list of favorites
        """
        self._data = PositionalList()  # list of _Item instances

    def __len__(self):
        """
        return number of entries on favorites list
        :return:
        """
        return len(self._data)

    def is_empty(self):
        """
        return True if list is empty
        :return:
        """
        return len(self._data) == 0

    def access(self, element):
        """
        access element, thereby increasing its access count
        :param element:
        :return:
        """
        position = self._find_position(element)  # try to locate existing element
        if position is None:
            position = self._data.add_last(self._Item(element))  # if new, place at end
        position.element()._count += 1  # always increment count
        self._move_up(position)  # consider moving forward

    def remove(self, element):
        """
        remove element from the list of favorites
        :param element:
        :return:
        """
        position = self._find_position(element)  # try to locate existing element
        if position is not None:  # delete if found
            self._data.delete(position)

    def top(self, k):
        """
        generate sequence of top k elements in terms of access count
        :param k:
        :return:
        """
        if not 1 <= k <= len(self):
            raise ValueError('Illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element()  # element of list is _Item
            yield item._value
            walk = self._data.after(walk)

    def clear(self):
        """
        returns the list to empty.
        :return:
        """
        self._data = PositionalList()

    def reset_counts(self):
        """
         resets all elements’ access counts to zero (while leaving the order of
         he list unchanged).
        :return:
        """
        cursor = self._data.first()
        while cursor is not None:
            cursor.element()._count = 0
            cursor = self._data.after(cursor)
for i in range(4): l.insert_last(i)
m = LinkedDeque()
for i in range(5): m.insert_last(i)
print("L+M", concatenate_doubly_linked_list(l, m))


# R-7.10
# There seems to be some redundancy in the repertoire of the positional
# list ADT, as the operation L.add first(e) could be enacted by the alter-
# native L.add before(L.first(), e). Likewise, L.add last(e) might be per-
# formed as L.add after(L.last(), e). Explain why the methods add first
# and add last are necessary.
pp = PositionalList()
pp.add_first(1)
# if PositionalList is empty, raise TypeError('position must be proper Position type')
pp.add_before(pp.first(), 2)
print(pp)


# R-7.11
# Implement a function, with calling syntax max(L), that returns the maximum
# element from a PositionalList instance L containing comparable
# elements.
def max_PositionalList(L):
    """
    return the maximum element from a PositionalList
    :param L:
    :return:
    """
    if not L.is_empty():
        cursor = L.first()  # return the first position in the list
Beispiel #27
0
            item = walk.element()
            yield item._value
            walk = self._data.after(walk)


if __name__ == "__main__":
    pl = PositionalList()
    pl.add_first(1)
    pl.add_first(34)
    p25 = pl.add_first(25)
    pl.add_first(35)
    pl.add_first(40)

    print([el for el in iter(pl)])
    print "the last element in list is %s" % pl.last().element()
    print "the first element in list is %s" % pl.first().element()
    print "the element before "
    print ("the element before 25 is %s") % pl.before(p25).element()
    print ("the element after 25 is %s") % pl.after(p25).element()
    print ("Delete the element %s " % pl.delete(p25))
    print([el for el in iter(pl)])
    print("now Sort the list ..")
    pl.insertion_sort()
    print([el for el in iter(pl)])

    fl = FavoritesList()
    fl.access("12")
    fl.access("11")
    fl.access("3")
    fl.access("12")
    fl.access("4")
Beispiel #28
0
class FavoritesList:
    """
    List of elements ordered from most frequently accessed to least.
    """
    #------------------------------ nested Item class ------------------------------
    class _Item:
        __slots__ = '_value', '_count'

        def __init__(self, e):
            self._value = e
            self._count = 0

    #------------------------------- nonpublic utilities -------------------------------
    def _find_position(self, e):
        """
        Search for element e and return its Position (or None if not found).
        :param e:
        :return:
        """
        walk = self._data.first()
        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        """
        Move item at Position p earlier in the list based on access count.
        :param p:
        :return:
        """
        if p != self._data.first():
            cnt = p.element()._count
            walk = self._data.before(p)
            if cnt > walk.element()._count:
                while (walk != self._data.first() and cnt > self._data.before(walk)._count):
                    walk = self._data.before(walk)
                self._data.add_before(walk, self._data.delete(p))


    #------------------------------- public methods -------------------------------

    def __init__(self):
        """
        Create an empty list of favorites.
        :return:
        """
        self._data = PositionalList()

    def __len__(self):
        """
        Return number of entries on favorites list.
        :return:
        """
        return len(self._data)


    def is_empty(self):
        """
        Return True is list is empty.
        :return:
        """
        return len(self._data) == 0


    def access(self, e):
        """
        Access element e,thereby increasing its access count.
        :param e:
        :return:
        """
        p = self._find_position(e)
        if p is None:
            p = self._data.add_last(self._Item(e))
        p.element()._count += 1
        self._move_up(p)

    def remove(self, e):
        """
        Remove element e from the list of favorites
        :param e:
        :return:
        """
        p = self._find_position(e)
        if p is not None:
            self._data.delete(p)

    def top(self, k):
        """
        Generate sequence of top k elements in terms of access count.
        :param e:
        :return:
        """
        if not 1 <= len(self):
            raise ValueError('Illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element()
            yield item._value
            walk = self._data.after(walk)
Beispiel #29
0
class FavoritesList:
    """List of elements ordered from most frequently accessed to least."""

    #------------------------------ nested Item class ------------------------------
    class _Item:
        __slots__ ='_value','_count'# streamline memory usage
        def __init__ (self, e):
            self._value = e # the user s element
            self._count = 0 # access count initially zero

    #------------------------------- nonpublic utilities -------------------------------
    def _find_position(self, e):
        """Search for element e and return its Position (or None if not found)."""
        walk = self._data.first()
        while walk is not None and walk.element()._value != e:
            walk = self._data.after(walk)
        return walk

    def _move_up(self, p):
        """Move item at Position p earlier in the list based on access count."""
        if p != self._data.first(): # consider moving...
            cnt = p.element()._count
            walk = self._data.before(p)
            if cnt > walk.element()._count: # must shift forward
                while (walk != self._data.first() and
                        cnt > self._data.before(walk).element()._count):
                    walk = self._data.before(walk)
                self._data.add_before(walk, self._data.delete(p)) # delete/reinsert
    #------------------------------- public methods -------------------------------
    def __init__ (self):
        """Create an empty list of favorites."""
        self._data = PositionalList() # will be list of Item instances

    def __iter__(self):
        """Generate a forward iteration of the elements of the list."""
        for i in self._data:
            yield i

    def __reversed__(self):
        """Generate a backward iteration of the elements of the list."""
        for i in reversed(self._data):
            yield i

    def __len__(self):
        """Return number of entries on favorites list."""
        return len(self._data)

    def is_empty(self):
        """Return True if list is empty."""
        return len(self._data) == 0

    def access(self, e):
        """Access element e, thereby increasing its access count."""
        p = self._find_position(e) # try to locate existing element
        if p is None:
            p = self._data.add_last(self._Item(e)) # if new, place at end
        p.element()._count += 1 # always increment count
        self._move_up(p) # consider moving forward

    def remove(self, e):
        """Remove element e from the list of favorites."""
        p = self._find_position(e) # try to locate existing element
        if p is not None:
            self._data.delete(p) # delete, if found

    def top(self, k):
        """Generate sequence of top k elements in terms of access count."""
        if not 1 <= k <= len(self):
            raise ValueError('Illegal value for k')
        walk = self._data.first()
        for j in range(k):
            item = walk.element() # element of list is Item
            yield item._value # report user’s element
            walk = self._data.after(walk)

    def clear(self):
        """Clean the list into empty"""
        self._data = PositionalList() # will be list of Item instances

    def reset_count(self):
        """Reset the count of element in list leaving the order unchanged"""
        for i in self:
            i._count = 0

    def showinfo(self):
        """Show the infomation of the current list"""
        print 'Favoriteslist info:[',
        for i in self:
            print '(',i._value,',',i._count,')',
        print ']'

    def showelement(self):
        """Show the element of the current list"""
        print 'Favoriteslist elements:[',
        for i in self:
            print i._value,
        print ']'
        predecessor.next = newest
        successor.prev = newest
        self._size += 1
        return newest

    def _delete_node(self, node):
        predecessor = node.prev
        successor = node.next
        predecessor.next = successor
        successor.prev = predecessor
        self._size -= 1
        element = node.element
        node.prev = node.next = node.element = None
        return element

    def reverse(self):
        node = self._header
        for _ in range(len(self)):
            node.next, node.prev = node.prev, node.next
            node = node.prev
        self._header, self._trailer = self._trailer, self._header

a = PositionalList()
a.add_first(1)
a.add_first(2)
a.add_first(3)
x = a.first()
print(x.element())
a.reverse()
x = a.first()
print(x.element())
    printlist(L2, 'L2 after being appended L1')
    L2.add_last('seventeen')
    printlist(L1, 'L1 after adding \'seventeen\' to L2')
    printlist(L2, 'L2 after adding \'seventeen\' to it')
    # Output
    # L1 after appending L2: 0 1 2 3 4 5 6 7 8 9 
    # L2 after being appended L1:
    #
    # (so L2 is empty)
    # L1 after adding 'seventeen' to L2: 0 1 2 3 4 5 6 7 8 9 
    # L2 after adding 'seventeen' to it: seventeen 
if True:
    #Task 2: Given a Position p, return a new Positional List that contains all
    #the elements up to and including what is at p.
    #¡Do this in constant time!
    p = L1.first()
    for i in range(4):
        p = L1.after(p)
    print('L3\'s last element should be {0}'.format(p.element()))
    L3 = L1.split(p)
    printlist(L3, 'L3')
    printlist(L1, 'L1')
    print('Length of L1 is {0}'.format(len(L1)))
    print('Length of L3 is {0}'.format(len(L3)))
    # Output:
    # L3's last element should be 4
    # L3: 0 1 2 3 4 
    # L1: 5 6 7 8 9
    # Length of L1 is 5
    # Length of L3 is 5
if True:
class FavoritesList:

    class _Item:

        def __init__(self, val):
            self._value = val
            self._count = 0

    def __init__(self):
        self._data = PositionalList()

    """ nonpublic utilities """
    def _find_position(self, val):
        walker = self._data.first()
        while walker is not None and walker.item()._value != val:
            walker = self._data.after(walker)
        return walker

    def _move_up(self, p):
        if p != self._data.first():
            walker = self._data.before(p)
            cnt = p.item()._count
            if cnt > walker.item()._count:
                while walker != self._data.first() and cnt > self._data.before(walker).item()._count:
                    walker = self._data.before(walker)

                self._data.add_before(self._data.delete(p), walker)

    """ public methods"""
    def __len__(self):
        return len(self._data)

    def is_empty(self):
        return len(self._data) == 0

    def access(self, val):
        p = self._find_position(val)
        if p is None:
            p = self._data.add_last(self._Item(val))
        p.item()._count += 1
        self._move_up(p)

    def access_item(self, item):
        val = item._value
        p = self._find_position(val)
        if p is None:
            p = self._data.add_last(item)
        self._move_up(p)

    def remove(self, val):
        p = self._find_position(val)
        if p is not None:
            self._data.delete(p)

    def top(self, k):
        if not 1 <= k <= len(self):
            raise ValueError('Illegal value for k')
        walker = self._data.first()
        for i in range(k):
            yield walker.item()._value
            walker = self._data.after(walker)