class TextEdit: def __init__(self): self.text = DoublyLinkedList() self.switch = 0 def insert(self, c): if self.switch == 0: self.text.insert_at_start(c) self.switch = self.switch + 1 elif self.switch == self.text.__len__(): self.text.insert_at_end(c) self.switch = self.switch + 1 else: counter = 0 current_node = self.text.header while counter < self.switch: current_node = current_node.next counter = counter + 1 self.text.insert_between(c, current_node.prev, current_node) self.switch = self.switch + 1 def delete(self): counter = 0 current_node = self.text.header while counter != self.switch: current_node = current_node.next counter = counter + 1 if self.switch < self.text.__len__(): self.text.delete_between(current_node) if self.switch == 1: self.switch = self.switch - 1 def left(self): if self.switch != 0: self.switch = self.switch - 1 def right(self): self.switch = self.switch + 1 def print(self): current_node = self.text.header count = 0 space = "" while count < self.text.__len__(): if count != self.switch - 1: space = space + " " else: space = space + "^" print(current_node.element, end="") current_node = current_node.next count = count + 1 if (self.switch == 0): print("\n^") else: print("\n", space)
def test_doubly_linked_list_insert_head(self): doubly_linked_list = DoublyLinkedList() doubly_linked_list.insert_head(25) self.assertIsNotNone(doubly_linked_list) self.assertTrue(doubly_linked_list.__len__() == 1) doubly_linked_list.insert_head("Riccardo") self.assertTrue(doubly_linked_list.__len__() == 2) self.assertTrue(doubly_linked_list.head.item == "Riccardo") self.assertFalse(doubly_linked_list.head.next.item == "Riccardo") self.assertTrue(doubly_linked_list.head.next.item == 25)
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def enqueue(self, value): # should add an item to the back of the queue. self.storage.add_to_tail(value) # increment the size self.size += 1 def dequeue(self): # should remove and return an item from the front of the queue. if self.size == 0: return else: value = self.storage.remove_from_head() # decrease size self.size -= 1 return value def len(self): # returns the number of items in the queue. return self.storage.__len__()
class RingBuffer: def __init__(self, capacity): self.capacity = capacity self.storage = DoublyLinkedList() self.oldest = None def append(self, item): if self.storage.__len__() < self.capacity: self.storage.add_to_tail(item) self.oldest = self.storage.head else: the_oldest = self.oldest if the_oldest.next is not None: next_oldest = the_oldest.next next_oldest.insert_before(item) self.storage.delete(the_oldest) self.storage.length = self.capacity self.oldest = next_oldest else: self.storage.remove_from_tail() self.storage.add_to_tail(item) self.oldest = self.storage.head def get(self): # Note: This is the only [] allowed list_buffer_contents = [] # TODO: Your code here current_node = self.storage.head while current_node is not None: list_buffer_contents.append(current_node.value) current_node = current_node.next return list_buffer_contents
class Queue(object): """This is a queue.""" def __init__(self, iterable=None): """Initialize Queue object.""" self.dll = DoublyLinkedList() def enqueue(self, val): """Add val to head of queue.""" if isinstance(val, (tuple, list)): for val in val: self.enqueue(val) else: self.dll.push(val) def dequeue(self): """Remove val from tail of queue.""" try: return self.dll.shift() except IndexError: raise IndexError('Cannot shift empty queue.') def peek(self): """Look at next val to be dequeued without mod queue.""" try: return self.dll.tail.data except AttributeError: return None def __len__(self): """Return length of queue.""" return self.dll.__len__()
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def push(self, value): self.storage.add_to_head(value) # decrease size self.size += 1 def pop(self): if self.size == 0: return None else: value = self.storage.remove_from_head() # decrease size self.size -= 1 return value def len(self): return self.storage.__len__()
class Queue: def __init__(self): # dll = DoublyLinkedList() self.dll = DoublyLinkedList() self.size = 0 # Why is our DLL a good choice to store our elements? # self.storage = ? def enqueue(self, value): self.dll.add_to_tail(value) # print(15, value, self.len()) def dequeue(self): j = self.dll.remove_from_head() # print(19, j) return j def len(self): return self.dll.__len__() # f = Queue() # f.enqueue(4) # print(f.len()) # f.enqueue(6) # print(f.len())
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def enqueue(self, value): self.storage.add_to_head(value) def dequeue(self): if self.storage.__len__() is 0: return else: return self.storage.remove_from_tail() def len(self): return self.storage.__len__()
def test_doubly_linked_list_append(self): doubly_linked_list = DoublyLinkedList() doubly_linked_list.append(25) self.assertIsNotNone(doubly_linked_list) self.assertTrue(doubly_linked_list.__len__() == 1) doubly_linked_list.append("Riccardo") self.assertTrue(doubly_linked_list.__len__() == 2) self.assertTrue(doubly_linked_list.head.item == 25) self.assertTrue(doubly_linked_list.tail.item == "Riccardo") self.assertEqual(doubly_linked_list.tail.next, None) self.assertTrue(doubly_linked_list.head.next.item == "Riccardo") self.assertFalse(doubly_linked_list.head.next.item == "25") doubly_linked_list.append("Try") self.assertTrue(doubly_linked_list.head.item == 25) self.assertTrue(doubly_linked_list.head.next.item == "Riccardo") self.assertTrue(doubly_linked_list.tail.item == "Try") self.assertTrue(doubly_linked_list.head.next.item == "Riccardo") self.assertEqual(doubly_linked_list.tail.next, None)
def test_doubly_linked_list_delete(self): doubly_linked_list = DoublyLinkedList() doubly_linked_list.append(25) doubly_linked_list.append("Riccardo") doubly_linked_list.append("Try") doubly_linked_list.insert(6, 3) self.assertIsNotNone(doubly_linked_list) self.assertTrue(doubly_linked_list.__len__() == 4) self.assertTrue(doubly_linked_list.tail.item == 6) doubly_linked_list.delete(3) self.assertTrue(doubly_linked_list.__len__() == 3) self.assertTrue(doubly_linked_list.tail.item == "Try") self.assertFalse(doubly_linked_list.tail.item == 6) self.assertTrue(doubly_linked_list.head.item == 25) self.assertFalse(doubly_linked_list.head.next.item == "Try") self.assertTrue(doubly_linked_list.head.next.item == "Riccardo") doubly_linked_list.delete(0) self.assertTrue(doubly_linked_list.head.item == "Riccardo") self.assertTrue(doubly_linked_list.head.next.item == "Try")
class Queue: # FIFO def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def enqueue(self, value): self.storage.add_to_tail(value) self.size = self.storage.__len__() def dequeue(self): if not self.storage.head: return None value = self.storage.head.value self.storage.remove_from_head() self.size = self.storage.__len__() return value def len(self): return self.size
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def push( self, value ): # Adding an item onto the stack, goes in front...will be the last thing to come off the stack. self.storage.add_to_tail(value) def pop(self): # Removing an item off of the stack. if self.storage.__len__() is 0: return else: self.size -= 1 return self.storage.remove_from_tail() def len(self): # Getting length of items on the stack. return self.storage.__len__()
class Stack: # FILO def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # Same because we are adding and removing from the end self.storage = DoublyLinkedList() def push(self, value): self.storage.add_to_tail(value) self.size = self.storage.__len__() def pop(self): if not self.storage.head: return None value = self.storage.tail.value self.storage.remove_from_tail() self.size = self.storage.__len__() return value def len(self): return self.size
class LRUCache: """ Our LRUCache class keeps track of the max number of nodes it can hold, the current number of nodes it is holding, a doubly- linked list that holds the key-value entries in the correct order, as well as a storage dict that provides fast access to every node stored in the cache. """ def __init__(self, limit=10): self.cache = dict() self.limit = limit self.storage = DoublyLinkedList() """ Retrieves the value associated with the given key. Also needs to move the key-value pair to the end of the order such that the pair is considered most-recently used. Returns the value associated with the key or None if the key-value pair doesn't exist in the cache. """ def get(self, key): if key in self.cache: self.storage.move_to_front(self.cache[key]) return self.cache[key].value[key] else: return None """ Adds the given key-value pair to the cache. The newly- added pair should be considered the most-recently used entry in the cache. If the cache is already at max capacity before this entry is added, then the oldest entry in the cache needs to be removed to make room. Additionally, in the case that the key already exists in the cache, we simply want to overwrite the old value associated with the key with the newly-specified value. """ def set(self, key, value): if key in self.cache: self.cache[key].value[key] = value self.storage.move_to_front(self.cache[key]) else: if self.storage.__len__() < self.limit: self.storage.add_to_head({key: value}) self.cache[key] = self.storage.head else: take_key_out = list(self.storage.remove_from_tail().keys())[0] self.cache.pop(take_key_out) self.storage.add_to_head({key: value}) self.cache[key] = self.storage.head
class RingBuffer: def __init__(self, capacity): self.capacity = capacity self.current = None self.storage = DoublyLinkedList() def append(self, item): #current is our record of how many items we have in our RingBuffer. We have to set it to zero so we can start counting if self.current == None: self.current = 0 #temp variable current_head = self.storage.head #if there's nothing in the RingBuffer, if current_head == None: self.storage.add_to_head(item) self.current += 1 return #if there is stuff but we are not at capacity (filling buffer for the first time) elif self.storage.__len__() < self.capacity: self.storage.add_to_tail(item) self.current += 1 #storage is full else: #increment self.current by one self.current += 1 if self.current < self.capacity: print("WHEEEEE!!!") else: #reset to 0 if at capacity self.current = 0 count = 0 current = self.storage.head #increment through, looking for current spot in queue while current != None: #Overwrite values here if count == self.current: current.value = item return current = current.next count += 1 def get(self): # Note: This is the only [] allowed list_buffer_contents = [] dll_item = self.storage.head while dll_item != None: list_buffer_contents.append(dll_item.value) dll_item = dll_item.next return list_buffer_contents
class RingBuffer: def __init__(self, capacity): self.capacity = capacity self.current = None self.storage = DoublyLinkedList() def append(self, item): if self.current == None: self.current = 0 # this starts as None current_head = self.storage.head # if our head is None, move item to tail if current_head is None: # so the head/prev are now None, tail is item self.storage.add_to_tail(item) self.current += 1 return else: # loop through current # as item gets passed in after the 1st item, # this is basically adding everything behind item1 # and moving the head for i in range(self.current): # if next node/element is not None if current_head.next != None: # then set that next node/element to current_head current_head = current_head.next else: # if next node/element is None # put item at tail self.storage.add_to_tail(item) self.current += 1 if self.current == self.capacity: self.current = 0 return current_head.value = item self.current += 1 if self.current == self.capacity: self.current = 0 def get(self): #Note: This is the only [] allowed list_buffer_contents = [] temp = 0 while temp < self.storage.__len__(): # loop through storage current_head = self.storage.head # grab head list_buffer_contents.append(current_head.value) # append head value to list self.storage.move_to_end(current_head) # move head to tail temp += 1 return list_buffer_contents
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # self.storage = ? self.storage = DoublyLinkedList() # push adds new items to the end of the array def push(self, value): return self.storage.add_to_tail(value) # pop remvoes items to the end of the array def pop(self): # what if no items on stack if self.storage.__len__() == 0: return else: return self.storage.remove_from_tail() # length of list def len(self): return self.storage.__len__()
class Stack: def __init__(self): self.size = 0 self.storage = DoublyLinkedList() def push(self, value): self.storage.add_to_tail(value) def pop(self): return self.storage.remove_from_tail() def len(self): return self.storage.__len__()
class Queue: def __init__(self): self.size = 0 self.storage = DoublyLinkedList() def enqueue(self, value): self.storage.add_to_tail(value) def dequeue(self): return self.storage.remove_from_head() def len(self): return self.storage.__len__()
class Stack: def __init__(self): # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def push(self, value): self.storage.add_to_head(value) def pop(self): return self.storage.remove_from_head() def len(self): return self.storage.__len__()
class RingBuffer: def __init__(self, capacity): self.capacity = capacity self.current = None self.storage = DoublyLinkedList() def append(self, item): # if we're at capacity: if self.storage.__len__() < self.capacity: # keep adding to the end of the list self.storage.add_to_tail(item) self.current = self.storage.tail # if we hit capacity elif self.storage.__len__() == self.capacity: # if we're at the last thing in the list, we know the first thing is the oldest if self.current.next == None: # remove thing at head self.storage.remove_from_head() # the new head we just added is now the current, so we can go on self.storage.add_to_head(item) # always set current to whatever we just changed self.current = self.storage.head # else we're not at the end of the list else: # the next thing to delete is gonna come after whatever we just updated #delete it self.current.next.delete() # insert the new node after it (where the deleted one just was) self.current.insert_after(item) # and keep moving forward self.current = self.current.next def get(self): # Note: This is the only [] allowed list_buffer_contents = [item for item in self.storage.iter()] return list_buffer_contents
class RingBuffer: def __init__(self, capacity): self.capacity = capacity self.current_node = None self.storage = DoublyLinkedList() def append(self, item): if self.capacity > self.storage.__len__(): self.storage.add_to_head(item) else: self.storage.remove_from_tail() self.storage.add_to_head(item) # pseudocode # if the len of the storgae is less then the capacity of the linked list # then we're going to access storage and add item to tail # then we are going to asign the current node to be at the tail of the linked list def get(self): # Note: This is the only [] allowed list_buffer_contents = [] num = 0 while num < self.storage.__len__( ): # create a loop saying while num is less then the storage it will exicute the code below current_head = self.storage.head # created a varible and assigned it the head node of storage list_buffer_contents.append( current_head.value ) # adds new node to the list_buffer_contents list self.storage.move_to_end( current_head ) # moves the current head to the beack of the list num += 1 # grow by one return list_buffer_contents
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # self.storage = ? self.storage = DoublyLinkedList() # should add an itemt o the back of the queue def enqueue(self, value): return self.storage.add_to_tail(value) # should remove and return an item from the front of the queue def dequeue(self): # what if the queue is empty if self.storage.__len__() == 0: return # else remove it from the front else: return self.storage.remove_from_head() # returns the numver of items int he queue def len(self): # using the len method return self.storage.__len__()
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def push(self, item): self.storage.add_to_head(item) def pop(self): if self.size > 0: self.size -= 1 return self.storage.remove_from_head() def len(self): return self.storage.__len__()
class Queue: def __init__(self): self.size = 0 # We're already storing len in Doubly Linked List # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def enqueue(self, value): # store to head return self.storage.add_to_head(value) def dequeue(self): # remove from tail return self.storage.remove_from_tail() def len(self): # call len return self.storage.__len__()
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def push(self, value): # add_to_tail return self.storage.add_to_tail(value) def pop(self): # remove_from_tail return self.storage.remove_from_tail() def len(self): # call len return self.storage.__len__()
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # It can be traversed in both forward and backward direction, as well as the delete operation is more efficent if the pointer to the node to be deleted is given. # self.storage = ? self.storage = DoublyLinkedList() def enqueue(self, value): self.storage.add_to_tail(value) # pass def dequeue(self): return self.storage.remove_from_head() # pass def len(self): return self.storage.__len__()
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # time complexity is constant. self.storage = DoublyLinkedList() def push(self, value): self.size += 1 self.storage.add_to_head(value) def pop(self): if self.len() > 0: self.size -= 1 value = self.storage.head.value self.storage.remove_from_head() return value def len(self): return self.storage.__len__()
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # time complexity > * (it's constant) self.storage = DoublyLinkedList() def enqueue(self, value): self.size += 1 self.storage.add_to_head(value) def dequeue(self): if self.len() > 0: self.size -= 1 value = self.storage.tail.value self.storage.remove_from_tail() return value def len(self): return self.storage.__len__()
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? self.storage = DoublyLinkedList() def push(self, value): self.size += 1 self.storage.add_to_head(value) def pop(self): if not self.storage.head: return None self.size -= 1 value = self.storage.head.value self.storage.remove_from_head() return value def len(self): return self.storage.__len__()
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # self.storage = ? self.storage = DoublyLinkedList() def enqueue(self, value): self.size += 1 self.storage.add_to_tail(value) def dequeue(self): if self.size == 0: return None else: self.size -= 1 value = self.storage.remove_from_head() return value def len(self): return self.storage.__len__()
class Stack: def __init__(self): # self.size = 0 # Why is our DLL a good choice to store our elements? self.dll = DoublyLinkedList() def push(self, value): return self.dll.add_to_tail(value) def pop(self): return self.dll.remove_from_tail() def len(self): return self.dll.__len__() # f = Stack() # f.push(4) # print(f.len()) # f.push(6) # print(f.len()) # print(f.pop()) # print(f.len())
class Deque(object): """Deque data structure, can add and remove from both ends.""" def __init__(self): """Initialize the deque.""" self.deque = DoublyLinkedList() def append(self, value): """Add an item to the back of the deque.""" self.deque.append(value) def appendleft(self, value): """Add an item to the front of the deque.""" self.deque.push(value) def pop(self): """Pop a value off of the back of the deque and return it.""" return self.deque.shift() def popleft(self): """Pop a value off of the front of the deque and return it.""" return self.deque.pop() def peek(self): """Return the next value that would be poped at the end of deque.""" try: return self.deque.tail.data except AttributeError: return None def peekleft(self): """Return the next value in the deque. This is the value that popleft would remove.""" try: return self.deque.head.data except AttributeError: return None def size(self): """Return the number of nodes in the deque.""" return self.deque.__len__()
def test_doubly_linked_list_empty(self): doubly_linked_list = DoublyLinkedList() self.assertTrue(doubly_linked_list.__len__() == 0) self.assertEqual(doubly_linked_list.head, None)