def _remove_vms(self, vms_to_remove): for vm in vms_to_remove: bin_ = self.vm_bins.get(vm.uuid) if bin_: del self.vm_bins[vm.uuid] bin_.remove(vm.uuid) if bin_.is_empty(): self.occupied_bins.remove(bin_) self.bins.append(bin_) heapq_max.heapify_max(self.occupied_bins)
import os import sys import heapq_max file = open(os.path.join(os.path.dirname(sys.argv[0]), 'rosalind_hea.txt')) lines = [line.rstrip('\n') for line in file] # Remove header first_line = lines.pop(0) n = int(first_line.split(" ")[0]) print(f"N: {n}") # Process inputs nums = [int(x) for x in lines[0].split(" ")] print(f"NUMS: {nums}") # Heap Max heap_max = [] heapq_max.heapify_max(nums) print(" ".join([str(x) for x in nums]))
# In the box below you should type the sum of these 10000 medians, # modulo 10000 (i.e., only the last 4 digits). import heapq import heapq_max from heapq_max import * MyList = [] with open("Median_HW3.txt", "r") as f: for line in f: x = int(line.rstrip()) MyList.append(x) Heap_low = [] heapq_max.heapify_max(Heap_low) Heap_high = [] heapq.heapify(Heap_high) heapq.heappush(Heap_high, MyList[0]) heapq_max.heappush_max(Heap_low, MyList[1]) M = 6331 + 2793 for x in MyList[2:]: if x >= Heap_high[0]: heapq.heappush(Heap_high, x) else: heapq_max.heappush_max(Heap_low, x) combined_len = len(Heap_low) + len(Heap_high)
import heapq_max if __name__ == '__main__': Jobs = [] with open("jobs.txt") as f: for x in f: weight, length = map(int, x.split()) #Jobs.append((weight-length, weight, length)) Jobs.append((weight / length, weight, length)) heapq_max.heapify_max(Jobs) currentTime = 0 sum = 0 while Jobs: job = heapq_max.heappop_max(Jobs) currentTime += job[2] sum += (job[1] * currentTime) print(sum)
def __init__(self, arr): heapify_max(arr) self.heap = arr
import heapq_max arr = [4, 3, 33, 90, 321, 23, 567] heapq_max.heapify_max(arr) print(arr) print(heapq_max.heappop_max(arr)) heapq_max.heappush_max(arr, 999) print(arr) heapq_max.heapreplace_max(arr, 100) print(arr)