コード例 #1
0
class QueueMin(object):
    def __init__(self):
        self.q = Queue()
        self.min = None

    # All queue items are always in Q,
    def __len__(self):
        return self.q.size

    def __repr__(self):
        return "{%r}/Min: %r" % (self.q, self.min)

    def __str__(self):
        return str(self.q)

    def minimum(self):
        return self.min

    # Add to Q and return
    def enqueue(self, x):
        self.q.enqueue(x)

        # update min
        # if min is None => this is the first item to be enqueued
        self.min = min(x, self.min) if self.min else x

    # Dequeue() from Q
    # if popped item was minima, Re-enqueue all items while calculating new minima
    def dequeue(self):
        # Helper function to re-enqueue all items from Queue, Q back into itself
        # while updating new minima
        def reenqueue(q):
            minima = q.front()
            for i in range(len(q)):
                x = q.dequeue()
                q.enqueue(x)
                minima = min(x, minima)
            return minima

        # SLL raises UnderFlowError if Q is empty
        item = self.q.dequeue()

        # We just dequeued the last item from the queue
        # restore min to be None
        if not self.q:
            self.min = None
        elif item == self.min:
            # Update min if item == minima
            self.min = reenqueue(self.q)

        return item

    # Return the front of Q
    def front(self):
        return self.q.front()

    # Return the back of Q
    def back(self):
        return self.q.back()
コード例 #2
0
class Stack(object):
	def __init__(self):
		self.q1 = Queue()
		self.q2 = Queue()


	# peek what's at the top of the stack
	# New items are always added to Q1's end
	# Stack's top would be at the back of Q1
	def top(self):
		return self.q1.back()


	def __len__(self):
		return self.q1.size


	# Just add to Q1 and return
	def push(self, x):
		self.q1.enqueue(x)


	# Move (n-1) items from q1 to q2
	# dequeue remaining item from q1 to return
	# swap q1, q2
	def pop(self):
		# Helper function to move 'n' items from queue a to queue b
		def move(a, b, n):
			for i in range(n):
				b.enqueue(a.dequeue())

		# Move (n-1) items from q1 to q2
		move(self.q1, self.q2, self.q1.size-1)

		# Swap Q1, Q2
		self.q1, self.q2 = self.q2, self.q1

		# Dequeue and return the last item from what was q1 earlier, and now q2
		return self.q2.dequeue()



	def __str__(self):
		slist = []
		for x in self.q1:
			slist.insert(0, str(x))

		return '[%d]: ' %(self.__len__()) + ' '.join(slist) 


	def __repr__(self):
		return "{%r , %r}" %(self.q1, self.q2)
コード例 #3
0
class Stack(object):
	def __init__(self):
		self.q = Queue()


	# peek what's at the top of the stack
	# New items are always added to Q's end
	# Stack's top would be at the back of Q
	def top(self):
		return self.q.back()


	def __len__(self):
		return self.q.size


	# Just add to Q and return
	def push(self, x):
		self.q.enqueue(x)


	# re-enqueue (n-1) items from Q, and re-enqueue back to Q
	def pop(self):
		# Helper function to reenqueue 'n' items from queue a back into it
		def reenqueue(a, n):
			for i in range(n):
				a.enqueue(a.dequeue())

		# reenqueue (n-1) items from q
		reenqueue(self.q, self.q.size-1)
		# Dequeue from the front of q
		return self.q.dequeue()



	# Actual stack order would be the q in reverse order
	def __str__(self):
		slist = []
		for x in self.q:
			slist.insert(0, str(x))

		return '[%d]: ' %(self.__len__())  +  ' '.join(slist)


	def __repr__(self):
		return "%r" %(self.q)
コード例 #4
0
class Stack(object):
    def __init__(self):
        self.q1 = Queue()
        self.q2 = Queue()

    # peek what's at the top of the stack
    # New items are always added to Q1's end
    # Stack's top would be at the back of Q1
    def top(self):
        return self.q1.back()

    def __len__(self):
        return self.q1.size

    # Just add to Q1 and return
    def push(self, x):
        self.q1.enqueue(x)

    # Move (n-1) items from q1 to q2
    # dequeue remaining item from q1 to return
    # swap q1, q2
    def pop(self):
        # Helper function to move 'n' items from queue a to queue b
        def move(a, b, n):
            for i in range(n):
                b.enqueue(a.dequeue())

        # Move (n-1) items from q1 to q2
        move(self.q1, self.q2, self.q1.size - 1)

        # Swap Q1, Q2
        self.q1, self.q2 = self.q2, self.q1

        # Dequeue and return the last item from what was q1 earlier, and now q2
        return self.q2.dequeue()

    def __str__(self):
        slist = []
        for x in self.q1:
            slist.insert(0, str(x))

        return '[%d]: ' % (self.__len__()) + ' '.join(slist)

    def __repr__(self):
        return "{%r , %r}" % (self.q1, self.q2)
コード例 #5
0
class Stack(object):
    def __init__(self):
        self.q = Queue()

    # peek what's at the top of the stack
    # New items are always added to Q's end
    # Stack's top would be at the back of Q
    def top(self):
        return self.q.back()

    def __len__(self):
        return self.q.size

    # Just add to Q and return
    def push(self, x):
        self.q.enqueue(x)

    # re-enqueue (n-1) items from Q, and re-enqueue back to Q
    def pop(self):
        # Helper function to reenqueue 'n' items from queue a back into it
        def reenqueue(a, n):
            for i in range(n):
                a.enqueue(a.dequeue())

        # reenqueue (n-1) items from q
        reenqueue(self.q, self.q.size - 1)
        # Dequeue from the front of q
        return self.q.dequeue()

    # Actual stack order would be the q in reverse order
    def __str__(self):
        slist = []
        for x in self.q:
            slist.insert(0, str(x))

        return '[%d]: ' % (self.__len__()) + ' '.join(slist)

    def __repr__(self):
        return "%r" % (self.q)
コード例 #6
0
class QueueMin(object):
	def __init__(self):
		self.q1 = Queue()
		self.dq2 = Deque()
		self.min = None


	# All queue items are always in Q1, 
	# DQ2 is merely used to store relative order of minimas
	def __len__(self):
		return self.q1.size


	def __repr__(self):
		return "{%r , %r}/Min: %r" %(self.q1, self.dq2, self.min)


	def __str__(self):
		return str(self.q1)


	def minimum(self):
		return self.min


	# Add item to Q1
	def enqueue(self, x):
		self.q1.enqueue(x)

		# update min
		# if min is None => this is the first item to be enqueued
		self.min = min(x, self.min) if self.min else x

		# Update DQ2 to maintain the order of minimas, 
		# so when a minima is removed, the next minima can be immediately retrieved
		# remove everything from the back of DQ2 as long as it is > item x
		while self.dq2 and self.dq2.back() > x:
			self.dq2.pop_back()

		# Add item x to DQ2
		self.dq2.push_back(x)



	# Dequeue() from Q1
	# DQ2 stores relative order of minimas for the current queue items
	# if popped item was minima, update minima to DQ2 new head()
	def dequeue(self):
		# SLL raises UnderFlowError if Q2 is empty
		item = self.q1.dequeue()

		# item removed was the current minima, remove it from DQ2 as well
		# item would be found in the head of DQ2
		if item == self.min:
			self.dq2.pop_front()
			# Also, update new min which would be the new head of DQ2 after removing the current minima
			# NOTE: self.min should be None, if dq2 is empty - ie, we just removed the last item in the Queue
			self.min = self.dq2.front() if self.dq2 else None

		return item


	# Return the front of Q1
	def front(self):
		return self.q1.front()


	# Return the back of Q1
	def back(self):
		return self.q1.back()
コード例 #7
0
class QueueMin(object):
	def __init__(self):
		self.q = Queue()
		self.min = None


	# All queue items are always in Q, 
	def __len__(self):
		return self.q.size


	def __repr__(self):
		return "{%r}/Min: %r" %(self.q, self.min)


	def __str__(self):
		return str(self.q)


	def minimum(self):
		return self.min


	# Add to Q and return
	def enqueue(self, x):
		self.q.enqueue(x)

		# update min
		# if min is None => this is the first item to be enqueued
		self.min = min(x, self.min) if self.min else x


	# Dequeue() from Q 
	# if popped item was minima, Re-enqueue all items while calculating new minima
	def dequeue(self):
		# Helper function to re-enqueue all items from Queue, Q back into itself
		# while updating new minima
		def reenqueue(q):
			minima = q.front()
			for i in range(len(q)):
				x = q.dequeue()
				q.enqueue(x)
				minima = min(x, minima)
			return minima

		# SLL raises UnderFlowError if Q is empty
		item = self.q.dequeue()

		# We just dequeued the last item from the queue
		# restore min to be None
		if not self.q:
			self.min = None
		elif item == self.min:
			# Update min if item == minima
			self.min = reenqueue(self.q)

		return item


	# Return the front of Q
	def front(self):
		return self.q.front()


	# Return the back of Q
	def back(self):
		return self.q.back()
コード例 #8
0
class QueueMin(object):
    def __init__(self):
        self.q1 = Queue()
        self.q2 = Queue()
        self.min = None

    # All queue items are always in Q1, Q2 is merely used to move queue elements and update minima
    # after which Q2 is renamed Q1
    def __len__(self):
        return self.q1.size

    def __repr__(self):
        return "{%r , %r}/Min: %r" % (self.q1, self.q2, self.min)

    def __str__(self):
        return str(self.q1)

    def minimum(self):
        return self.min

    # Add to Q1 and return
    def enqueue(self, x):
        self.q1.enqueue(x)

        # update min
        # if min is None => this is the first item to be enqueued
        self.min = min(x, self.min) if self.min else x

    # Dequeue() from Q2
    # if popped item was minima, use Q2 to move all items while calculating new minima
    def dequeue(self):
        # Helper function to move all items from Queue, A to Queue, B
        # while updating new minima
        def move(a, b):
            minima = a.front()
            while a:
                x = a.dequeue()
                b.enqueue(x)
                minima = min(x, minima)
            return minima

        # SLL raises UnderFlowError if Q2 is empty
        item = self.q1.dequeue()

        # We just dequeued the last item from the queue
        # restore min to be None
        if not self.q1:
            self.min = None
        elif item == self.min:
            # Update min if item == minima
            self.min = move(self.q1, self.q2)
            # swap Q1, Q2
            self.q1, self.q2 = self.q2, self.q1

        return item

    # Return the front of Q1
    def front(self):
        return self.q1.front()

    # Return the back of Q1
    def back(self):
        return self.q1.back()