class MedianHeap(object): def __init__(self): self.minheap = MinHeap() self.maxheap = MaxHeap() def get_median(self): if self.minheap.size == self.maxheap.size: return (self.minheap.get_min() + self.maxheap.get_max()) / 2.0 elif self.minheap.size > self.maxheap.size: return self.minheap.get_min() else: return self.maxheap.get_max() def insert(self, value): if self.minheap.size == 0: self.minheap.insert(value) elif value > self.minheap.get_min(): self.minheap.insert(value) else: self.maxheap.insert(value) self.rebalance() def rebalance(self): if self.minheap.size < self.maxheap.size - 1: self.minheap.insert(self.maxheap.delete_max()) elif self.maxheap.size < self.minheap.size - 1: self.maxheap.insert(self.minheap.delete_min())
class MedianHeap(object): def __init__(self): self.minheap = MinHeap() self.maxheap = MaxHeap() def get_median(self): if self.minheap.size == self.maxheap.size: return (self.minheap.get_min() + self.maxheap.get_max()) / 2.0 elif self.minheap.size > self.maxheap.size: return self.minheap.get_min() else: return self.maxheap.get_max() def insert(self, value): if self.minheap.size == 0: self.minheap.insert(value) elif value > self.minheap.get_min(): self.minheap.insert(value) else: self.maxheap.insert(value) self.rebalance() def rebalance(self): if self.minheap.size < self.maxheap.size-1: self.minheap.insert(self.maxheap.delete_max()) elif self.maxheap.size < self.minheap.size-1: self.maxheap.insert(self.minheap.delete_min())
def heap_sort(numbers): print("Initial Array") print(MaxHeap(numbers)) # Heapify numbers list i = len(numbers) // 2 - 1 while i >= 0: max_heap_percolate_down(i, numbers, len(numbers)) i = i - 1 print("After Heapify") print(MaxHeap(numbers)) i = len(numbers) - 1 while i > 0: # Swap numbers[0] and numbers[i] temp = numbers[0] numbers[0] = numbers[i] numbers[i] = temp print("After Swap") print(MaxHeap(numbers)) max_heap_percolate_down(0, numbers, i) print("After Filter") print(MaxHeap(numbers)) i = i - 1
def heapSortMax(nums: List[int]) -> List[int]: if(not nums): return [] heapUtil = MaxHeap() for num in nums: heapUtil.heappush(num) returnList = [] while(heapUtil.size() > 0): returnList.append(heapUtil.heappop()) return returnList
def greedKnapsackMaxheap(): basicOp2B = 0 global sackValues greedNodes = sackValues.copy() answer = [] mH = MaxHeap(greedNodes, len(greedNodes)) mH.heapification() currCap = 0 knapVal = 0 for i in range(1, len(greedNodes)): basicOp2B += 1 if currCap + mH.H[1].weight < capacity[0]: answer.append(mH.H[1].itemNum) currCap += mH.H[1].weight knapVal += mH.H[1].value mH.deleteMax() answer.sort() mH.basicOperation += basicOp2B return answer, knapVal, mH.basicOperation + basicOp2B
def __init__(self): self.minheap = MinHeap() self.maxheap = MaxHeap()
import text_tokenizer import histogram import hashtable import Heap from text_tokenizer import * from hashtable import * from histogram import * from Heap import MaxHeap # If we subtract the Unix dictionary's keys from our word count dict's keys, # we can find specialized/jargon words in our corpus or words # we didn't normalize. We can even use this to refine our regular expressions # and normalization techniques! if __name__ == '__main__': user_input = "humannature.txt" tokenized_text = text_Parser(user_input) # TODO are we supposed to rewrite the histogram function to incorporate our hash table? histogram = histogram(tokenized_text) max_heap = MaxHeap() for i in histogram: max_heap.insert((i, histogram[i])) print(max_heap.peek(10))
from Heap import MaxHeap import Heapsort # Program to test the heap class. h = MaxHeap() input_list = [25, 44, 55, 99, 30, 37, 15, 10, 2, 4] for item in input_list: h.insert(item) print(' --> array: %s\n' % h.heap_array) print(h) h = MaxHeap(input_list) print(h) numbers = [82, 36, 49, 82, 34, 75, 18, 9, 23] #numbers = [6, 5, 4, 3, 2, 1] print("UNSORTED:", numbers) Heapsort.heap_sort(numbers) print("SORTED: ", numbers)
from dotenv import load_dotenv import os # If we subtract the Unix dictionary's keys from our word count dict's keys, # we can find specialized/jargon words in our corpus or words # we didn't normalize. We can even use this to refine our regular expressions # and normalization techniques! if __name__ == '__main__': dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path, verbose=True) api = twitter.Api(consumer_key=os.environ.get('CONSUMER_KEY'), consumer_secret=os.environ.get('CONSUMER_SECRET'), access_token_key=os.environ.get('ACCESS_TOKEN'), access_token_secret=os.environ.get('ACCESS_TOKEN_SECRET')) user_input = "obama_tweets.txt" tokenized_text = text_Parser(user_input) # TODO are we supposed to rewrite the histogram function to incorporate our hash table? histogram = histogram(tokenized_text) max_heap = MaxHeap() for i in histogram: max_heap.insert((i, histogram[i])) words = "obama_tweets.txt" tokenized_text = text_Parser(words) sentence_generator = SentenceGenerator(10) List = sentence_generator.build_dict(tokenized_text) sentence = sentence_generator.generate_sentences(List) # status = api.PostUpdate(sentence) # print(status.text)
from Heap import MaxHeap heap = MaxHeap(10) heap.insert(20) heap.insert(10) heap.insert(1) heap.insert(0) heap.insert(50) heap.printHeap() print('\n') print(heap.getMax()) print('\n') heap.printHeap()
output_file_1 = root_dir + sys.argv[2] # WRITE STREAMING MEDIAN TO "ft2.txt" # SET OUTPUT FILE PATH #output_file_2 = root_dir + '/tweet_output/ft2_v2.txt' output_file_2 = root_dir + sys.argv[3] ##################################################### # MAIN PROGRAM # ##################################################### # hashtable for counting words frequency words_dict = collections.Counter() # number of tweets processed num_tweets = 0 # max/min heap for calculating streaming median max_heap = MaxHeap() min_heap = MinHeap() with open(input_file, 'rb') as ff: # let's read in one tweet at a time for line in ff: # parse this tweet into separate words tmp_words = line.rstrip('\n').split(' ') # calculate total number of times each word tweeted for word in tmp_words: if word != '': words_dict[word] += 1 # we have processed one tweet num_tweets += 1
y = 60 vel = 10 screenContent = [] toBeDrawn = {} fish = [] enemies = [] fishHitboxes = {} numbers = [] timerTick = 0 scoreQueue = Queue(10) #scoreQueue.enqueue #scoreQueue.dequeue() Heap = MaxHeap() #while len(Heap.heap_list) != 16: #Heap.add(random.randint(1, 10)) #Heap.print_heap() class Image(pygame.sprite.Sprite): def __init__(self, image_file, location): pygame.sprite.Sprite.__init__(self) #call Sprite initializer self.image = pygame.image.load(image_file) self.rect = self.image.get_rect() self.rect.left, self.rect.top = location class Rectangle:
def test(self): # test data ob = MaxHeap() ob.insert(10) ob.insert(5) ob.insert(6) ob.insert(3) ob.insert(8) ob.insert(20) self.assertEqual(ob.peek(), 20, msg="Max Element is not matched") ob.pop() ob.pop() self.assertEqual(ob.peek(), 8, msg="Max Element is not matched") ob.pop() self.assertEqual(ob.peek(), 6, msg="Max Element is not matched") ob.pop() ob.pop() ob.pop() self.assertEqual(ob.peek(), None, msg="Max Element is not matched")