Advertisement
Fhernd

cola-prioridad.py

Mar 18th, 2018
2,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. import heapq
  2.  
  3. class ColaPrioridad:
  4.     def __init__(self):
  5.         self._cola = []
  6.         self._indice = 0
  7.        
  8.     def push(self, elemento, prioridad):
  9.         heapq.heappush(self._cola, (-prioridad, self._indice, elemento))
  10.         self._indice += 1
  11.        
  12.     def pop(self):
  13.         return heapq.heappop(self._cola)[-1]
  14.        
  15.  
  16. class Elemento:
  17.     def __init__(self, nombre):
  18.         self.nombre = nombre
  19.        
  20.     def __repr__(self):
  21.         return '{!r}'.format(self.nombre)
  22.        
  23.  
  24. tareas = ColaPrioridad()
  25. tareas.push(Elemento('Escribir receta LINQ'), 2)
  26. tareas.push(Elemento('Escribir receta C#'), 3)
  27. tareas.push(Elemento('Escribir receta JavaScript'), 1)
  28. tareas.push(Elemento('Escribir receta Python'), 4)
  29.  
  30. print(tareas.pop())
  31. print(tareas.pop())
  32. print(tareas.pop())
  33. print(tareas.pop())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement