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 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")
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()