def test_add():
    mammals = LinkedList()
    mammals.insert('mouse')
    mammals.insert('dog')
    mammals.add('cat')

    assert mammals.print() == 'dog; mouse; cat; '
Exemple #2
0
class Stack:
    def __init__(self):
        self.items = LinkedList()

    def push(self, value):
        """
        Adds the value to the back of the LinkedList
        :param value: the value to be added
        """
        self.items.add(value)

    def pop(self):
        """
        Removes the value at the tail
        :return: the value of the item removed
        """
        if self.items.tail is None:
            return None
        value = self.items.tail.value
        self.items.remove(value)
        return value

    def peek(self):
        """
        :returns: the value of the item at the tail
        """
        if not self.items.tail:
            return None
        return self.items.tail.value
Exemple #3
0
 def test_add_one_item(self):
     linked_list = LinkedList()
     linked_list.add(1)
     # assert that the item we inserted is at the head
     self.assertEqual(linked_list.head.value, 1)
     # assert that when only one item is in the list the head and the tail are the same
     self.assertEqual(linked_list.tail.value, 1)
def test_add_multiple():
    mammals = LinkedList()
    mammals.add('mouse')
    mammals.add('dog')
    mammals.add('cat')

    assert mammals.print() == 'mouse; dog; cat; '
Exemple #5
0
class Queue:
    def __init__(self):
        """
        Initialize the Queue with an empty LinkedList
        """
        self.items = LinkedList()

    def is_empty(self):
        """
        :returns: True if the LinkedList is empty anf False if it isn't
        """
        return self.items.head is None

    def enqueue(self, value):
        """
        Adds the value to the back of the LinkedList
        :param value: the value to be added
        """
        self.items.add(value)

    def dequeue(self):
        """
        Removes the value at the head
        :return: the value of the item at the head
        """
        value = self.items.head.value
        self.items.remove(value)
        return value

    def peek(self):
        """
        :returns: the value of the item at the head
        """
        return self.items.head.value
Exemple #6
0
def test_linked_list_expressesion():
    ll = LinkedList()
    ll.add('apples')
    ll.add('bananas')

    cap_fruits = [f.upper() for f in ll]

    assert cap_fruits == ['APPLES', 'BANANAS']
Exemple #7
0
def test_linked_list_conversion():
    ll = LinkedList()
    ll.add('apples')
    ll.add('bananas')

    items = list(ll)

    assert items == ['apples', 'bananas']
Exemple #8
0
def test_linked_list_iteration():
    ll = LinkedList()
    ll.add('apples')
    ll.add('bananas')

    items = []
    for item in ll:
        items.append(item)

    assert items == ['apples', 'bananas']
def test_find_from_k():
    mammals = LinkedList()
    mammals.add('orca')
    mammals.add('beluga')
    mammals.add('porpoise')
    mammals.add('otter')
    mammals.add('seal')

    assert mammals.find_from_end(2) == 'porpoise'
    assert mammals.find_from_end(4) == 'orca'
    assert mammals.find_from_end(5) is None
Exemple #10
0
class Stack:
    def __init__(self):
        self.items = LinkedList()

    def push(self, value):
        self.items.add(value)

    def pop(self):
        if self.items.tail is None:
            return None
        value = self.items.tail.value
        self.items.remove(value)
        return value

    def peek(self):
        return self.items.tail.value
def test_ll_zip_longer():
    ll_1 = LinkedList()
    ll_1.add('A')
    ll_1.add('B')
    ll_1.add('C')
    ll_1.add('D')
    ll_1.add('E')

    ll_2 = LinkedList()
    ll_2.add('1')
    ll_2.add('2')
    ll_2.add('3')
    ll_2.add('4')

    head = ll_zip(ll_1, ll_2)
    assert head.value == 'A'
    assert ll_1.print() == 'A; 1; B; 2; C; 3; D; 4; E; '
Exemple #12
0
class CircularQueue:
    """
    The CircularQueue uses a LinkedList internally to manage the items
    """
    def __init__(self):
        """
        Initialize the Queue with an empty LinkedList
        """
        self.items = LinkedList()

    def is_empty(self):
        """
        :returns: True if the LinkedList is empty and False if it isn't
        """
        return self.items.head is None

    def enqueue(self, value):
        """
        Adds the value to the back of the LinkedList
        :param value: the value to be added
        """
        self.items.add(value)
        self.items.tail.next = self.items.head

    def dequeue(self):
        """
        Removes the value at the head
        :return: the value of the item at the head
        """
        current_node = self.items.head
        if not self.items.head:
            return None
        self.items.head = self.items.head.next
        return current_node.value

    def peek(self):
        """
        :returns: the value of the item at the head
        """
        return self.items.head.value
Exemple #13
0
def test_linked_list_filter():
    letters = LinkedList()
    letters.add('a')
    letters.add('b')
    letters.add('c')
    letters.add('d')
    letters.add('e')
    letters.add('f')
    letters.add('g')

    vowels = 'aeiou'

    consonants = [char for char in letters if char not in vowels]

    assert consonants == ['b', 'c', 'd', 'f', 'g']
def test_insert_after():
    mammals = LinkedList()
    mammals.add('whale')
    mammals.add('wolf')
    mammals.add('ape')
    mammals.insert_after('wolf', 'bat')

    assert mammals.print() == 'whale; wolf; bat; ape; '
Exemple #15
0
 def test_search_item_at_tail(self):
     linked_list = LinkedList()
     linked_list.add(2)
     linked_list.add(4)
     linked_list.add(25)
     linked_list.add(20)
     linked_list.add(12)
     self.assertEqual(linked_list.search(12), True)
Exemple #16
0
 def test_walk(self):
     linked_list = LinkedList()
     linked_list.add(2)
     linked_list.add(4)
     linked_list.add(25)
     linked_list.add(20)
     linked_list.add(12)
     self.assertEqual(linked_list.walk(), [2, 4, 25, 20, 12])
Exemple #17
0
 def test_remove_item_not_in_list(self):
     linked_list = LinkedList()
     linked_list.add(2)
     linked_list.add(4)
     linked_list.add(25)
     linked_list.add(20)
     linked_list.add(12)
     self.assertEqual(linked_list.remove(0), False)
     self.assertEqual(linked_list.walk(), [2, 4, 25, 20, 12])
Exemple #18
0
 def test_remove_from_tail(self):
     linked_list = LinkedList()
     linked_list.add(2)
     linked_list.add(4)
     linked_list.add(25)
     linked_list.add(20)
     linked_list.add(12)
     self.assertEqual(linked_list.remove(12), True)
     self.assertEqual(linked_list.walk(), [2, 4, 25, 20])
Exemple #19
0
 def test_remove_from_middle(self):
     linked_list = LinkedList()
     linked_list.add(2)
     linked_list.add(4)
     linked_list.add(25)
     linked_list.add(20)
     linked_list.add(12)
     self.assertEqual(linked_list.remove(25), True)
     self.assertEqual(linked_list.walk(), [2, 4, 20, 12])
Exemple #20
0
 def test_search_item_not_in_list(self):
     linked_list = LinkedList()
     linked_list.add(2)
     linked_list.add(4)
     linked_list.add(25)
     linked_list.add(52)
     linked_list.add(20)
     linked_list.add(12)
     self.assertEqual(linked_list.search(0), False)
def test_ll_reverse():
    ll = LinkedList()
    ll.add('A')
    ll.add('B')
    ll.add('C')
    ll.add('D')
    ll.add('E')
    ll_reverse(ll)
    assert ll.print() == 'E; D; C; B; A; '
Exemple #22
0
def test_linked_list_add_one():
    ll = LinkedList()
    ll.add('apples')
    assert ll.head.value == 'apples'
Exemple #23
0
 def test_prepend(self):
     linked_list = LinkedList()
     linked_list.add(1)
     linked_list.prepend(20)
     self.assertEqual(linked_list.head.value, 20)
     self.assertEqual(linked_list.tail.value, 1)
Exemple #24
0
 def test_add_two_items(self):
     linked_list = LinkedList()
     linked_list.add(1)
     linked_list.add(22)
     self.assertEqual(linked_list.head.next.value, 22)
     self.assertEqual(linked_list.head.next.value, linked_list.tail.value)
Exemple #25
0
def test_linked_list_add_two():
    ll = LinkedList()
    ll.add('apples')
    ll.add('bananas')
    assert ll.head.value == 'apples'
    assert ll.head.nxt.value == 'bananas'