Exemplo n.º 1
0
 def __init__(self, iterable=None):
     """Initialize this queue and enqueue the given items, if any."""
     # Initialize a new linked list to store the items
     self.list = LinkedList()
     if iterable is not None:
         for item in iterable:
             self.enqueue(item)
 def test_items(self):
     ll = LinkedList()
     assert ll.items() == []
     ll.append('B')
     assert ll.items() == ['B']
     ll.prepend('A')
     assert ll.items() == ['A', 'B']
     ll.append('C')
     assert ll.items() == ['A', 'B', 'C']
 def test_get_at_index(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.get_at_index(0) == 'A'  # head item
     assert ll.get_at_index(1) == 'B'  # middle item
     assert ll.get_at_index(2) == 'C'  # tail item
     with self.assertRaises(ValueError):
         ll.get_at_index(3)  # index too high
     with self.assertRaises(ValueError):
         ll.get_at_index(-1)  # index too low
 def test_items_after_append(self):
     ll = LinkedList()
     assert ll.items() == []
     # Append should add new item to tail of list
     ll.append('A')
     assert ll.items() == ['A']
     ll.append('B')
     assert ll.items() == ['A', 'B']
     ll.append('C')
     assert ll.items() == ['A', 'B', 'C']
 def test_items_after_prepend(self):
     ll = LinkedList()
     assert ll.items() == []
     # Prepend should add new item to head of list
     ll.prepend('C')
     assert ll.items() == ['C']
     ll.prepend('B')
     assert ll.items() == ['B', 'C']
     ll.prepend('A')
     assert ll.items() == ['A', 'B', 'C']
 def test_length_after_prepend(self):
     ll = LinkedList()
     assert ll.length() == 0
     # Prepend should increase length
     ll.prepend('C')
     assert ll.length() == 1
     ll.prepend('B')
     assert ll.length() == 2
     ll.prepend('A')
     assert ll.length() == 3
 def test_append(self):
     ll = LinkedList()
     # Append should always update tail node
     ll.append('A')
     assert ll.head.data == 'A'  # New head
     assert ll.tail.data == 'A'  # New tail
     ll.append('B')
     assert ll.head.data == 'A'  # Unchanged
     assert ll.tail.data == 'B'  # New tail
     ll.append('C')
     assert ll.head.data == 'A'  # Unchanged
     assert ll.tail.data == 'C'  # New tail
 def test_prepend(self):
     ll = LinkedList()
     # Prepend should always update head node
     ll.prepend('C')
     assert ll.head.data == 'C'  # New head
     assert ll.tail.data == 'C'  # New head
     ll.prepend('B')
     assert ll.head.data == 'B'  # New head
     assert ll.tail.data == 'C'  # Unchanged
     ll.prepend('A')
     assert ll.head.data == 'A'  # New head
     assert ll.tail.data == 'C'  # Unchanged
 def test_length_after_append_and_prepend(self):
     ll = LinkedList()
     assert ll.length() == 0
     # Append and prepend should increase length
     ll.append('C')
     assert ll.length() == 1
     ll.prepend('B')
     assert ll.length() == 2
     ll.append('D')
     assert ll.length() == 3
     ll.prepend('A')
     assert ll.length() == 4
 def test_prepend(self):
     ll = LinkedList()
     ll.prepend('C')
     assert ll.head.data == 'C'  # new head
     assert ll.tail.data == 'C'  # new head
     assert ll.size == 1
     ll.prepend('B')
     assert ll.head.data == 'B'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 2
     ll.prepend('A')
     assert ll.head.data == 'A'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
 def test_length_after_delete(self):
     ll = LinkedList(['A', 'B', 'C', 'D', 'E'])
     assert ll.length() == 5
     # Delete should decrease length
     ll.delete('A')
     assert ll.length() == 4
     ll.delete('E')
     assert ll.length() == 3
     ll.delete('C')
     assert ll.length() == 2
     ll.delete('D')
     assert ll.length() == 1
     ll.delete('B')
     assert ll.length() == 0
    def test_prepend_append(self):
        l = LinkedList()
        
        l.prepend('Z')
        assert l.head.data == 'Z'
        assert l.tail.data == 'Z'  

        l.append('B')
        assert l.head.data == 'Z'  
        assert l.tail.data == 'B'  

        l.prepend('A')
        assert l.head.data == 'A' 
        assert l.tail.data == 'B' 
 def test_replace(self):
     ll = LinkedList(['A', 'B', 'C'])
     ll.replace('A', 'D')
     assert ll.head.data == 'D'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     ll.replace('B', 'E')
     assert ll.head.data == 'D'  # unchanged
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
     ll.replace('C', 'F')
     assert ll.head.data == 'D'  # unchanged
     assert ll.tail.data == 'F'  # new tail
     assert ll.size == 3
     with self.assertRaises(ValueError):
         ll.replace('X', 'Y')  # item not in list
 def test_delete(self):
     ll = LinkedList(['A', 'B', 'C'])
     ll.delete('A')
     assert ll.head.data == 'B'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 2
     ll.delete('C')
     assert ll.head.data == 'B'  # unchanged
     assert ll.tail.data == 'B'  # new tail
     assert ll.size == 1
     ll.delete('B')
     assert ll.head is None  # new head
     assert ll.tail is None  # new head
     assert ll.size == 0
     with self.assertRaises(ValueError):
         ll.delete('X')  # item not in list
 def test_length(self):
     ll = LinkedList()
     assert ll.length() == 0
     # append and prepend operations increase length
     ll.append('B')
     assert ll.length() == 1
     ll.prepend('A')
     assert ll.length() == 2
     ll.append('C')
     assert ll.length() == 3
     # delete operations decrease length
     ll.delete('B')
     assert ll.length() == 2
     ll.delete('C')
     assert ll.length() == 1
     ll.delete('A')
     assert ll.length() == 0
 def test_size(self):
     ll = LinkedList()
     assert ll.size == 0
     # append and prepend operations increment size
     ll.append('B')
     assert ll.size == 1
     ll.prepend('A')
     assert ll.size == 2
     ll.append('C')
     assert ll.size == 3
     # delete operations decrement size
     ll.delete('B')
     assert ll.size == 2
     ll.delete('C')
     assert ll.size == 1
     ll.delete('A')
     assert ll.size == 0
 def _resize(self, new_size=None):
     """Resize this hash table's buckets and rehash all key-value entries.
     Should be called automatically when load factor exceeds a threshold
     such as 0.75 after an insertion (when set is called with a new key).
     Best and worst case running time: O(n) runtime
     Best and worst case space usage: O(n), O(n) memory usage based on the
     new bucket abount"""
     # If unspecified, choose new size dynamically based on current size
     self.size = 0
     if new_size is None:
         new_size = len(self.buckets) * 2  # Double size
     # Option to reduce size if buckets are sparsely filled (low load factor)
     elif new_size is 0:
         new_size = len(self.buckets) / 2  # Half size
     setlist = self.items()
     self.buckets = [LinkedList() for i in range(new_size)]
     for item in setlist:
         self.set(item[0], item[1])
 def test_delete_with_5_items(self):
     ll = LinkedList(['A', 'B', 'C', 'D', 'E'])
     assert ll.head.data == 'A'  # First item
     assert ll.tail.data == 'E'  # Last item
     ll.delete('A')
     assert ll.head.data == 'B'  # New head
     assert ll.tail.data == 'E'  # Unchanged
     ll.delete('E')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'D'  # New tail
     ll.delete('C')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'D'  # Unchanged
     ll.delete('D')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'B'  # New tail
     ll.delete('B')
     assert ll.head is None  # No head
     assert ll.tail is None  # No tail
 def test_delete_with_3_items(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.head.data == 'A'  # First item
     assert ll.tail.data == 'C'  # Last item
     ll.delete('A')
     assert ll.head.data == 'B'  # New head
     assert ll.tail.data == 'C'  # Unchanged
     ll.delete('C')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'B'  # New tail
     ll.delete('B')
     assert ll.head is None  # No head
     assert ll.tail is None  # No tail
     # Delete should raise error if item was already deleted
     with self.assertRaises(ValueError):
         ll.delete('A')  # Item no longer in list
     with self.assertRaises(ValueError):
         ll.delete('B')  # Item no longer in list
     with self.assertRaises(ValueError):
         ll.delete('C')  # Item no longer in list
 def test_insert_at_index(self):
     ll = LinkedList()
     ll.insert_at_index(0, 'B')  # append('B')
     assert ll.head.data == 'B'  # new head (at index 0)
     assert ll.tail.data == 'B'  # new tail (at index 0)
     assert ll.size == 1
     ll.insert_at_index(0, 'A')  # prepend('A')
     assert ll.head.data == 'A'  # new head (at index 0)
     assert ll.tail.data == 'B'  # unchanged (now at index 1)
     assert ll.size == 2
     ll.insert_at_index(2, 'D')  # append('D')
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # new tail (now at index 2)
     assert ll.size == 3
     ll.insert_at_index(2, 'C')  # insert 'C' between 'B' and 'D'
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # unchanged (now at index 3)
     assert ll.size == 4
     with self.assertRaises(ValueError):
         ll.insert_at_index(5, 'X')  # index too high
     with self.assertRaises(ValueError):
         ll.insert_at_index(-1, 'Y')  # index too low
 def __init__(self, init_size=8):
     """Initialize this hash table with the given initial size."""
     self.buckets = [LinkedList() for i in range(init_size)]
     self.size = 0  # Number of key-value entries
 def test_find(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.find(lambda item: item == 'B') == 'B'
     assert ll.find(lambda item: item < 'B') == 'A'
     assert ll.find(lambda item: item > 'B') == 'C'
     assert ll.find(lambda item: item == 'X') is None
 def test_init_with_list(self):
     ll = LinkedList(['A', 'B', 'C'])
     # Initializer should append items in order
     assert ll.head.data == 'A'  # First item
     assert ll.tail.data == 'C'  # Last item
 def test_init(self):
     ll = LinkedList()
     # Initializer should add instance properties
     assert ll.head is None  # First node
     assert ll.tail is None  # Last node
 def test_delete_with_item_not_in_list(self):
     ll = LinkedList(['A', 'B', 'C'])
     # Delete should raise error if item not found
     with self.assertRaises(ValueError):
         ll.delete('X')  # Item not found in list
 def test_find(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.find(lambda item: item == 'B') == 'B'  # Match equality
     assert ll.find(lambda item: item < 'B') == 'A'  # Match less than
     assert ll.find(lambda item: item > 'B') == 'C'  # Match greater than
     assert ll.find(lambda item: item == 'X') is None  # No matching item
 def test_init(self):
     ll = LinkedList()
     assert ll.head is None
     assert ll.tail is None
     assert ll.size == 0
 def __init__(self, start_size=8, mode='Static'):
     self.item_count = 0
     self.mode = mode
     self.buckets = []
     for i in range(start_size):
         self.buckets.append(LinkedList())
 def test_init_with_list(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.head.data == 'A'  # first item
     assert ll.tail.data == 'C'  # last item
     assert ll.size == 3