コード例 #1
0
class IHOP_LL(object):
    def __init__(self, iterable=None):
        self.list = LonkedList()
        if iterable is not None:
            for item in iterable:
                self.push(item)

    def __repr__(self):
        """Return a string representation of this stack."""
        return 'Stack({} items, top={})'.format(self.length(), self.peek())

    def length(self):  # O(1)
        return self.list.length()

    def is_empty(self):  # O(1)
        if self.length() > 0:
            return False
        return True

    def push(self, item):  # O(1)
        self.list.prepend(item)

    def peek(self):  # O(1)
        if self.is_empty():
            return
        return self.list.head.data

    def pop(self):  # O(1)
        if self.is_empty():
            raise ValueError
        data = self.list.head.data
        self.list.delete(self.list.head.data)
        return data
コード例 #2
0
 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
コード例 #3
0
 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
コード例 #4
0
 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
コード例 #5
0
 def test_prepend(self):
     ll = LinkedList()
     # Prepend should always update head node
     ll.prepend('C')
     assert ll.head.data == 'C'  # New head
     assert ll.tail.data == 'C'  # New head
     ll.prepend('B')
     assert ll.head.data == 'B'  # New head
     assert ll.tail.data == 'C'  # Unchanged
     ll.prepend('A')
     assert ll.head.data == 'A'  # New head
     assert ll.tail.data == 'C'  # Unchanged
コード例 #6
0
class LinkedQ:
    def __init__(self, iterable=None):
        self.list = LonkedList()

        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 __iter__(self):
        for item in self.list:
            yield item

    def is_empty(self):
        if self.length() > 0:
            return False
        return True

    def length(self):
        return self.list.length()

    def front(self):
        if self.length() > 0:
            return self.list.head.data
        return

    def enqueue(self, item):
        self.list.append(item)

    def dequeue(self):
        if self.is_empty():
            raise ValueError
        data = self.list.head.data
        self.list.delete(self.list.head.data)
        return data
コード例 #7
0
 def test_append(self):
     ll = LinkedList()
     # Append should always update tail node
     ll.append('A')
     assert ll.head.data == 'A'  # New head
     assert ll.tail.data == 'A'  # New tail
     ll.append('B')
     assert ll.head.data == 'A'  # Unchanged
     assert ll.tail.data == 'B'  # New tail
     ll.append('C')
     assert ll.head.data == 'A'  # Unchanged
     assert ll.tail.data == 'C'  # New tail
コード例 #8
0
 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.size == 2
     ll.prepend('A')
     assert ll.head.data == 'A'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 3
コード例 #9
0
    def test_prepend_append(self):
        l = LinkedList()
        
        l.prepend('Z')
        assert l.head.data == 'Z'
        assert l.tail.data == 'Z'  

        l.append('B')
        assert l.head.data == 'Z'  
        assert l.tail.data == 'B'  

        l.prepend('A')
        assert l.head.data == 'A' 
        assert l.tail.data == 'B' 
コード例 #10
0
 def test_length_after_prepend(self):
     ll = LinkedList()
     assert ll.length() == 0
     # Prepend should increase length
     ll.prepend('C')
     assert ll.length() == 1
     ll.prepend('B')
     assert ll.length() == 2
     ll.prepend('A')
     assert ll.length() == 3
コード例 #11
0
 def test_items_after_prepend(self):
     ll = LinkedList()
     assert ll.items() == []
     # Prepend should add new item to head of list
     ll.prepend('C')
     assert ll.items() == ['C']
     ll.prepend('B')
     assert ll.items() == ['B', 'C']
     ll.prepend('A')
     assert ll.items() == ['A', 'B', 'C']
コード例 #12
0
 def test_items_after_append(self):
     ll = LinkedList()
     assert ll.items() == []
     # Append should add new item to tail of list
     ll.append('A')
     assert ll.items() == ['A']
     ll.append('B')
     assert ll.items() == ['A', 'B']
     ll.append('C')
     assert ll.items() == ['A', 'B', 'C']
コード例 #13
0
 def test_delete_with_item_not_in_list(self):
     ll = LinkedList(['A', 'B', 'C'])
     # Delete should raise error if item not found
     with self.assertRaises(ValueError):
         ll.delete('X')  # Item not found in list
コード例 #14
0
 def test_delete_with_5_items(self):
     ll = LinkedList(['A', 'B', 'C', 'D', 'E'])
     assert ll.head.data == 'A'  # First item
     assert ll.tail.data == 'E'  # Last item
     ll.delete('A')
     assert ll.head.data == 'B'  # New head
     assert ll.tail.data == 'E'  # Unchanged
     ll.delete('E')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'D'  # New tail
     ll.delete('C')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'D'  # Unchanged
     ll.delete('D')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'B'  # New tail
     ll.delete('B')
     assert ll.head is None  # No head
     assert ll.tail is None  # No tail
コード例 #15
0
 def __init__(self, iterable=None):
     self.list = LonkedList()
     if iterable is not None:
         for item in iterable:
             self.push(item)
コード例 #16
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.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.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.size == 3
     ll.insert_at_index(2, 'C')  # insert 'C' between 'B' and 'D'
     assert ll.head.data == 'A'  # unchanged (at index 0)
     assert ll.tail.data == 'D'  # unchanged (now at index 3)
     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
コード例 #17
0
 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
コード例 #18
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
コード例 #19
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']
コード例 #20
0
 def test_length_after_append_and_prepend(self):
     ll = LinkedList()
     assert ll.length() == 0
     # Append and prepend should increase length
     ll.append('C')
     assert ll.length() == 1
     ll.prepend('B')
     assert ll.length() == 2
     ll.append('D')
     assert ll.length() == 3
     ll.prepend('A')
     assert ll.length() == 4
コード例 #21
0
 def test_find(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.find(lambda item: item == 'B') == 'B'  # Match equality
     assert ll.find(lambda item: item < 'B') == 'A'  # Match less than
     assert ll.find(lambda item: item > 'B') == 'C'  # Match greater than
     assert ll.find(lambda item: item == 'X') is None  # No matching item
コード例 #22
0
 def test_length_after_delete(self):
     ll = LinkedList(['A', 'B', 'C', 'D', 'E'])
     assert ll.length() == 5
     # Delete should decrease length
     ll.delete('A')
     assert ll.length() == 4
     ll.delete('E')
     assert ll.length() == 3
     ll.delete('C')
     assert ll.length() == 2
     ll.delete('D')
     assert ll.length() == 1
     ll.delete('B')
     assert ll.length() == 0
コード例 #23
0
 def test_delete_with_3_items(self):
     ll = LinkedList(['A', 'B', 'C'])
     assert ll.head.data == 'A'  # First item
     assert ll.tail.data == 'C'  # Last item
     ll.delete('A')
     assert ll.head.data == 'B'  # New head
     assert ll.tail.data == 'C'  # Unchanged
     ll.delete('C')
     assert ll.head.data == 'B'  # Unchanged
     assert ll.tail.data == 'B'  # New tail
     ll.delete('B')
     assert ll.head is None  # No head
     assert ll.tail is None  # No tail
     # Delete should raise error if item was already deleted
     with self.assertRaises(ValueError):
         ll.delete('A')  # Item no longer in list
     with self.assertRaises(ValueError):
         ll.delete('B')  # Item no longer in list
     with self.assertRaises(ValueError):
         ll.delete('C')  # Item no longer in list
コード例 #24
0
    def __init__(self, iterable=None):
        self.list = LonkedList()

        if iterable:
            for item in iterable:
                self.enqueue(item)
コード例 #25
0
 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