Advertisement
hoangreal

inserts the pins to columns with least consumed heights

Oct 21st, 2024 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. """
  2. Given a set of pins with attributes (pin id, height), write a function that takes the argument 'k' (determining the number of columns)
  3. and inserts the pins such that every pin goes into a column with least consumed height. If there is a tie then insert into the left most column.
  4. """
  5.  
  6. import heapq
  7.  
  8. class PriorityQueue:
  9.     def __init__(self):
  10.         self._queue = []
  11.  
  12.     def pop(self):
  13.         return heapq.heappop(self._queue)
  14.  
  15.     def push(self, item, lst, priority):
  16.         heapq.heappush(self._queue, (priority, lst, item))
  17.  
  18. def arrange_pins(pins, n_col):
  19.     position = PriorityQueue()
  20.    
  21.     for i in range(n_col):
  22.         position.push(f'Col_{i}', [], 0)
  23.    
  24.     for pin, name in pins:
  25.         height, lst, name_col = position.pop()
  26.         position.push(name_col, lst + [name], height + pin)
  27.    
  28.     return position._queue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement