Ejemplo n.º 1
0
    def _get_sample_linkedlist(self, size=4):
        """Create a sample linkedlist and return the linkedlist obj."""
        ll = LinkedList()

        for item in range(size):
            ll.push(item)

        return ll
Ejemplo n.º 2
0
def test_three_nodes():
    binaryTree = BinaryTree()
    binaryTree.root = Node(Node(data=1), Node(data=3), 2)
    arrlinkedResult = [LinkedList(), LinkedList()]
    arrlinkedResult[0].add(2)
    arrlinkedResult[1].add(1)
    arrlinkedResult[1].add(3)

    assert arrlinkedResult == getDepthLists(binaryTree)
Ejemplo n.º 3
0
def test_five_nodes():
    binaryTree = BinaryTree()
    binaryTree.root = Node(Node(left=Node(data=1), data=2),
                           Node(right=Node(data=5), data=4), 3)
    arrlinkedResult = [LinkedList(), LinkedList(), LinkedList()]
    arrlinkedResult[0].add(3)
    arrlinkedResult[1].add(2)
    arrlinkedResult[1].add(4)
    arrlinkedResult[2].add(1)
    arrlinkedResult[2].add(5)

    assert arrlinkedResult == getDepthLists(binaryTree)
Ejemplo n.º 4
0
    def test_three_elements(self):
        three_elem = LinkedList()
        three_elem.add(1)
        three_elem.add(2)
        three_elem.add(3)
        node = three_elem.root.next
        deleteMiddle(node)

        result = LinkedList()
        result.add(1)
        result.add(3)

        self.assertEqual(result, three_elem)
Ejemplo n.º 5
0
def test_seven_nodes():
    binaryTree = BinaryTree()
    binaryTree.root = Node(Node(Node(data=1), Node(data=3), 2),
                           Node(Node(data=5), Node(data=7), 6), 4)

    arrlinkedResult = [LinkedList(), LinkedList(), LinkedList()]
    arrlinkedResult[0].add(4)
    arrlinkedResult[1].add(2)
    arrlinkedResult[1].add(6)
    arrlinkedResult[2].add(1)
    arrlinkedResult[2].add(3)
    arrlinkedResult[2].add(5)
    arrlinkedResult[2].add(7)

    assert arrlinkedResult == getDepthLists(binaryTree)
Ejemplo n.º 6
0
def test_one_node():
    binaryTree = BinaryTree()
    binaryTree.root = Node(data=1)
    arrlinkedResult = [LinkedList()]
    arrlinkedResult[0].add(1)

    assert arrlinkedResult == getDepthLists(binaryTree)
Ejemplo n.º 7
0
def recurDepthLists(node, depth, arr):
    if not node:
        return
    if depth >= len(arr):
        arr.append(LinkedList())
    arr[depth].add(node.data)
    recurDepthLists(node.left, depth + 1, arr)
    recurDepthLists(node.right, depth + 1, arr)
def test_insert_sorted_two():
    ll = LinkedList()
    ll.push(1)
    ll.push(2)
    ll.push(5)
    ll.push(10)

    head = insertsorted(ll.head, 3)

    assert(_get_all_data(head)==[1,2,3,5,10])
def test_insert_sorted_three():
    ll = LinkedList()
    ll.push(3)
    ll.push(4)
    ll.push(5)
    ll.push(10)

    head = insertsorted(ll.head, 1)

    assert(_get_all_data(head)==[1,3,4,5,10])
Ejemplo n.º 10
0
def test_splitoddeven_six():
    ll = LinkedList()

    print(_get_all_data(ll.head))

    odd, even = splitoddeven(ll.head)

    print(_get_all_data(odd))
    print(_get_all_data(even))

    assert (isoddlist(odd) == True)
    assert (isevenlist(even) == True)
Ejemplo n.º 11
0
    def test_three_letter(self):
        l1 = LinkedList()
        l1.add('a')
        l1.add('b')
        l1.add('a')

        self.assertEqual(True, isPalindrome(l1, len(l1), "3"))
Ejemplo n.º 12
0
    def test_cycle(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        l1.root.next.next.next.next = l1.root.next
Ejemplo n.º 13
0
    def test_no_cycle(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        self.assertEqual(None, findCycleStart(l1))
Ejemplo n.º 14
0
    def test_cycle_immediately(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        l1.root.next.next.next.next = l1.root
Ejemplo n.º 15
0
    def test_four_elements(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        self.assertEqual(2, kFromEnd(l1, 2))
Ejemplo n.º 16
0
def test_partition_x():
    ll = LinkedList()
    ll.push(1)
    ll.push(4)
    ll.push(3)
    ll.push(2)
    ll.push(5)
    ll.push(2)

    head = partition_x(ll.head, 3)
    cur = head
    while cur is not None:
        print cur.data
        cur = cur.next


            
Ejemplo n.º 17
0
def test_splitoddeven_two():
    ll = LinkedList()
    ll.push(2)
    ll.push(3)
    ll.push(4)
    ll.push(2)
    ll.push(9)

    print(_get_all_data(ll.head))

    odd, even = splitoddeven(ll.head)

    print(_get_all_data(odd))
    print(_get_all_data(even))

    assert (isoddlist(odd) == True)
    assert (isevenlist(even) == True)
Ejemplo n.º 18
0
def test_insert_sorted_five():
    ll = LinkedList()
    ll.push(1)
    ll.push(2)
    ll.push(3)
    ll.push(4)

    head = insertsorted(ll.head, 10)

    assert(_get_all_data(head)==[1,2,3,4,10])
Ejemplo n.º 19
0
def test_deleteduplicates_one():
    ll = LinkedList()
    ll.push(1)
    ll.push(1)
    ll.push(1)

    head = deleteduplicates(ll.head)
    result = _get_all_data(head)

    assert (result == [1])
Ejemplo n.º 20
0
def test_deleteduplicates_two():
    ll = LinkedList()
    ll.push(1)
    ll.push(2)
    ll.push(3)

    head = deleteduplicates(ll.head)
    result = _get_all_data(head)

    assert (result == [1, 2, 3])
Ejemplo n.º 21
0
def partitionList(list, val):
    lowerList = LinkedList()
    upperList = LinkedList()
    current = list.root
    while current:
        if current.data < val:
            lowerList.insertAtHead(current.data)
        else:
            upperList.insertAtHead(current.data)
        current = current.next

    if lowerList.root:
        current = lowerList.root
        while (current.next):
            current = current.next
        current.next = upperList.root
        return lowerList
    else:
        return upperList
Ejemplo n.º 22
0
def test_reorg_two():
    ll = LinkedList()
    ll.push(0)
    ll.push(1)
    ll.push(2)
    ll.push(3)

    print " "

    result = _get_all_data(ll.head)
    print result

    head = reorg(ll.head)
    result = _get_all_data(head)
    print result
    
    assert(result == [0,2,1,3])
Ejemplo n.º 23
0
    def test_large_elements(self):
        l1 = LinkedList()
        result = LinkedList()

        l1.add(2)
        l1.add(9)
        l1.add(7)
        l1.add(6)
        l1.add(3)
        l1.add(4)
        l1.add(1)

        result.add(1)
        result.add(3)
        result.add(2)
        result.add(4)
        result.add(6)
        result.add(7)
        result.add(9)

        self.assertEqual(result, partitionList(l1, 4))
Ejemplo n.º 24
0
 def setUp(self):
     self.l1 = LinkedList()
     self.l2 = LinkedList()
Ejemplo n.º 25
0
def test_reorg_one():
    ll = LinkedList()
    ll.push(0)
    ll.push(1)
    ll.push(2)
    ll.push(3)
    ll.push(4)
    ll.push(5)
    ll.push(6)
    ll.push(7)
    ll.push(8)
    ll.push(9)
    ll.push(10)
    ll.push(11)

    print " "

    result = _get_all_data(ll.head)
    print result

    head = reorg(ll.head)
    result = _get_all_data(head)
    print result
    
    assert(result == [0,2,1,5,4,3,9,8,7,6,11,10])
Ejemplo n.º 26
0
def test_insert_sorted_four():
    ll = LinkedList()
    head = insertsorted(ll.head, 10)

    assert(_get_all_data(head)==[10])
Ejemplo n.º 27
0
class TestRemoveDups(unittest.TestCase):
    def helper_equal_lists(self, l1, l2):
        node1 = l1.root
        node2 = l2.root

        while node1 and node2:
            if (node1.data != node2.data):
                return False

            node1 = node1.next
            node2 = node2.next

        return not (node1 or node2)

    def setUp(self):
        self.l1 = LinkedList()
        self.l2 = LinkedList()

    def test_single_element(self):
        self.l1.add(1)
        self.l2.add(1)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         True)

    def test_one_dup(self):
        self.l1.add(1)
        self.l1.add(2)
        self.l1.add(2)

        self.l2.add(1)
        self.l2.add(2)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         True)

    def test_three_dups(self):
        self.l1.add(1)
        self.l1.add(2)
        self.l1.add(2)
        self.l1.add(3)
        self.l1.add(3)

        self.l2.add(1)
        self.l2.add(2)
        self.l2.add(3)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         True)

    def test_high_dup_occurence(self):
        self.l1.add(1)
        self.l1.add(2)
        self.l1.add(2)
        self.l1.add(3)
        self.l1.add(3)
        self.l1.add(3)
        self.l1.add(3)

        self.l2.add(1)
        self.l2.add(2)
        self.l2.add(3)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         True)

    def test_interspersed_dups(self):
        self.l1.add(1)
        self.l1.add(2)
        self.l1.add(2)
        self.l1.add(3)
        self.l1.add(3)
        self.l1.add(4)
        self.l1.add(4)
        self.l1.add(3)
        self.l1.add(5)
        self.l1.add(5)
        self.l1.add(5)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(6)
        self.l1.add(6)

        self.l2.add(1)
        self.l2.add(2)
        self.l2.add(3)
        self.l2.add(4)
        self.l2.add(5)
        self.l2.add(6)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         True)

    def test_out_of_order_dups(self):
        self.l1.add(1)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(5)
        self.l1.add(2)
        self.l1.add(3)
        self.l1.add(4)
        self.l1.add(4)
        self.l1.add(3)
        self.l1.add(5)
        self.l1.add(5)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(6)
        self.l1.add(6)

        self.l2.add(1)
        self.l2.add(3)
        self.l2.add(2)
        self.l2.add(5)
        self.l2.add(4)
        self.l2.add(6)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         True)

    def test_invalid_case_dups_in_expected(self):
        self.l1.add(1)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(5)
        self.l1.add(2)
        self.l1.add(3)
        self.l1.add(4)
        self.l1.add(4)
        self.l1.add(3)
        self.l1.add(5)
        self.l1.add(5)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(6)
        self.l1.add(6)

        self.l2.add(1)
        self.l2.add(3)
        self.l2.add(3)
        self.l2.add(2)
        self.l2.add(5)
        self.l2.add(4)
        self.l2.add(6)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         False)

    def test_invalid_case_dups_out_of_order(self):
        self.l1.add(1)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(5)
        self.l1.add(2)
        self.l1.add(3)
        self.l1.add(4)
        self.l1.add(4)
        self.l1.add(3)
        self.l1.add(5)
        self.l1.add(5)
        self.l1.add(3)
        self.l1.add(2)
        self.l1.add(6)
        self.l1.add(6)

        self.l2.add(3)
        self.l2.add(1)
        self.l2.add(2)
        self.l2.add(5)
        self.l2.add(4)
        self.l2.add(6)

        self.assertEqual(self.helper_equal_lists(self.l2, removeDups(self.l1)),
                         False)
Ejemplo n.º 28
0
    def test_partition_four_elem(self):
        l1 = LinkedList()
        result = LinkedList()

        result.add(2)
        result.add(1)
        result.add(4)
        result.add(3)

        l1.add(1)
        l1.add(3)
        l1.add(4)
        l1.add(2)

        self.assertEqual(result, partitionList(l1, 3))
def test_mergetwolists_one():
    ll1 = LinkedList()
    ll1.push(-10)
    ll1.push(-10)
    ll1.push(-9)
    ll1.push(-4)
    ll1.push(1)
    ll1.push(6)
    ll1.push(6)

    ll2 = LinkedList()
    ll2.push(-7)

    solution = Solution()
    head = solution.mergeTwoLists(ll1.head, ll2.head)

    result = _get_all_data(head)
    assert(result==[-10,-10,-9,-7,-4,1,6,6])
Ejemplo n.º 30
0
    def test_partition_sorted_list(self):
        l1 = LinkedList()
        result = LinkedList()

        result.add(1)
        result.add(4)
        result.add(3)
        result.add(2)

        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        self.assertEqual(result, partitionList(l1, 2))