Exemple #1
0
    def test_merge_sorted(self):
        ll1 = from_array_list([1, 3, 5, 7, 9])
        ll2 = from_array_list([1, 4, 6, 8])

        head = merge_sorted(ll1.head, ll2.head)
        ll = LinkedList()
        ll.head = head

        self.assertEqual([1, 1, 3, 4, 5, 6, 7, 8, 9], ll.to_list())
Exemple #2
0
def simple_list(n):
    """
    Generates simple linked lists with numbers from 0 to n.

    Time complexity: O(n)
    Space complexity: O(n)
    """
    ll = LinkedList()
    for i in range(n - 1, -1, -1):
        ll.prepend(i)
    return ll
Exemple #3
0
class Stack:
    def __init__(self):
        self.ll = LinkedList()

    def push(self, data):
        self.ll.prepend(data)

    def pop(self):
        data = self.ll.head.value
        self.ll.delete(0)
        return data
    def test_insertsorted(self):
        ll = from_array_list([1, 2, 3, 5, 6, 7])
        insert_sorted(ll, 4)
        self.assertEqual([1, 2, 3, 4, 5, 6, 7], ll.to_list())

        ll = from_array_list([1, 2, 3])
        insert_sorted(ll, 4)
        self.assertEqual([1, 2, 3, 4], ll.to_list())

        ll = LinkedList()
        insert_sorted(ll, 1)
        self.assertEqual([1], ll.to_list())
    def test_get(self):
        ll = LinkedList()
        ll.append(1)
        ll.append(2)
        ll.append(3)

        self.assertEqual(1, ll.get(0).value)
        self.assertEqual(2, ll.get(1).value)
        self.assertEqual(3, ll.get(2).value)
        self.assertEqual(None, ll.get(3))
 def test_insert(self):
     ll = LinkedList()
     ll.insert(1, 0)
     self.assertEqual(1, ll.head.value)
     ll.insert(2, 1)
     self.assertEqual(2, ll.head.next.value)
     ll.insert(3, 2)
     self.assertEqual(3, ll.head.next.next.value)
Exemple #7
0
class Stack:
    """
    Stack implementation using linked list which
    supports max operation.

    Implemented using two linked lists. One for elements
    themselfs and one for current max value.

    Time complexity: O(1)
    Space complexity: O(N) + O(N) for max value.
    """
    def __init__(self):
        self._main_stack = LinkedList()
        self._max_stack = LinkedList()
        self._map = {}

    def peek(self):
        """
        Takes object from the top of this stack
        without removing it from the stack.
        """
        return self._main_stack.head.value

    def pop(self):
        """
        Removes object from the top of this stack
        and returns that object.
        """
        head = self._main_stack.head.value
        self._main_stack.delete(0)
        self._max_stack.delete(0)

        return head

    def push(self, item):
        """
        Pushes an item onto the top of this stack.
        """
        if not self._max_stack.head or self._max_stack.head.value < item:
            self._max_stack.prepend(item)
        else:
            self._max_stack.prepend(self._max_stack.head.value)

        self._main_stack.prepend(item)

    def max(self):
        return self._max_stack.head.value
Exemple #8
0
    def test_has_cycle_list(self):
        for method in self.methods:
            ll = LinkedList()
            self.assertFalse(method(ll.head), method.__name__)

            ll = simple_list(LIST_SIZE)
            self.assertFalse(method(ll.head), method.__name__)

            cll = cycle_list(LIST_SIZE)
            self.assertTrue(method(cll.head), method.__name__)

            rcll = random_cycle_list(LIST_SIZE)
            self.assertTrue(method(rcll.head), method.__name__)
 def test_constructor(self):
     ll = LinkedList()
     self.assertEqual(ll.head, None)
Exemple #10
0
 def __init__(self):
     self._main_stack = LinkedList()
     self._max_stack = LinkedList()
     self._map = {}
Exemple #11
0
    def test_find_loop_lengt(self):
        ll = cycle_list(LIST_SIZE)
        self.assertEqual(LIST_SIZE, find_cycle_length(ll.head))

        ll = LinkedList()
        self.assertEqual(0, find_cycle_length(ll.head))
Exemple #12
0
def breadth_first_traversal(tr_queue, level, build_str):
    """Print a graphical depiction of the tree using breadth first traversal."""
    if len(tr_queue) == 0:
        return

    root = tr_queue.popleft()
    build_str = build_str + "{0:4d}".format(root.data)

    if root.data == 2**level - 1:  # reached a new level of tree
        print_str = "{: ^50}\n".format(build_str)
        build_str = ""
        print(print_str)
        level = level + 1

    if root.left:
        tr_queue.append(root.left)
    if root.right:
        tr_queue.append(root.right)

    breadth_first_traversal(tr_queue, level, build_str)


if __name__ == "__main__":
    """Generate linked list, convert to binary tree."""
    l_list = build_linked_list(24, 1, LinkedList())
    bin_tree = build_tree_from_linked_list(l_list.head)

    queue = deque()
    queue.append(bin_tree.root)
    breadth_first_traversal(queue, 1, "")
Exemple #13
0
class LinkedListTest(unittest.TestCase):
    def setUp(self):
        self.ll = LinkedList()

    def tearDown(self):
        self.ll = None
        pass

    def test_insert(self):
        self.ll.insert(5)
        self.assertEqual(self.ll.toList(), [5])
        self.assertNotEqual(self.ll.toList(), [6])

        self.ll.insert(10)
        self.assertEqual(self.ll.toList(), [5, 10])

    def test_insert_after(self):
        self.ll.insert(5)
        self.ll.insert(10)

        self.ll.insert_after(6, 7)
        self.assertEqual(self.ll.toList(), [5, 7, 10])

        self.ll.insert_after(100, 1000)
        self.assertEqual(self.ll.toList(), [5, 7, 10, 1000])

    def test_delete(self):
        self.ll.insert(5)
        self.ll.delete(5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [10, 1000])

        self.ll.delete(1000)
        self.assertEqual(self.ll.toList(), [10])

        self.ll.delete(10)
        self.assertEqual(self.ll.toList(), [])

        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [])

    def test_delete_at(self):
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.push(10)
        self.ll.push(100)
        self.ll.push(1000)
        self.ll.delete_at(2)
        self.assertEqual(self.ll.toList(), [1000,100])

    def test_length(self):
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 1)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 2)

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(len(self.ll), 4)

    def test_contains(self):
        self.assertFalse(5 in self.ll)

        self.ll.insert(5)
        self.assertTrue(5 in self.ll)

        self.ll.push(0)
        self.assertTrue(0 in self.ll)
        self.assertFalse(3 in self.ll)


    def test_push(self):
        self.ll.push(5)
        self.assertEqual(self.ll.toList(), [5])

        self.ll.push(10)
        self.assertEqual(self.ll.toList(), [10, 5])

    def test_pop(self):
        self.ll.push(5)
        self.assertEqual(self.ll.pop(), 5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.push(1)
        self.ll.push(2)
        self.ll.push(3)
        self.ll.push(4)
        self.ll.push(100)
        self.assertEqual(self.ll.pop(), 100)
        self.assertEqual(self.ll.toList(), [4, 3, 2, 1])

    def test_swap_at(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.swap_at(1, 3)
        self.assertEqual(self.ll.toList(), [1, 4, 3, 2, 5])

        self.ll.swap_at(0, 4)
        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])

        self.ll.swap_at(0, 1)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 2, 1])

        self.ll.swap_at(4, 3)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 1, 2])

    def test_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.kth(0), 1)
        self.assertEqual(self.ll.kth(3), 4)

    def test_last_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.last_kth(1), 5)
        self.assertEqual(self.ll.last_kth(3), 3)

    def test_count(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(5)
        self.ll.push(1)

        self.assertEqual(self.ll.count(1), 1)
        self.assertEqual(self.ll.count(5), 2)

    def test_reverse(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.reverse()

        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])
Exemple #14
0
 def setUp(self):
     self.ll = LinkedList()
Exemple #15
0
 def __init__(self):
     self.ll = LinkedList()
class TestLinkedList(TestCase):
    def setUp(self):
        self.al = LinkedList()

    def test_append__when_list_is_empty__expect_append_to_the_end(self):
        self.al.append(1)
        values = list(self.al)

        self.assertListEqual([1], values)

    def test_append__expect_to_return_the_list(self):
        result = self.al.append(1)
        self.assertEqual(self.al, result)

    def test_append__when_list_not_empty__expect_append_to_the_end(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)
        values = list(self.al)

        self.assertListEqual([1, 2, 3], values)

    def test_append__1024_values__expect_append_to_the_end(self):
        values = [x for x in range(1024)]
        [self.al.append(x) for x in values]
        list_values = list(self.al)

        self.assertListEqual(values, list_values)

    def test_append__expect_to_increase_size(self):
        self.al.append(1)

        self.assertEqual(1, self.al.size())

    def test_remove__when_index_is_valid__expect_remove_values_and_return_it(
            self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(333)
        self.al.append(4)

        result = self.al.remove(2)
        self.assertListEqual([1, 2, 4], list(self.al))
        self.assertEqual(333, result)

    def test_remove__when_index_is_invalid__expect_to_raise(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)

        with self.assertRaises(IndexError):
            self.al.remove(self.al.size())

    def test_get__when_index_is_valid__expect_to_return_it(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(333)
        self.al.append(4)

        result = self.al.get(2)
        self.assertEqual(333, result)

    def test_get__when_index_is_invalid__expect_to_raise(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)

        with self.assertRaises(IndexError):
            self.al.get(self.al.size())

    def test_extend__with_empty_iterable__expect_to_be_same(self):
        self.al.append(1)

        self.al.extend([])
        self.assertListEqual([1], list(self.al))

    def test_extend__with_list__expect_to_append_the_list(self):
        self.al.append(1)

        self.al.extend([2])
        self.assertListEqual([1, 2], list(self.al))

    def test_extend__with_generator__expect_to_append_the_list(self):
        self.al.append(1)

        self.al.extend((x for x in range(1)))
        self.assertListEqual([1, 0], list(self.al))

    def test_extend__when_empty__expect_to_append_to_list(self):
        self.al.append(1)

        self.al.extend([1])
        self.assertListEqual([1, 1], list(self.al))

    def test_extend__with_not_iterable_expect_to_raise(self):
        self.al.append(1)

        with self.assertRaises(ValueError):
            self.al.extend(2)

    def test_insert__when_index_is_valid__expect_to_place_value_at_index(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(4)
        self.al.append(5)
        self.al.append(6)
        self.al.append(7)
        self.al.append(8)
        self.al.append(9)

        self.al.insert(2, 333)
        self.assertListEqual([1, 2, 333, 4, 5, 6, 7, 8, 9], list(self.al))

    def test_insert__when_index_is_invalid__expect_to_raise(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)

        with self.assertRaises(IndexError):
            self.al.insert(self.al.size() + 1, 2)

    def test_pop__expect_to_remove_the_last_value_and_return_it(self):
        self.al.append(1)
        self.al.append(2)
        self.al.append(3)
        self.al.append(4)

        result = self.al.pop()

        self.assertEqual(4, result)
        self.assertListEqual([1, 2, 3], list(self.al))

    def test_pop__when_empty__expect_to_raise(self):
        with self.assertRaises(IndexError):
            self.al.pop()

    def test_clear__expect_to_be_empty(self):
        [self.al.append(x) for x in range(15)]
        self.al.clear()
        self.assertEqual([], list(self.al))

    def test_index__when_item_is_present__expect_return_correct_index(self):
        [self.al.append(x) for x in range(15)]

        index = self.al.index(5)

        self.assertEqual(5, index)

    def test_index__when_item_is_not_present__expect_raise(self):
        [self.al.append(x) for x in range(15)]

        with self.assertRaises(ValueError):
            self.al.index(17)

    def test_count__when_item_is_present_one_time__expect_to_return_1(self):
        [self.al.append(x) for x in range(15)]

        expected_count = 1
        actual_count = self.al.count(5)
        self.assertEqual(expected_count, actual_count)

    def test_count__when_item_is_present_multiple_times__expect_to_return_correct_count(
            self):
        [self.al.append(x) for x in range(15)]
        self.al.append(5)
        self.al.insert(3, 5)
        self.al.insert(7, 5)
        self.al.insert(1, 5)
        self.al.insert(9, 5)

        expected_count = 6
        actual_count = self.al.count(5)
        self.assertEqual(expected_count, actual_count)

    def test_count__when_item_is_present_multiple_times_and_once_poped__expect_to_return_correct_count(
            self):
        [self.al.append(x) for x in range(15)]
        self.al.insert(3, 5)
        self.al.insert(7, 5)
        self.al.insert(1, 5)
        self.al.insert(9, 5)
        self.al.append(5)
        self.al.pop()

        expected_count = 5
        actual_count = self.al.count(5)
        self.assertEqual(expected_count, actual_count)

    def test_count__when_item_is_not_present__expect_to_return_0(self):
        [self.al.append(x) for x in range(15)]

        expected_count = 0
        actual_count = self.al.count(55)
        self.assertEqual(expected_count, actual_count)

    def test_copy__expect_to_return_another_list_with_same_values(self):
        [self.al.append(x) for x in range(5)]

        copied_list = self.al.copy()

        expected_result = [x for x in range(5)]
        actual_result = list(copied_list)

        self.assertNotEqual(copied_list, self.al)
        self.assertListEqual(expected_result, actual_result)

    def test_add_first__when_empty__expect_to_add(self):
        self.al.add_first(1)

        self.assertListEqual([1], list(self.al))

    def test_add_first__when_non_empty__expect_to_add(self):
        [self.al.append(x) for x in range(5)]

        self.al.add_first(1)

        self.assertListEqual([1, 0, 1, 2, 3, 4], list(self.al))
 def test_prepend(self):
     ll = LinkedList()
     ll.prepend(1)
     self.assertEqual([1], ll.to_list())
     ll.prepend(2)
     self.assertEqual([2, 1], ll.to_list())
     ll.prepend(3)
     self.assertEqual([3, 2, 1], ll.to_list())
 def test_append(self):
     ll = LinkedList()
     ll.append(1)
     self.assertEqual([1], ll.to_list())
     ll.append(2)
     self.assertEqual([1, 2], ll.to_list())
 def test_last(self):
     ll = LinkedList()
     ll.head = Node(1)
     ll.head.next = Node(2)
     ll.head.next.next = Node(3)
     self.assertEqual(3, ll.length())
Exemple #20
0
class LinkedListTest(unittest.TestCase):
    def setUp(self):
        self.ll = LinkedList()

    def tearDown(self):
        self.ll = None
        pass

    def test_insert(self):
        self.ll.insert(5)
        self.assertEqual(self.ll.toList(), [5])
        self.assertNotEqual(self.ll.toList(), [6])

        self.ll.insert(10)
        self.assertEqual(self.ll.toList(), [5, 10])

    def test_insert_after(self):
        self.ll.insert(5)
        self.ll.insert(10)

        self.ll.insert_after(6, 7)
        self.assertEqual(self.ll.toList(), [5, 7, 10])

        self.ll.insert_after(100, 1000)
        self.assertEqual(self.ll.toList(), [5, 7, 10, 1000])

    def test_delete(self):
        self.ll.insert(5)
        self.ll.delete(5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [10, 1000])

        self.ll.delete(1000)
        self.assertEqual(self.ll.toList(), [10])

        self.ll.delete(10)
        self.assertEqual(self.ll.toList(), [])

        self.ll.delete(100)
        self.assertEqual(self.ll.toList(), [])

    def test_delete_at(self):
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.ll.delete_at(0)
        self.assertEqual(len(self.ll), 0)

        self.ll.push(10)
        self.ll.push(100)
        self.ll.push(1000)
        self.ll.delete_at(2)
        self.assertEqual(self.ll.toList(), [1000, 100])

    def test_length(self):
        self.assertEqual(len(self.ll), 0)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 1)

        self.ll.insert(5)
        self.assertEqual(len(self.ll), 2)

        self.ll.insert(10)
        self.ll.insert(100)
        self.ll.insert(1000)
        self.ll.delete(100)
        self.assertEqual(len(self.ll), 4)

    def test_contains(self):
        self.assertFalse(5 in self.ll)

        self.ll.insert(5)
        self.assertTrue(5 in self.ll)

        self.ll.push(0)
        self.assertTrue(0 in self.ll)
        self.assertFalse(3 in self.ll)

    def test_push(self):
        self.ll.push(5)
        self.assertEqual(self.ll.toList(), [5])

        self.ll.push(10)
        self.assertEqual(self.ll.toList(), [10, 5])

    def test_pop(self):
        self.ll.push(5)
        self.assertEqual(self.ll.pop(), 5)
        self.assertEqual(self.ll.toList(), [])

        self.ll.push(1)
        self.ll.push(2)
        self.ll.push(3)
        self.ll.push(4)
        self.ll.push(100)
        self.assertEqual(self.ll.pop(), 100)
        self.assertEqual(self.ll.toList(), [4, 3, 2, 1])

    def test_swap_at(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.swap_at(1, 3)
        self.assertEqual(self.ll.toList(), [1, 4, 3, 2, 5])

        self.ll.swap_at(0, 4)
        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])

        self.ll.swap_at(0, 1)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 2, 1])

        self.ll.swap_at(4, 3)
        self.assertEqual(self.ll.toList(), [4, 5, 3, 1, 2])

    def test_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.kth(0), 1)
        self.assertEqual(self.ll.kth(3), 4)

    def test_last_kth(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.assertEqual(self.ll.last_kth(1), 5)
        self.assertEqual(self.ll.last_kth(3), 3)

    def test_count(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(5)
        self.ll.push(1)

        self.assertEqual(self.ll.count(1), 1)
        self.assertEqual(self.ll.count(5), 2)

    def test_reverse(self):
        self.ll.push(5)
        self.ll.push(4)
        self.ll.push(3)
        self.ll.push(2)
        self.ll.push(1)

        self.ll.reverse()

        self.assertEqual(self.ll.toList(), [5, 4, 3, 2, 1])
    def test_delete(self):
        ll = LinkedList()
        ll.append(1)
        ll.append(2)
        ll.append(3)
        ll.append(4)

        ll.delete(0)
        self.assertEqual([2, 3, 4], ll.to_list())

        ll.delete(1)
        self.assertEqual([2, 4], ll.to_list())

        ll.delete(1)
        self.assertEqual([2], ll.to_list())

        ll.delete(0)
        self.assertEqual([], ll.to_list())
 def setUp(self):
     self.al = LinkedList()
 def test_to_list(self):
     ll = LinkedList()
     ll.head = Node(1)
     ll.head.next = Node(2)
     ll.head.next.next = Node(3)
     self.assertEqual([1, 2, 3], ll.to_list())