Beispiel #1
0
 def test_next_error(self):
     myList = linked_list.Pair(
         4, linked_list.Pair(5, linked_list.Pair(6, None)))
     thisIT = linked_list.object_iterator(myList)
     linked_list.next(thisIT)
     linked_list.next(thisIT)
     with self.assertRaises(StopIteration):
         linked_list.next(thisIT)
Beispiel #2
0
 def test_build_sorted_leaves(self):
     self.assertEqual(
         build_sorted_leaves(self.huff_list),
         ll.Pair(
             Leaf(100, 1),
             ll.Pair(
                 Leaf(99, 2),
                 ll.Pair(Leaf(32, 3),
                         ll.Pair(Leaf(98, 3), ll.Pair(Leaf(97, 4)))))))
Beispiel #3
0
 def test_yield_iterator2(self):
     myList = linked_list.Pair(
         4, linked_list.Pair(5, linked_list.Pair(6, None)))
     y = linked_list.yield_iterator(myList)
     next(y)  #4
     next(y)  #5
     next(y)  #6
     with self.assertRaises(StopIteration):
         next(y)
Beispiel #4
0
def insert(ht, key, item):
    index = hash(key) % ht.size
    if ht.table[index] != None:
        if insert_helper(ht.table[index], key, item):
            pass
        else:
            ht.collisions += 1
            ht.items += 1
            ht.table[index] = linked_list.add(
                ht.table[index], linked_list.length(ht.table[index]),
                (key, item))
    else:
        ht.table[index] = linked_list.add(ht.table[index],
                                          linked_list.length(ht.table[index]),
                                          (key, item))
        ht.items += 1
    if ht.items / ht.size > 1.5:
        new_table = HashTable(ht.size * 2, ht.items, [None] * (ht.size * 2), 0)
        for item in ht.table:
            if item != None:
                if linked_list.length(item) > 1:
                    for item_index in range(linked_list.length(item)):
                        new_index = hash(linked_list.get(
                            item, item_index)[0]) % new_table.size
                        if new_table.table[new_index] != None:
                            new_table.collisions += 1
                            new_table.table[new_index] = linked_list.add(
                                new_table.table[new_index],
                                linked_list.length(new_table.table[new_index]),
                                linked_list.get(item, item_index))
                        else:
                            new_table.table[new_index] = linked_list.Pair(
                                linked_list.get(item, item_index), None)
                else:
                    new_index = hash(item.first[0]) % new_table.size
                    #                    if new_table.table[new_index] != None:
                    #                        if insert_helper(new_table.table[new_index], item.first[0], item.first[1]):
                    #                            pass
                    #                        else:
                    #                            collisions += 1
                    #                            new_table.table[new_index] = linked_list.Pair(item.first, None)
                    #                    else:
                    new_table.table[new_index] = linked_list.Pair(
                        item.first, None)
        ht = new_table
    return ht
Beispiel #5
0
 def test_IteratorObject_eq(self):
     list1 = linked_list.Pair(5, linked_list.Pair(6, None))
     myIterator = linked_list.ListIterator(list1)
     list2 = linked_list.Pair(5, linked_list.Pair(6, None))
     secondIterator = linked_list.ListIterator(list2)
     self.assertEqual(myIterator == secondIterator, True)
Beispiel #6
0
 def test_yield_iterator(self):
     myList = linked_list.Pair(
         4, linked_list.Pair(5, linked_list.Pair(6, None)))
     y = linked_list.yield_iterator(myList)
     self.assertEqual(next(y), 4)
Beispiel #7
0
 def test_next(self):
     myList = linked_list.Pair(
         4, linked_list.Pair(5, linked_list.Pair(6, None)))
     thisIT = linked_list.object_iterator(myList)
     self.assertEqual(linked_list.next(thisIT), 4)
Beispiel #8
0
 def test_ObjectIterator(self):
     myList = linked_list.Pair(5, linked_list.Pair(6, None))
     thisIT = linked_list.object_iterator(myList)
     compareIterator = linked_list.ListIterator(myList)
     self.assertEqual(thisIT == compareIterator, True)
Beispiel #9
0
 def test_convert_llist(self):
     self.assertEqual(convert_llist(array_list.List([10,0,0,1,2,3,0],7,7)),
                      linked_list.Pair(Leaf(3, 1), linked_list.Pair(Leaf(4, 2), linked_list.Pair(Leaf(5, 3), linked_list.Pair(Leaf(0, 10), None)))))