示例#1
0
class IndexTests(AbstractHeapTest):
    def test_common(self):
        self.setHeapState([1, 2, 3])
        self.assertEqual(1, self.heap._index(2))

    def test_index_of_not_contained(self):
        self.setHeapState([1, 2, 3])
        with self.assertRaises(ValueError):
            self.heap._index(4)

    def test_index_of_with_empty_heap(self):
        self.setHeapState([])
        with self.assertRaises(ValueError):
            self.heap._index(4)

    def test_common_with_priority_function(self):
        self.heap = Heap(prior_func=lambda x: x[1])
        self.setHeapState([('a', 1), ('b', 2), ('c', 3)])
        self.assertEqual(1, self.heap._index(('b', 2)))

    def test_common_with_priority_function_and_not_contained(self):
        self.heap = Heap(prior_func=lambda x: x[1])
        self.setHeapState([('a', 1), ('b', 2), ('c', 3)])
        with self.assertRaises(ValueError):
            self.heap._index(('a', 2))
示例#2
0
class ExtendTests(AbstractHeapTest):
    def test_extend_in_empty_heap(self):
        self.setHeapState([])
        self.heap.extend([2, 1, 5, 4, 3])
        self.assertHeapState([1, 2, 5, 4, 3])

    def test_extend_with_empty_array(self):
        self.setHeapState([1, 2, 3])
        self.heap.extend([])
        self.assertHeapState([1, 2, 3])

    def test_extend_common(self):
        self.setHeapState([1, 2, 3])
        self.heap.extend([0, 2, 4])
        self.assertHeapState([0, 1, 3, 2, 2, 4])

    def test_with_random(self):
        for i in range(100):
            size = random.randint(0, 10)
            lst = [random.randint(0, 100) for _ in range(size)]
            self.heap.extend(lst)
            self.verify_heap()

    def test_with_random_and_priority_function(self):
        self.heap = Heap(prior_func=lambda x: x[1])
        for i in range(100):
            size = random.randint(0, 10)
            lst = [('a', random.randint(0, 100)) for _ in range(size)]
            self.heap.extend(lst)
            self.verify_heap()
示例#3
0
 def test_with_random_and_priority_function(self):
     self.heap = Heap(prior_func=lambda x: x[1])
     for i in range(100):
         size = random.randint(0, 10)
         lst = [('a', random.randint(0, 100)) for _ in range(size)]
         self.heap.extend(lst)
         self.verify_heap()
示例#4
0
class InsertTests(AbstractHeapTest):
    def test_insert_in_empty_heap(self):
        self.setHeapState([])
        self.heap.insert(1)
        self.assertHeapState([1])

    def test_insert_minimum_element(self):
        self.setHeapState([1, 2, 3, 4, 5, 5])
        self.heap.insert(0)
        self.assertHeapState([0, 2, 1, 4, 5, 5, 3])

    def test_insert_maximum_element(self):
        self.setHeapState([1, 2, 3, 4, 5, 5])
        self.heap.insert(10)
        self.assertHeapState([1, 2, 3, 4, 5, 5, 10])

    def test_insert_middle_element(self):
        self.setHeapState([1, 2, 3, 4, 5, 5])
        self.heap.insert(2)
        self.assertHeapState([1, 2, 2, 4, 5, 5, 3])

    def test_insert_with_random(self):
        for i in range(100):
            self.heap.insert(random.randint(0, 100))
            self.verify_heap()

    def test_insert_with_random_and_priority_function(self):
        self.heap = Heap(prior_func=lambda x: x[1])
        for i in range(100):
            self.heap.insert(('a', random.randint(0, 100)))
            self.verify_heap()
    def test_build(self):

        built = Heap()

        built.build([23, 12, 7, 5, 34, 88, 2])

        self.assertEqual([0, 2, 5, 7, 12, 34, 88, 23], built.heap_list)
    def build_basic_heap(self):
        heap = Heap()

        heap.insert(23)
        heap.insert(12)
        heap.insert(7)
        heap.insert(5)
        heap.insert(34)
        heap.insert(88)
        heap.insert(2)

        return heap
示例#7
0
def heap_sort(data_list):
    """
    Sort a list using heap sort (create a heap and sort by popping the min/max element)
    :param data_list: list of elements to sort
    :return: sorted list of elements from least to greatest
    """
    sorted_list = []
    heap = Heap(max_heap=False)
    for element in data_list:
        heap.add_element(element)
    while len(heap.data) > 0:
        sorted_list.append(heap.pop())
    return sorted_list
    def test_find_min(self):

        heap = self.build_basic_heap()

        self.assertEqual(2, heap.min())

        self.assertEqual(None, Heap().min())
示例#9
0
def find_tasks_metrics(n: int, tasks_durations: Sequence[int]) -> List[Metric]:
    """
    Возвращает массив, в котором для каждой задачи указан процессор и время,
    когда задача будет им выполнена.

    :param n: количество процессоров
    :param tasks_durations: массив с длительностями задач
    :return: массив из кортежей (a, b) для каждой задачи, где a - номер
        процессора (нумерация с нуля), b - время исполнения (отсчет начинается
        с нуля)
    """
    # В куче хранятся кортежи - время освобождения процессора и его номер
    p = Heap([(0, proc_num) for proc_num in range(n)])
    res = []
    for i in range(len(tasks_durations)):
        time, p_num = p.extract_min()
        res.append((p_num, time))
        p.insert((time + tasks_durations[i], p_num))
    return res
示例#10
0
class ShiftDownTests(AbstractHeapTest):
    def test_no_need_to_shift_down_leaf(self):
        self.setHeapState([1, 2, 3, 4])
        self.heap._shift_down(3)
        self.assertHeapState([1, 2, 3, 4])

    def test_no_need_to_shift_down_one_child(self):
        self.setHeapState([1, 2, 3, 4])
        self.heap._shift_down(1)
        self.assertHeapState([1, 2, 3, 4])

    def test_no_need_to_shift_down_two_children(self):
        self.setHeapState([1, 2, 3, 4, 5])
        self.heap._shift_down(1)
        self.assertHeapState([1, 2, 3, 4, 5])

    def test_no_need_to_shift_down_one_equal_child(self):
        self.setHeapState([1, 2, 3, 2, 4])
        self.heap._shift_down(1)
        self.assertHeapState([1, 2, 3, 2, 4])

    def test_no_need_to_shift_down_two_equal_children(self):
        self.setHeapState([1, 2, 3, 2, 2])
        self.heap._shift_down(1)
        self.assertHeapState([1, 2, 3, 2, 2])

    def test_shift_down_first_element(self):
        self.setHeapState([5, 1, 3, 4, 2])
        self.heap._shift_down(0)
        self.assertHeapState([1, 2, 3, 4, 5])

    def test_shift_down_middle_element(self):
        self.setHeapState([1, 5, 3, 7, 2, 3])
        self.heap._shift_down(1)
        self.assertHeapState([1, 2, 3, 7, 5, 3])

    def test_shift_down_with_priority_function(self):
        self.heap = Heap(prior_func=lambda x: x[1])
        self.setHeapState([('d', 1), ('c', 2), ('b', 3), ('a', 4)])
        self.heap._shift_down(1)
        self.assertHeapState([('d', 1), ('c', 2), ('b', 3), ('a', 4)])
示例#11
0
def custom_heap_sort(lst: list, sort='min'):
    copy_list = deepcopy(lst)
    if sort == 'max':
        copy_list = list(map(lambda x: x * -1, copy_list))
    heap = Heap()
    heap.build(copy_list)
    sorted_list = [heap.delete_min() for i in range(heap.size())]
    if sort == 'max':
        sorted_list = list(map(lambda x: x * -1, sorted_list))
    return sorted_list
    def test_delete_min(self):
        heap = self.build_basic_heap()

        self.assertEqual(2, heap.delete_min())

        self.assertEqual([0, 5, 7, 12, 23, 34, 88], heap.heap_list)

        heap = Heap()

        self.assertEqual(None, heap.delete_min())

        heap.insert(4)

        self.assertEqual(4, heap.delete_min())
示例#13
0
 def test_empty(self):
     self.heap = Heap(lst=[])
     self.verify_heap()
示例#14
0
def gen_heap(services: Services, *args, **kwargs) -> Heap:
    if prefer_tracked(services):
        return TrackedHeap(*args, **kwargs)
    else:
        return Heap(*args, **kwargs)
示例#15
0
 def test_shift_down_with_priority_function(self):
     self.heap = Heap(prior_func=lambda x: x[1])
     self.setHeapState([('d', 1), ('c', 2), ('b', 3), ('a', 4)])
     self.heap._shift_down(1)
     self.assertHeapState([('d', 1), ('c', 2), ('b', 3), ('a', 4)])
示例#16
0
 def __iter__(self):
     return Heap.__iter__(self)
示例#17
0
 def test_insert_with_random_and_priority_function(self):
     self.heap = Heap(prior_func=lambda x: x[1])
     for i in range(100):
         self.heap.insert(('a', random.randint(0, 100)))
         self.verify_heap()
示例#18
0
 def __len__(self):
     return Heap.__len__(self)
示例#19
0
 def __contains__(self, item):
     return Heap.__contains__(self, item)
示例#20
0
 def __init__(self) -> None:
     Heap.__init__(self)
     TrackedContainer.__init__(self)
示例#21
0
 def test_common_with_priority_function(self):
     self.heap = Heap(prior_func=lambda x: x[1])
     self.setHeapState([('a', 1), ('b', 2), ('c', 3)])
     self.assertEqual(1, self.heap._index(('b', 2)))
示例#22
0
 def setUp(self):
     self.heap = Heap()
示例#23
0
 def test_dynamic(self):
     for i in range(100):
         lst = [random.randint(0, 100) for _ in range(50)]
         self.heap = Heap(lst=lst)
         self.verify_heap()
示例#24
0
 def test_common_with_priority_function_and_not_contained(self):
     self.heap = Heap(prior_func=lambda x: x[1])
     self.setHeapState([('a', 1), ('b', 2), ('c', 3)])
     with self.assertRaises(ValueError):
         self.heap._index(('a', 2))