Ejemplo n.º 1
0
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 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
Ejemplo n.º 3
0
 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")
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
Ejemplo n.º 5
0
 def __init__(self):
     self.minheap = MinHeap()
     self.maxheap = MaxHeap()
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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()
Ejemplo n.º 9
0
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: