Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
797 B
1
Indexable
Never
import heapq

class News:
    def __init__(self, id, section):
        self.id = id
        self.section = section

class NewsHeap:
    def __init__(self):
        self.heap = []

    def insert(self, news):
        heapq.heappush(self.heap, (news.section, news))

    def get_min(self):
        if self.heap:
            return self.heap[0][1]
        else:
            return None

    def remove_min(self):
        if self.heap:
            return heapq.heappop(self.heap)[1]
        else:
            return None

# Example usage:
news1 = News(1, 5)
news2 = News(2, 3)
news3 = News(3, 8)

heap = NewsHeap()
heap.insert(news1)
heap.insert(news2)
heap.insert(news3)

min_news = heap.get_min()
print(f"Min news: id={min_news.id}, section={min_news.section}")

# Output: Min news: id=2, section=3