class LinkedListTests(unittest.TestCase): def setUp(self): self.list = LinkedList() def test_add_to_tail(self): self.list.add_to_tail(1) self.assertEqual(self.list.tail.value, 1) self.assertEqual(self.list.head.value, 1) self.list.add_to_tail(2) self.assertEqual(self.list.tail.value, 2) self.assertEqual(self.list.head.value, 1) def test_add_to_head(self): self.list.add_to_head(5) self.assertEqual(self.list.head.value, 5) self.list.add_to_head(10) self.assertEqual(self.list.head.value, 10) self.assertEqual(self.list.tail.value, 5) def test_remove_tail(self): self.list.add_to_tail(1) self.list.add_to_tail(2) self.list.add_to_tail(5) self.list.add_to_tail(10) self.list.remove_tail() self.assertEqual(self.list.tail.value, 5) self.list.remove_tail() self.assertEqual(self.list.tail.value, 2) def test_contains(self): self.list.add_to_tail(1) self.list.add_to_tail(2) self.list.add_to_tail(5) self.list.add_to_tail(10) self.assertTrue(self.list.contains(10)) self.assertTrue(self.list.contains(2)) self.assertFalse(self.list.contains(1000)) def test_remove_head(self): self.list.add_to_tail(10) self.list.add_to_tail(20) self.assertEqual(self.list.remove_head(), 10) self.assertFalse(self.list.contains(10)) self.assertEqual(self.list.remove_head(), 20) self.assertFalse(self.list.contains(20)) self.list.add_to_tail(10) self.assertEqual(self.list.remove_head(), 10) self.assertIsNone(self.list.head) self.assertIsNone(self.list.tail) self.assertIsNone(self.list.remove_head()) def test_get_max(self): self.assertIsNone(self.list.get_max()) self.list.add_to_tail(100) self.assertEqual(self.list.get_max(), 100) self.list.add_to_tail(55) self.assertEqual(self.list.get_max(), 100) self.list.add_to_tail(101) self.assertEqual(self.list.get_max(), 101)
def test_linked_list_convert_to_array(): list_ = LinkedList() nodes = create_nodes(6) for node in nodes: list_.add_to_head(node) array = list_.convert_to_array() assert nodes[::-1] == array
class Stack: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): # returns size attr for the declared stack return self.size def push(self, value): # adds value to the end of the stack self.storage.add_to_head(value) # update size when added self.size = self.size + 1 # does not need to return what is stored def pop(self): # what if there is no stack if not self.storage.head: return None # what if there is something in stack else: # stacks return elements that we're removed # saving it for later value = self.storage.head.get_value() # deleting last value from storage # has to remove whatever at the top most recently added of the stack self.storage.remove_from_head() # remove value amt from size self.size = self.size - 1 return value
def test_linked_list_remove_tail_single_node_list(self): ll = LinkedList() ll.add_to_head(7) ll.remove_tail() self.assertEqual(None, ll.head) self.assertEqual(None, ll.tail) self.assertEqual(0, ll.count)
class Stack: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size # O(n) when pushing to an array # However, we're using LinkedLists! # O(1) because of LL :D def push(self, value): # add an element to the front of our array self.size += 1 self.storage.add_to_head(value) # O(n) when popping from an array. # However, we're using LinkedLists! # O(1) because of LL :D def pop(self): # check if empty if self.size == 0: return None # remove the first element in storage self.size -= 1 node = self.storage.remove_head() return node
def compress_file(file_to_be_segmented, segment_length, output_location=None): """ A method to compress a file by segmenting the text then allocating an integer value to represent that segmented string then optimize the compression by creating a list of positions of each segment at a given time """ #Edge Case: we can't do anything if the segment_length doesn't make sense greater_than_zero_or_raise(segment_length) position_list = LinkedList() #doubly linkedlist of nodes that correspond to a segment for easy reordering segment = '' #to represent a section of text initial_legend = OrderedDict() #to store segment as a key with (corresponding LLNode reference, compression value) as value output_list = [] #the compressed text as a list of positions of compressed values from position_list compression_value = 0 #an integer to represent a segment counting from 0 up by when it was added current_letter_index = 1 #an interger to remember where in the text the current letter is for seeking back with open(file_to_be_segmented) as text: current_letter = text.read(1) while current_letter: segment += current_letter next_letter = text.read(1) text.seek(current_letter_index) current_letter_index += 1 #check if the segment is ready to be added if segment_ready_to_be_added(segment, segment_length, current_letter, next_letter): #if segment in dictionary: create an LLNode and add last position to output_list if segment in initial_legend: output_list.append(initial_legend[segment][0].position) position_list.move_to_head(initial_legend[segment][0]) segment = "" #if segment not in dictionary: find it in head and add position to output_list else: new_node = LLNode() position_list.add_to_head(new_node) output_list.append(compression_value) initial_legend[segment] = (new_node, compression_value) compression_value += 1 segment = "" current_letter = text.read(1) (output_list_as_string, output_legend) = format_printouts(output_list, initial_legend) if not output_location: print output_list_as_string print output_legend else: output_file = open(output_location, 'w+b') output_file.write(output_list_as_string + '\n') #print string object of output_list with spaces seperating numbers output_file.write(output_legend) #print string of dictionary values in order output_file.close()
def test_linked_list_iterator(): list_ = LinkedList() list_.add_to_head(create_nodes(count=1)) iterator = iter(list_) with pytest.raises(StopIteration): next(iterator) next(iterator)
def test_linked_list_add_to_head(self): ll = LinkedList() ll.add_to_head(3) self.assertEqual(3, ll.head.value) self.assertEqual(3, ll.tail.value) self.assertEqual(None, ll.head.next_node) self.assertEqual(None, ll.head.previous_node) self.assertEqual(1, ll.count)
def test_linked_list_find_all(): nodes = [Node(100), Node(200), Node(100), Node(300), Node(100)] list_ = LinkedList() for node in nodes: list_.add_to_head(node) expected = [node for node in nodes[::-1] if node.get_value() == 100] assert list_.find_all(value=100) == expected
def test_linked_list_clear(self): ll = LinkedList() ll.add_to_head(7) ll.add_to_head(5) ll.add_to_head(3) ll.clear() self.assertEqual(None, ll.head) self.assertEqual(None, ll.tail) self.assertEqual(0, ll.count)
def test_linked_list_insert_to_one_node(): list_ = LinkedList() node1, node2 = create_nodes(count=2) list_.add_to_head(node1) list_.insert(node1, node2) assert node1.get_next() == node2 assert list_.get_head() == node1 assert list_.get_tail() == node2
def test_enumerate(self): ll = LinkedList() ll.add_to_head(7) ll.add_to_head(5) ll.add_to_head(3) # 3 <-> 5 <-> 7 generator = ll.enumerate() self.assertEqual(3, next(generator).value) self.assertEqual(5, next(generator).value) self.assertEqual(7, next(generator).value)
def test_linked_list_remove_head_two_node_list(self): ll = LinkedList() ll.add_to_head(7) ll.add_to_head(5) ll.remove_head() # BEFORE: 5 <-> 7 # AFTER: 7 self.assertEqual(7, ll.head.value) self.assertEqual(7, ll.tail.value) self.assertEqual(1, ll.count)
def test_linked_list_add_to_head_two_nodes(self): ll = LinkedList() ll.add_to_head(5) ll.add_to_head(3) # 3 <-> 5 self.assertEqual(3, ll.head.value) self.assertEqual(5, ll.tail.value) self.assertEqual(5, ll.head.next_node.value) self.assertEqual(3, ll.tail.previous_node.value) self.assertEqual(2, ll.count)
def test_linked_list_remove_by_value_all(): nodes = [Node(100), Node(200), Node(100), Node(300), Node(100)] list_nodes = nodes[::-1] list_ = LinkedList() for node in nodes: list_.add_to_head(node) list_.remove(value=100, is_all=True) expected = [node for node in list_nodes if node.get_value() != 100] assert list_.convert_to_array() == expected assert len(list_) == 2
def test_linked_list_remove_tail_two_node_list(self): ll = LinkedList() ll.add_to_head(7) ll.add_to_head(5) ll.remove_tail() # BEFORE: 5 <-> 7 # AFTER: 5 self.assertEqual(5, ll.head.value) self.assertEqual(5, ll.tail.value) self.assertEqual(None, ll.head.next_node) self.assertEqual(None, ll.tail.previous_node) self.assertEqual(1, ll.count)
def test_linked_list_remove_tail_three_node_list(self): ll = LinkedList() ll.add_to_head(7) ll.add_to_head(5) ll.add_to_head(3) ll.remove_tail() # BEFORE: 3 <-> 5 <-> 7 # AFTER: 3 <-> 5 self.assertEqual(3, ll.head.value) self.assertEqual(5, ll.head.next_node.value) self.assertEqual(5, ll.tail.value) self.assertEqual(3, ll.tail.previous_node.value) self.assertEqual(2, ll.count)
def test_linked_list_remove_tail_clear_list(self): ll = LinkedList() ll.add_to_head(7) ll.add_to_head(5) ll.add_to_head(3) ll.remove_tail() ll.remove_tail() ll.remove_tail() # BEFORE: 3 <-> 5 <-> 7 # AFTER: self.assertEqual(None, ll.head) self.assertEqual(None, ll.tail) self.assertEqual(0, ll.count)
class Stack: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size def push(self, value): self.storage.add_to_head(value) self.size += 1 def pop(self): self.size -= 1 return self.storage.remove_head()
def test_linked_list_remove_by_value(): nodes = [Node(100), Node(200), Node(100), Node(300), Node(100)] list_nodes = nodes[::-1] list_ = LinkedList() for node in nodes: list_.add_to_head(node) list_.remove(value=100) del list_nodes[0] assert list_.convert_to_array() == list_nodes list_.remove(value=200) assert list_nodes[1].get_next() != list_nodes[2] del list_nodes[2] assert list_.convert_to_array() == list_nodes
class StackL: def __init__(self): self.size = 0 self.storage = LinkedList() def __len__(self): return self.size def push(self, value): self.storage.add_to_head(value) self.size += 1 def pop(self): if self.size == 0: return None self.size -= 1 return self.storage.remove_head() def peek(self): return self.storage.head.get_value()
def test_linked_list_add_to_head(): list_ = LinkedList() node1, node2, node3 = create_nodes(count=3) list_.add_to_head(node1) assert list_.get_head() == node1 assert list_.get_tail() == node1 list_.add_to_head(node2) assert list_.get_head() == node2 assert node2.next == node1 assert list_.get_tail() == node1 list_.add_to_head(node3) assert list_.get_head() == node3 assert node3.next == node2 assert list_.get_tail() == node1
def test_add_to_head(): ll = LinkedList() assert str(ll) == '' for i in range(5): ll.add_to_head(i) assert str(ll) == '4->3->2->1->0'
class LinkedListTests(unittest.TestCase): def setUp(self): self.linked_list = LinkedList() def test_create_empty_list(self): self.assertEqual(self.linked_list.head, None) self.assertEqual(self.linked_list.tail, None) def test_add_to_head(self): self.linked_list.add_to_head(5) self.assertEqual(self.linked_list.head.value, 5) self.assertEqual(self.linked_list.tail.value, 5) self.linked_list.add_to_head(8) self.assertEqual(self.linked_list.head.value, 8) self.assertEqual(self.linked_list.tail.value, 5) self.linked_list.add_to_head(1) self.assertEqual(self.linked_list.head.value, 1) self.assertEqual(self.linked_list.tail.value, 5) def test_add_to_tail(self): self.linked_list.add_to_tail(10) self.assertEqual(self.linked_list.tail.value, 10) self.assertEqual(self.linked_list.head.value, 10) self.linked_list.add_to_tail(45) self.assertEqual(self.linked_list.tail.value, 45) self.assertEqual(self.linked_list.head.value, 10) self.linked_list.add_to_tail(70) self.assertEqual(self.linked_list.tail.value, 70) self.assertEqual(self.linked_list.head.value, 10) def test_length(self): self.assertEqual(self.linked_list.length, 0) self.linked_list.add_to_head(3) self.linked_list.add_to_head(4) self.linked_list.add_to_head(5) self.assertEqual(self.linked_list.length, 3) def test_remove_from_head(self): self.linked_list.add_to_head(3) self.linked_list.add_to_head(4) self.assertEqual(self.linked_list.head.value, 4) self.linked_list.remove_from_head() self.assertEqual(self.linked_list.head.value, 3) self.linked_list.remove_from_head() self.assertEqual(self.linked_list.head, None)
def create_linked_list(node_count=2) -> LinkedList: list_ = LinkedList() for node in create_nodes(node_count): list_.add_to_head(node) return list_
def test_linked_list_remove_from_tail_one_node(): list_ = LinkedList() list_.add_to_head(create_nodes(1)) list_.remove_from_tail() assert list_.is_empty()