0

For school i have to make an assignment -->

"The city of Amsterdam wants to store the maximum values of the past few years for research purposes. It is important that the current maximum measured value can be accessed very quickly. One idea to fulfill this requirement is to use a priority queue. Your job is to implement a priority queue with a maximum heap and return again a tuple of the current maximal measurement and its corresponding date when the maximum occurred. Output: date,covid level"

The program takes as input:

(string)’yyyy-mm-dd’, (int)sensor id, (int)covid level.

The expected output is: yyyy-mm-dd,covid level.

Input: 2022−09−08, 23, 371; 2022−09−08, 2, 3171; 2022−09−08, 12, 43; 2021−03−21, 4, 129

Output: 2022 −09 −08, 3171

I have provided my code below. When creating a max heap, the max element should be the first element (root). A Max-Heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node, though when inserting the tuples the nodes do not get sorted. My output is very strange, i do not understand where it comes from. When putting in the above input, this is my output:

1.1.1977, 9223372036854775807

could somebody help me? what piece of code am i missing, i have gone over it so many times.

import sys


class MaxHeap:

    def __init__(self, maxsize):
        self.maxsize = maxsize
        self.size = 0
        self.Heap = [0] * (self.maxsize + 1)
        self.Heap[0] = ('1.1.1977', sys.maxsize)
        self.FRONT = 1

    # Function to return the position of
    # parent for the node currently
    # at pos
    def parent(self, pos):
        return pos // 2

    # Function to return the position of
    # the left child for the node currently
    # at pos
    def leftChild(self, pos):
        return 2 * pos

    # Function to return the position of
    # the right child for the node currently
    # at pos
    def rightChild(self, pos):
        return (2 * pos) + 1

    # Function that returns true if the passed
    # node is a leaf node
    def isLeaf(self, pos):
        if pos >= (self.size // 2) and pos <= self.size:
            return True
        return False

    # Function to swap two nodes of the heap
    def swap(self, fpos, spos):
        self.Heap[fpos], self.Heap[spos] = (self.Heap[spos],
                                            self.Heap[fpos])

    # Function to heapify the node at pos
    def maxHeapify(self, pos):
        if not self.isLeaf(pos):
            if (self.Heap[pos] < self.Heap[self.leftChild(pos)] or
                    self.Heap[pos] < self.Heap[self.rightChild(pos)]):
                if (self.Heap[self.leftChild(pos)] >
                        self.Heap[self.rightChild(pos)]):
                    self.swap(pos, self.leftChild(pos))
                    self.maxHeapify(self.leftChild(pos))
                else:
                    self.swap(pos, self.rightChild(pos))
                    self.maxHeapify(self.rightChild(pos))

    # Function to insert a node into the heap
    def insert(self, element):
        if self.size >= self.maxsize:
            return
        self.size += 1
        self.Heap[self.size] = element
        current = self.size
        while (self.Heap[current] >
               self.Heap[self.parent(current)]):
            self.swap(current, self.parent(current))
            current = self.parent(current)

    # Function to print the contents of the heap
    def Print(self):
        for i in range(1, (self.size // 2) + 1):
            print(i)
            print("PARENT : " + str(self.Heap[i]) +
                  "LEFT CHILD : " + str(self.Heap[2 * i]) +
                  "RIGHT CHILD : " + str(self.Heap[2 * i + 1]))

    # Function to remove and return the maximum
    # element from the heap
    def extractMax(self):
        extraction = self.Heap[self.FRONT]
        self.Heap[self.FRONT] = self.Heap[self.size]
        self.size -= 1
        self.maxHeapify(self.FRONT)
        return extraction


# Driver Code
if __name__ == "__main__":
    input = input()
    input = input.split(";")
    dates = []
    values = []
    for d in input:
        date = d.split(',', 2)
        dates.append(date[0])
        values.append(date[2])

    values = [int(x) for x in values]
    tuples = list(zip(dates, values))
    heap = MaxHeap(len(tuples) + 1)
    # print(tuples)
    for t in tuples:
        heap.insert(t)
        print(t)

    print(heap.extractMax())

  • this should help: https://stackoverflow.com/questions/36991716/whats-the-difference-between-heapq-and-priorityqueue-in-python – D.L Sep 25 '22 at 13:00
  • thanks, but unfortunately we cannot make use of any classes or built in functions – msh schoonmaak Sep 25 '22 at 13:23
  • ....but the code in the question has a class ? `MaxHeap` – D.L Sep 25 '22 at 13:24
  • Likely there is an issue with your `insert` function. I suggest initialising a correct heap, then adding one element, and adding `print(self.Heap)` statements in the `insert` function to see how the element moves up and at what point it breaks the heap property – Stef Sep 25 '22 at 13:34

0 Answers0