Exemple #1
0
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     if not lists:
         return None
     st = PriorityQueue()
     for x in lists:
         if x:
             st.push(x, x.val)
     if st.size() == 0:
         return None
         
     head = tail = None
     while st.size() != 0:
         ptr = st.pop()
         if ptr.next:
             st.push(ptr.next, ptr.next.val)
         ptr.next = None
         
         if ptr == None:
             continue
         if tail == None:
             head = ptr
         else:
             tail.next = ptr
         tail = ptr
         tail.next = None
     return head
Exemple #2
0
class WorkersList(object):
	"""docstring for WorkersList"""
	def __init__(self):
		super(WorkersList, self).__init__()
		self.queue = PriorityQueue()
		self.workers = []

	def empty_queue(self):
		while not self.queue.empty():
			next_level = self.queue.get()
			print 'Processing level:', next_level.schedule

	def get_from_queue(self):
		return self.queue.get()

	def queue_size(self):
		return self.queue.size()

	def _collect_workers(self):
		while not self.queue.empty():
			next_job = self.queue.get()
			worker = Worker(next_job, next_job.description)
			self.workers.append(worker)
			worker.start()

	def start(self):
		self._collect_workers()
		for worker in self.workers:
			worker.join()

	def put_in_queue(self, job):
		self.queue.put(job)

	def stop_single_worker(self, value):
		for worker in self.workers:
			if worker.name == value:
				worker.triggerStop = False
				break
		else:
			raise WorkerException("Worker with name/id {}".format(value))

	def stop_all_workers(self):
		for worker in self.workers:
			worker.triggerStop = False

	def terminate_all_workers(self):
		for worker in workers:
			worker.terminate()

	def terminate_single_worker(self, value):
		for worker in self.workers:
			if worker.name == value:
				worker.terminate()
				break
		else:
			raise WorkerException("Worker with name/id {}".format(value))
Exemple #3
0
def mergeKsortedList(self, lists):
    temp = ListNode(None)
    curr = temp

    q = PriorityQueue()
    for node in lists:
        if node:
            q.put((node.val, node))

    while q.size() > 0:
        curr.next = q.get()[1]
        curr = curr.next
        if curr.next:
            q.put((curr.next.val, curr.next))

    return curr.next