TypeError: ‘int’ object is not callable
class TopKHeap:
# The constructor of the class to initialize an empty data structure
def __init__(self, k):
self.k = k
self.A = []
self.H = MinHeap()
def size(self):
return len(self.A) + (self.H.size())
def get_jth_element(self, j):
assert 0 <= j < self.k-1
assert j < self.size()
return self.A[j]
def satisfies_assertions(self):
# is self.A sorted
for i in range(len(self.A) -1 ):
assert self.A[i] <= self.A[i+1], f’Array A fails to be sorted at position {i}, {self.A[i], self.A[i+1]}’
# is self.H a heap (check min-heap property)
self.H.satisfies_assertions()
# is every element of self.A less than or equal to each element of self.H
for i in range(len(self.A)):
assert self.A[i] <= self.H.min_element(), f’Array element A[{i}] = {self.A[i]} is larger than min heap element {self.H.min_element()}’
# Function : insert_into_A
# This is a helper function that inserts an element `elt` into `self.A`.
# whenever size is < k,
# append elt to the end of the array A
# Move the element that you just added at the very end of
# array A out into its proper place so that the array A is sorted.
# return the “displaced last element” jHat (None if no element was displaced)
def insert_into_A(self, elt):
print(“k = “, k)
assert(self.size() < k)
self.A.append(elt)
j = len(self.A)-1
while (j >= 1 and self.A[j] < self.A[j-1]):
# Swap A[j] and A[j-1]
(self.A[j], self.A[j-1]) = (self.A[j-1], self.A[j])
j = j -1
return
# Function: insert — insert an element into the data structure.
# Code to handle when self.size < self.k is already provided
def insert(self, elt):
size = self.size()
# If we have fewer than k elements, handle that in a special manner
if size <= self.k:
self.insert_into_A(elt)
return
# Code up your algorithm here.
# your code here
k = self.k # 9/11/21
size = self.size()
# If we have fewer than k elements, handle that in a special manner
if size <= self.k:
self.insert_into_A(elt)
return
# Code up your algorithm here.
# your code here
# If elt is larger than the ‘least k’ array A, insert into the heap
if elt >= max(self.A):
self.H.insert(elt)
elif elt < max(self.A):
# If elt is small enough to belog in A, insert into A
self.insert_into_A(elt)
# Then take the largest element of A and insert it into H
self.H.insert(max(self.A))
self.A = self.A[0:5]
return
# Function: Delete top k — delete an element from the array A
# In particular delete the j^{th} element where j = 0 means the least element.
# j must be in range 0 to self.k-1
def delete_top_k(self, j):
k = self.k
assert self.size() > k # we need not handle the case when size is less than or equal to k
assert j >= 0
assert j < self.k
# your code here
self.A = self.A[:j] + self.A[j+1:]
self.insert_into_A(self.H.min_element())
self.H.delete_min()
class TopKHeap:
# The constructor of the class to initialize an empty data structure
def __init__(self, k):
self.k = k
self.A = []
self.H = MinHeap()
def size(self):
return len(self.A) + (self.H.size())
def get_jth_element(self, j):
assert 0 <= j < self.k-1
assert j < self.size()
return self.A[j]
def satisfies_assertions(self):
# is self.A sorted
for i in range(len(self.A) -1 ):
assert self.A[i] <= self.A[i+1], f’Array A fails to be sorted at position {i}, {self.A[i], self.A[i+1]}’
# is self.H a heap (check min-heap property)
self.H.satisfies_assertions()
# is every element of self.A less than or equal to each element of self.H
for i in range(len(self.A)):
assert self.A[i] <= self.H.min_element(), f’Array element A[{i}] = {self.A[i]} is larger than min heap element {self.H.min_element()}’
# Function : insert_into_A
# This is a helper function that inserts an element `elt` into `self.A`.
# whenever size is < k,
# append elt to the end of the array A
# Move the element that you just added at the very end of
# array A out into its proper place so that the array A is sorted.
# return the “displaced last element” jHat (None if no element was displaced)
def insert_into_A(self, elt):
print(“k = “, k)
assert(self.size() < k)
self.A.append(elt)
j = len(self.A)-1
while (j >= 1 and self.A[j] < self.A[j-1]):
# Swap A[j] and A[j-1]
(self.A[j], self.A[j-1]) = (self.A[j-1], self.A[j])
j = j -1
return
# Function: insert — insert an element into the data structure.
# Code to handle when self.size < self.k is already provided
def insert(self, elt):
size = self.size()
# If we have fewer than k elements, handle that in a special manner
if size <= self.k:
self.insert_into_A(elt)
return
# Code up your algorithm here.
# your code here
k = self.k # 9/11/21
size = self.size()
# If we have fewer than k elements, handle that in a special manner
if size <= self.k:
self.insert_into_A(elt)
return
# Code up your algorithm here.
# your code here
# If elt is larger than the ‘least k’ array A, insert into the heap
if elt >= max(self.A):
self.H.insert(elt)
elif elt < max(self.A):
# If elt is small enough to belog in A, insert into A
self.insert_into_A(elt)
# Then take the largest element of A and insert it into H
self.H.insert(max(self.A))
self.A = self.A[0:5]
return
# Function: Delete top k — delete an element from the array A
# In particular delete the j^{th} element where j = 0 means the least element.
# j must be in range 0 to self.k-1
def delete_top_k(self, j):
k = self.k
assert self.size() > k # we need not handle the case when size is less than or equal to k
assert j >= 0
assert j < self.k
# your code here
self.A = self.A[:j] + self.A[j+1:]
self.insert_into_A(self.H.min_element())
self.H.delete_min()
h = TopKHeap(5)
# Force the array A
h.A = [-10, -9, -8, -4, 0]
# Force the heap to this heap
[h.H.insert(elt) for elt in [1, 4, 5, 6, 15, 22, 31, 7]]
print(‘Initial data structure: ‘)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
# Insert an element -2
print(‘Test 1: Inserting element -2’)
h.insert(-2)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
# After insertion h.A should be [-10, -9, -8, -4, -2]
# After insertion h.H should be [None, 0, 1, 5, 4, 15, 22, 31, 7, 6]
assert h.A == [-10,-9,-8,-4,-2]
assert h.H.min_element() == 0 , ‘Minimum element of the heap is no longer 0’
h.satisfies_assertions()
print(‘Test2: Inserting element -11’)
h.insert(-11)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
assert h.A == [-11, -10, -9, -8, -4]
assert h.H.min_element() == -2
h.satisfies_assertions()
print(‘Test 3 delete_top_k(3)’)
h.delete_top_k(3)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
h.satisfies_assertions()
assert h.A == [-11,-10,-9,-4,-2]
assert h.H.min_element() == 0
h.satisfies_assertions()
print(‘Test 4 delete_top_k(4)’)
h.delete_top_k(4)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
assert h.A == [-11, -10, -9, -4, 0]
h.satisfies_assertions()
print(‘Test 5 delete_top_k(0)’)
h.delete_top_k(0)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
assert h.A == [-10, -9, -4, 0, 1]
h.satisfies_assertions()
print(‘Test 6 delete_top_k(1)’)
h.delete_top_k(1)
print(‘t A = ‘, h.A)
print(‘t H = ‘, h.H)
assert h.A == [-10, -4, 0, 1, 4]
h.satisfies_assertions()
print(‘All tests passed – 15 points!’)
Initial data structure: A = [-10, -9, -8, -4, 0] H = [1, 4, 5, 6, 15, 22, 31, 7] Test 1: Inserting element -2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more