class AnimalQueueSingle(object): def __init__(self): self.queue = LinkedList() def enqueue(self, animal): self.queue.append(animal) def dequeue_any(self): return self.queue._remove_first() def dequeue_dog(self): return self.queue.remove('dog') def dequeue_cat(self): return self.queue.remove('cat')
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_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 rkr_gst(mantok_t, mantok_p, minimum_match_length, initial_search_length): search_length = initial_search_length stop = False while not stop: # Parameter longest_maximal_match is size of largest maximal-matches found in this iteration. maximal_matches = LinkedList() longest_maximal_match = scanpattern(mantok_t, mantok_p, search_length, maximal_matches) # Very long string; don't mark tiles but try again with larger s. if longest_maximal_match > 2*search_length: search_length = longest_maximal_match else: # Create tiles from matches taken from list of queues. mark_strings(mantok_t, mantok_p, maximal_matches) if search_length > 2*minimum_match_length: search_length = search_length/2 elif search_length > minimum_match_length: search_length = minimum_match_length else: stop = True stop2 = 2 debug = 1
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_init_with_list(self): ll = LinkedList(['A', 'B', 'C']) assert ll.head.data == 'A' # first item assert ll.head.previous is None # The first item previous pointer shouldn't point to anything assert ll.head.next.data == 'B' assert ll.tail.data == 'C' # last item assert ll.tail.next is None assert ll.tail.previous.data == 'B' # The last item previous pointer should point to Node B assert ll.size == 3
class AnimalQueueDouble(object): def __init__(self): self.dogs = LinkedList() self.cats = LinkedList() self.order = 0 def enqueue(self, animal): if animal == 'dog': self.dogs.append((animal, self.order)) else: self.cats.append((animal, self.order)) self.order += 1 def dequeue_any(self): if self.dogs.first is None: return self.dequeue_cat() if self.cats.first is None: return self.dequeue_dog() cat = self.cats.first dog = self.dogs.first if cat.value[1] < dog.value[1]: return self.dequeue_cat() else: return self.dequeue_dog() def dequeue_dog(self): return self.dogs._remove_first() def dequeue_cat(self): return self.cats._remove_first()
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.tail.previous.data == 'B' 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 __init__(self): self.dogs = LinkedList() self.cats = LinkedList() self.order = 0
def setUp(self): self.l = LinkedList()
class TestLinkedList(unittest.TestCase): def setUp(self): self.l = LinkedList() def test_len(self): self.assertEqual(len(self.l), 0) def test_append_to_empty_list(self): self.l.append(0) self.assertEqual(self.l.first.value, 0) self.assertEqual(self.l.last.value, 0) self.assertEqual(len(self.l), 1) def test_append_to_none_empty_list(self): self.l.append(0) self.l.append(1) self.assertEqual(self.l.first.value, 0) self.assertEqual(self.l.first.next.value, 1) self.assertEqual(self.l.last.prev.value, 0) self.assertEqual(len(self.l), 2) def test_str_of_empty_list(self): self.assertEqual(str(self.l), "Linked list: []") def test_str_of_one_node_list(self): self.l.append(0) self.assertEqual(str(self.l), "Linked list: [0]") def test_str_of_multiple_nodes_list(self): self.l.append(0) self.l.append(1) self.assertEqual(str(self.l), "Linked list: [0<->1]") def test_remove_from_empty_list(self): x = self.l.remove(0) self.assertEqual(x, None) self.assertEqual(str(self.l), "Linked list: []") def test_remove_from_one_node_list(self): self.l.append(0) x = self.l.remove(0) self.assertEqual(x.value, 0) self.assertEqual(str(self.l), "Linked list: []") def test_remove_from_two_nodes_list(self): self.l.append(0) self.l.append(1) x = self.l.remove(1) self.assertEqual(x.value, 1) self.assertEqual(str(self.l), "Linked list: [0]") def test_remove_first_from_multiple_nodes_list(self): for v in range(3): self.l.append(v) x = self.l.remove(0) self.assertEqual(x.value, 0) self.assertEqual(str(self.l), "Linked list: [1<->2]") def test_remove_middle_from_multiple_nodes_list(self): for v in range(3): self.l.append(v) x = self.l.remove(1) self.assertEqual(x.value, 1) self.assertEqual(str(self.l), "Linked list: [0<->2]") def test_remove_last_from_multiple_nodes_list(self): for v in range(2): self.l.append(v) self.l.append(2) x = self.l.remove(2) self.assertEqual(x.value, 2) self.assertEqual(str(self.l), "Linked list: [0<->1]") def test_remove_none_existing_node(self): for v in range(3): self.l.append(v) x = self.l.remove(4) self.assertEqual(x, None) self.assertEqual(str(self.l), "Linked list: [0<->1<->2]") def test_reverse_empty_list(self): self.l.reverse() self.assertEqual(str(self.l), "Linked list: []") def test_reverse_one_node_list(self): self.l.append(0) self.l.reverse() self.assertEqual(str(self.l), "Linked list: [0]") def test_reverse_multiple_nodes_list(self): self.l.append(0) self.l.append(1) self.l.append(2) self.l.reverse() self.assertEqual(str(self.l), "Linked list: [2<->1<->0]") def test_reverse_iterative(self): self.l.append(0) self.l.append(1) self.l.append(2) self.l.reverse() self.assertEqual(str(self.l), "Linked list: [2<->1<->0]")
def __init__(self): self.queue = LinkedList()
class LRU: def __init__(self, capacity): self.capacity = capacity self.cache = {} self.queue = LinkedList() def insert(self, key, value): if key in self.cache: node = self.cache[key] node.data = (key, value) self.queue.removeNode(node) self.queue.insert_node(node, 0) else: new_node = Node((key, value)) evicted_value = None if len(self.cache) >= self.capacity: lru_node = self.queue.tail lru_key, evicted_value = lru_node.data del self.cache[lru_key] self.queue.removeNode(lru_node) self.cache[key] = new_node self.queue.insert_node(new_node, 0) return evicted_value def get(self, key): if key not in self.cache: return None node = self.cache[key] self.queue.removeNode(node) self.queue.insert_node(node, 0) _, value = node.data return value
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_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 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_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_init(self): ll = LinkedList() assert ll.head is None assert ll.tail is None assert ll.size == 0
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.head.next is None # previous pointer (at index 0) assert ll.head.previous is None # next pointer (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.head.next.data == 'B' # new head next pointer should be B assert ll.head.previous is None # new head previous pointer should still be None assert ll.tail.previous.data == 'A' # Tail previous pointer should point to Node B assert ll.tail.next is None # Tail next pointer should be None 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.tail.previous.data == 'B' # new tail previous pointer should point to B assert ll.size == 3 ll.insert_at_index(2, 'C') # insert 'C' between 'B' and 'D' print(ll) assert ll.head.data == 'A' # unchanged (at index 0) assert ll.tail.data == 'D' # unchanged (now at index 3) assert ll.tail.previous.data == 'C' # new tail previous pointer should point to C 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, capacity): self.capacity = capacity self.cache = {} self.queue = LinkedList()