def test_append(self): dll = DoublyLinkedList() dll.append('M') assert dll.head.data == 'M' assert dll.tail.data == 'M' dll.append('E') assert dll.head.data == 'M' assert dll.tail.data == 'E'
class Deque: def __init__(self, items=None): """ initializes a Deque with items if any are given. If items are given they are populated into Deque with push_front method (loading in front by default) """ # initialize linked list for our deque to use self.list = DoublyLinkedList() # build are deque if items is not None: for item in items: self.push_front(item) def push_front(item): """ Takes in given item and prepends it to the front of the deque """ # use linked list prepend method self.list.prepend(item) def push_back(item): """ Takes an item as parameter and appends it to the back of the deque """ # uses linked list append method self.list.append(item) def pop_front(): """ Removes the item at front of deque and returns it """ # grab item to be popped/returned popped_item = self.list.head # remove from left side of list using linkedlist delete method # note: this is still constant b/c popped_item is first item in linkedlist self.list.delete(popped_item) return popped_item # returning item that was just deleted def pop_back(): """ Removes the item at the end of deque and returns its value """ # grab item to be removed (tail of linked list) popped_item = self.list.tail # remove item from right side # currently O(n) self.list.delete(popped_item) return popped_item # return value of deleted item
def test_find(self): dll = DoublyLinkedList() dll.append('A') dll.append('B') dll.append('C') assert dll.find(lambda item: item == 'B') == 'B' assert dll.find(lambda item: item < 'B') == 'A' assert dll.find(lambda item: item > 'B') == 'C' assert dll.find(lambda item: item == 'D') is None
def test_items(self): ll = DoublyLinkedList() 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']
class LinkedQueue(object): 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 = DoublyLinkedList() self.size = 0 if iterable is not None: for item in iterable: self.enqueue(item) def __repr__(self): """Return a string representation of this queue.""" return 'Queue({} items, front={})'.format(self.length(), self.front()) def is_empty(self): """Return True if this queue is empty, or False otherwise.""" # TODO: Check if empty return self.list.is_empty() def length(self): """Return the number of items in this queue.""" # TODO: Count number of items return self.size def enqueue(self, item): """Insert the given item at the back of this queue. Running Time and why O(1) every case, because we keep track of the tail at all times, and enqueue appends at the tail""" # TODO: Insert given item self.list.append(item) # set tail to the Node(item), constant time self.size += 1 def front(self): """Return the item at the front of this queue without removing it, or None if this queue is empty.""" # TODO: Return front item, if any if self.is_empty(): return None return self.list.head.data def dequeue(self): """Remove and return the item at the front of this queue, or raise ValueError if this queue is empty. Running Time and why O(1), because we keep track of the head at all times and dequeue removes at the head""" # TODO: Remove and return front item, if any if self.is_empty(): raise ValueError("Head is empty, can't dequeue") head = self.front() self.list.head = self.list.head.next self.size -= 1 return head
def test_length_after_append_and_prepend(self): dll = DoublyLinkedList() assert dll.length() == 0 # Append and prepend should increase length dll.append('C') assert dll.length() == 1 dll.prepend('B') assert dll.length() == 2 dll.append('D') assert dll.length() == 3 dll.prepend('A') assert dll.length() == 4
def test_delete(self): ll = DoublyLinkedList() ll.append('A') ll.append('B') ll.append('C') ll.delete('A') assert ll.head.data == 'B' assert ll.tail.data == 'C' ll.delete('C') assert ll.head.data == 'B' assert ll.tail.data == 'B' ll.delete('B') assert ll.head is None assert ll.tail is None with self.assertRaises(ValueError): ll.delete('D')
def test_size(self): ll = DoublyLinkedList() 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 josephus(n, m): L = DoublyLinkedList() for i in range(1, n + 1): L.append(chr(ord('A') + i - 1)) p = L.first() for i in range(n - 1): print(L, p.el) for j in range(m): p = p.next if p.el is None: p = L.first() q = p p = p.next if p.el is None: p = L.first() L.remove(q) return L.first().el
def test_length(self): ll = DoublyLinkedList() 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_delete(self): dll = DoublyLinkedList() dll.append('L') dll.append('K') dll.append('G') dll.delete('L') assert dll.head.data == 'K' assert dll.tail.data == 'G' assert dll.length() == 2 print(dll) dll.delete('K') assert dll.head.data == 'G' assert dll.tail.data == 'G' assert dll.length() == 1
class LinkedQueue(object): 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 = DoublyLinkedList() if iterable: for item in iterable: self.enqueue(item) def __repr__(self): """Return a string representation of this queue""" return 'Queue({} items, front={})'.format(self.length(), self.front()) def is_empty(self): """Return True if this queue is empty, or False otherwise""" # TODO: Check if empty return self.list.size is 0 def length(self): """Return the number of items in this queue""" # TODO: Count number of items return self.list.size def enqueue(self, item): """Insert the given item at the back of this queue""" # TODO: Insert given item self.list.append(item) def front(self): """Return the item at the front of this queue without removing it, or None if this queue is empty""" # TODO: Return front item, if any if self.is_empty(): return None return self.list.head.data def dequeue(self): """Remove and return the item at the front of this queue, or raise ValueError if this queue is empty""" # TODO: Remove and return front item, if any if self.is_empty(): raise ValueError('Queue is empty') item = self.list.head.data self.list.delete(item) return item
def DISABLE_test_delete(self): ll = DoublyLinkedList() ll.append('A') ll.append('B') ll.append('C') ll.delete('A') assert ll.head.data == 'B' assert ll.tail.data == 'C' assert ll.size == 2 ll.delete('C') assert ll.head.data == 'B' assert ll.tail.data == 'B' assert ll.size == 1 ll.delete('B') assert ll.head is None assert ll.tail is None assert ll.size == 0 with self.assertRaises(ValueError): ll.delete('D')
def test_delete(self): dll = DoublyLinkedList() dll.append('A') dll.append('B') dll.append('C') dll.delete('A') assert dll.head.data == 'B' assert dll.tail.data == 'C' assert dll.size == 2 dll.delete('C') assert dll.head.data == 'B' assert dll.tail.data == 'B' assert dll.size == 1 dll.delete('B') assert dll.head is None assert dll.tail is None assert dll.size == 0 with self.assertRaises(ValueError): dll.delete('D')
def test_length(self): ll = DoublyLinkedList() assert ll.length() == 0 ll.append('A') assert ll.length() == 1 ll.append('B') assert ll.length() == 2 ll.append('C') assert ll.length() == 3
def test_size(self): ll = DoublyLinkedList() assert ll.size == 0 ll.append('A') assert ll.size == 1 ll.append('B') assert ll.size == 2 ll.append('C') assert ll.size == 3
def test_length(self): dll = DoublyLinkedList() assert dll.length() == 0 dll.append('A') assert dll.length() == 1 dll.append('B') assert dll.length() == 2 dll.append('C') assert dll.length() == 3
def test_size(self): dll = DoublyLinkedList() assert dll.size == 0 dll.append('A') assert dll.size == 1 dll.append('B') assert dll.size == 2 dll.append('C') assert dll.size == 3
def test_as_list(self): ll = DoublyLinkedList() assert ll.as_list() == [] ll.append('A') assert ll.as_list() == ['A'] ll.append('B') assert ll.as_list() == ['A', 'B'] ll.append('C') assert ll.as_list() == ['A', 'B', 'C']
def test_items(self): dll = DoublyLinkedList() assert dll.items() == [] dll.append('A') assert dll.items() == ['A'] dll.append('B') assert dll.items() == ['A', 'B'] dll.append('C') assert dll.items() == ['A', 'B', 'C']
def test_items_after_append(self): dll = DoublyLinkedList() assert dll.items() == [] # Append should add new item to tail of list dll.append('A') assert dll.items() == ['A'] dll.append('B') assert dll.items() == ['A', 'B'] dll.append('C') assert dll.items() == ['A', 'B', 'C']
def test_append(self): ll = DoublyLinkedList() ll.append('A') assert ll.head.data == 'A' assert ll.tail.data == 'A' ll.append('B') assert ll.head.data == 'A' assert ll.tail.data == 'B' ll.append('C') assert ll.head.data == 'A' assert ll.tail.data == 'C'
def test_append(self): dll = DoublyLinkedList() # Append should always update tail node dll.append('A') assert dll.head.data == 'A' # New head assert dll.tail.data == 'A' # New tail dll.append('B') assert dll.head.data == 'A' # Unchanged assert dll.tail.data == 'B' # New tail dll.append('C') assert dll.head.data == 'A' # Unchanged assert dll.tail.data == 'C' # New tail
def test_append(self): ll = DoublyLinkedList() ll.append('A') assert ll.head.data == 'A' # new head assert ll.tail.data == 'A' # new tail assert ll.size == 1 ll.append('B') assert ll.head.data == 'A' # unchanged assert ll.tail.data == 'B' # new tail assert ll.size == 2 ll.append('C') assert ll.head.data == 'A' # unchanged assert ll.tail.data == 'C' # new tail assert ll.size == 3
def test_append(self): dll = DoublyLinkedList() dll.append('A') assert dll.head.data == 'A' assert dll.tail.data == 'A' assert dll.size == 1 dll.append('B') assert dll.head.data == 'A' assert dll.tail.data == 'B' assert dll.size == 2 dll.append('C') assert dll.head.data == 'A' assert dll.tail.data == 'C' assert dll.size == 3
def intersecao(): batata = 0 CIU = DoublyLinkedList() while batata < (CI1.__len__() + CI2.__len__()): if CI1.__getitem__(batata) == CI2.__getitem__(batata): CIU.append(CI1.__getitem__(batata)) CIU.size = CIU.size + 1 elif CI1.__getitem__(batata-1) == CI2.__getitem__(batata): CIU.append(CI1.__getitem__(batata)) CIU.size = CIU.size + 1 elif CI1.__getitem__(batata) == CI2.__getitem__(batata-1): CIU.append(CI2.__getitem__(batata)) CIU.size = CIU.size + 1 for i in range(CIU.__len__()): print("Valor número %d da união dos dois conjuntos: %d" % (i, CIU.__getitem__(i)))
def test_length(self): dll = DoublyLinkedList() assert dll.length() == 0 dll.append('L') dll.append('K') assert dll.length() == 2
class Deque(object): 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 = DoublyLinkedList() if iterable is not None: for item in iterable: self.push_back(item) def __repr__(self): """Return a string representation of this queue.""" return 'Queue({} items, front={})'.format(self.length(), self.front()) def is_empty(self): """Return True if this queue is empty, or False otherwise.""" return self.list.head is None and self.list.tail is None def length(self): """Return the number of items in this queue.""" return self.list.size def push_front(self, item): """Insert the given item at the front of this queue. Running time: O(1)""" self.list.prepend(item) def push_back(self, item): """Insert the given item at the back of this queue. Running time: O(1) – adding a new node with a linked list takes O(1) time""" self.list.append(item) def front(self): """Return the item at the front of this queue without removing it, or None if this queue is empty.""" if self.is_empty(): return None return self.list.head.data def back(self): """Return the item at the back of this queue without removing it, or None if this queue is empty.""" if self.is_empty(): return None return self.list.tail.data def pop_front(self): """Remove and return the item at the front of this queue, or raise ValueError if this queue is empty. Running time: O(1) – removing an item from the front of a linked list takes O(1)""" if self.is_empty(): raise ValueError('Queue us empty') data = self.list.head.data self.list.head = self.list.head.next self.list.size -= 1 if self.list.size == 0: self.list.tail = None self.list.head = None return data def pop_back(self): """Remove and return the item at the back of this queue, or raise ValueError if this queue is empty. Running time: O(1)""" if self.is_empty(): raise ValueError('Queue is empty') data = self.list.tail.data self.list.tail = self.list.tail.prev self.list.size -= 1 if self.list.size == 0: self.list.head = None self.list.tail = None return data
def make(*els): a = DoublyLinkedList() for e in els: a.append(e) return a
class LinkedDeque(object): def __init__(self, iterable=None): self.list = DoublyLinkedList() if iterable is not None: for item in iterable: self.push_back(item) def __repr__(self): """Return a string representation of this deque.""" return 'Deque({} items, front={}, back={})'.format( self.length(), self.front(), self.back()) def is_empty(self): """Return True if this deque is empty, or False otherwise.""" return self.list.is_empty() def length(self): """Return the number of items in this deque.""" return self.list.size def push_front(self, item): """Insert the given item at the front of this deque. Running time: O(1) - add item front of the DoublyLinkedList""" self.list.prepend(item) def push_back(self, item): """Insert the given item at the back of this deque. Running time: O(1) - add item back of the DoublyLinkedList""" self.list.append(item) def front(self): """Return the item at the front of this deque without removing it, or None if this deque is empty.""" if self.is_empty(): return None else: return self.list.head.data def back(self): """Return the item at the back of this deque without removing it, or None if this deque is empty.""" if self.is_empty(): return None else: return self.list.tail.data def pop_front(self): """remove and return the item at the front of the deque. Running time: O(1) - delete item front of the DoublyLinkedList""" if self.is_empty(): raise ValueError('This deque is empty.') else: data = self.front() self.list.head = self.list.head.next if self.list.head is None: self.list.tail = None else: self.list.head.previous = None self.list.size -= 1 return data def pop_back(self): """remove and return the item at the back of the deque. Running time: O(1) - delete item back of the DoublyLinkedList""" if self.is_empty(): raise ValueError('This deque is empty.') else: data = self.back() self.list.tail = self.list.tail.previous if self.list.tail is None: self.list.head = None else: self.list.tail.next = None self.list.size -= 1 return data