예제 #1
0
    def test_example_3(self):
        l1 = ListNode(val = 9)
        l1.next = ListNode(val = 9)
        l1.next.next = ListNode(val = 9)
        l1.next.next.next = ListNode(val = 9)
        l1.next.next.next.next = ListNode(val = 9)
        l1.next.next.next.next.next = ListNode(val = 9)
        l1.next.next.next.next.next.next = ListNode(val = 9)

        l2 = ListNode(val = 9)
        l2.next = ListNode(val = 9)
        l2.next.next = ListNode(val = 9)
        l2.next.next.next = ListNode(val = 9)

        [8,9,9,9,0,0,0,1]
        expected = ListNode(val = 8)
        expected.next = ListNode(val = 9)
        expected.next.next = ListNode(val = 9)
        expected.next.next.next = ListNode(val = 9)
        expected.next.next.next.next = ListNode(val = 0)
        expected.next.next.next.next.next = ListNode(val = 0)
        expected.next.next.next.next.next.next = ListNode(val = 0)
        expected.next.next.next.next.next.next.next = ListNode(val = 1)

        self.assert_linked_lists_equal( expected, Solution.add_two_numbers( l1, l2 ) )
예제 #2
0
def test_answer():
    expectedAnswer = [2, 0, 6]
    # [5, 7, 4]
    l1 = ListNode(5)
    l1_head = l1

    l1.next = ListNode(7)
    l1 = l1.next
    l1.next = ListNode(4)

    # [7, 2, 1]
    l2 = ListNode(7)
    l2_head = l2

    l2.next = ListNode(2)
    l2 = l2.next
    l2.next = ListNode(1)

    sn = Solution()
    ret_list_node = sn.addTwoNumbers(l1_head, l2_head)

    answer = [ret_list_node.val]
    while ret_list_node.next is not None:
        answer.append(ret_list_node.next.val)
        ret_list_node = ret_list_node.next

    assert (len(expectedAnswer) == len(answer)
            and sorted(expectedAnswer) == sorted(answer))
예제 #3
0
    def test_addTwoNumbers_withTwoDigits_shouldReturnTreeDigitResult(self):
        l1 = ListNode(9)
        l1.next = ListNode(8)

        l2 = ListNode(7)
        l2.next = ListNode(6)

        self.assertEqual([6, 5, 1], Solution().addTwoNumbers(l1, l2).toArray())
예제 #4
0
    def test_addTwoNumbers_withTwoDigits_shouldReturnTwoDigitResult(self):
        l1 = ListNode(1)
        l1.next = ListNode(2)

        l2 = ListNode(2)
        l2.next = ListNode(4)

        self.assertEqual([3, 6], Solution().addTwoNumbers(l1, l2).toArray())
예제 #5
0
파일: test_.py 프로젝트: ftlka/problems
def create_linked_list(l):
	root = ListNode(0)
	start = ListNode(l[0])
	root.next = start
	for el in l[1:]:
		start.next = ListNode(el)
		start = start.next
	return root.next
예제 #6
0
    def test_addTwoNumbers_withTreeDigits_shouldReturnTreeDigitResult(self):
        l1 = ListNode(2)
        l1.next = ListNode(4)
        l1.next.next = ListNode(3)

        l2 = ListNode(5)
        l2.next = ListNode(6)
        l2.next.next = ListNode(4)

        self.assertEqual([7, 0, 8], Solution().addTwoNumbers(l1, l2).toArray())
예제 #7
0
 def test1(self):
     head = ListNode(1)
     node2 = ListNode(2)
     node3 = ListNode(3)
     head.next = node2
     node2.next = node3
     s = Solution()
     res = s.reverseBetween(head, 1, 3)
     while res:
         print res.val
         res = res.next
예제 #8
0
def test_1():
    sol = Solution()
    n1 = ListNode(1)
    n2 = ListNode(2)
    n1.next = n2
    n3 = ListNode(3)
    n2.next = n3
    n4 = ListNode(4)
    n3.next = n4
    n5 = ListNode(5)
    n4.next = n5
    n5.next = n2
    assert sol.hasCycle(n1)
예제 #9
0
def test_solution():
    l1 = ListNode(2)
    l1.next = ListNode(4)
    l1.next.next = ListNode(3)

    l2 = ListNode(5)
    l2.next = ListNode(6)
    l2.next.next = ListNode(4)

    res = Solution().addTwoNumbers(l1, l2)
    assert res.val == 7
    assert res.next.val == 0
    assert res.next.next.val == 8
예제 #10
0
def test_solution():
    head = ListNode(3)
    head.next = ListNode(2)
    head.next.next = ListNode(0)
    head.next.next.next = ListNode(-4)
    head.next.next.next.next = head.next

    assert Solution().detect_cycle(head).val == 2

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = head

    assert Solution().detect_cycle(head).val == 1
예제 #11
0
def test_solution_recursive_2():
    l1 = ListNode(2)
    l1.next = ListNode(4)
    l1.next.next = ListNode(3)

    l2 = ListNode(5)
    l2.next = ListNode(6)
    l2.next.next = ListNode(4)

    # pudb.set_trace()
    res = SolutionRecursive2().add_two_numbers(l1, l2)
    assert res.val == 7
    assert res.next.val == 0
    assert res.next.next.val == 8
예제 #12
0
    def test_example_1(self):
        l1 = ListNode(val = 2)
        l1.next = ListNode(val = 4)
        l1.next.next = ListNode(val = 3)

        l2 = ListNode(val = 5)
        l2.next = ListNode(val = 6)
        l2.next.next = ListNode(val = 4)
        
        expected = ListNode(val = 7)
        expected.next = ListNode(val = 0)
        expected.next.next = ListNode(val = 8)

        self.assert_linked_lists_equal( expected, Solution.add_two_numbers( l1, l2 ) )
예제 #13
0
    def test1(self):
        l1 = ListNode(2)
        l1.next = ListNode(4)
        l1.next.next = ListNode(3)

        l2 = ListNode(5)
        l2.next = ListNode(6)
        l2.next.next = ListNode(4)

        ans = ListNode(7)
        ans.next = ListNode(0)
        ans.next.next = ListNode(8)

        self.customAssertEqual(Solution().addTwoNumbers(l1, l2), ans)
예제 #14
0
def test_0():
    sol = Solution()
    assert not sol.hasCycle(None)

    n1 = ListNode(1)
    n1.next = n1
    assert sol.hasCycle(n1)

    n1 = ListNode(1)
    assert not sol.hasCycle(n1)

    n1 = ListNode(1)
    n2 = ListNode(2)
    n1.next = n2
    assert not sol.hasCycle(n1)
예제 #15
0
def insert_ll(ll, x):
    """
    insert x into front of linked-list ll
    """
    node = ListNode(x)
    node.next = ll
    return node
예제 #16
0
    def test_add_two_numbers(self):
        l1 = ListNode(2)
        l1.next = ListNode(4)
        l1.next.next = ListNode(3)

        l2 = ListNode(5)
        l2.next = ListNode(6)
        l2.next.next = ListNode(4)

        output = Solution().addTwoNumbers(l1, l2)

        outputNumbers = [7, 0, 8]
        i = 0
        while output is not None:
            self.assertEqual(output.val, outputNumbers[i])
            print output.val
            i = i + 1
            output = output.next

        l1 = ListNode(1)
        l1.next = ListNode(8)

        l2 = ListNode(0)

        output = Solution().addTwoNumbers(l1, l2)

        outputNumbers = [1, 8]
        i = 0
        while output is not None:
            self.assertEqual(output.val, outputNumbers[i])
            print output.val
            i = i + 1
            output = output.next

        l1 = ListNode(5)

        l2 = ListNode(5)

        output = Solution().addTwoNumbers(l1, l2)

        outputNumbers = [0, 1]
        i = 0
        while output is not None:
            self.assertEqual(output.val, outputNumbers[i])
            print output.val
            i = i + 1
            output = output.next
예제 #17
0
 def test2(self):
     head = ListNode(1)
     node2 = ListNode(2)
     head.next = node2
     s = Solution()
     res = s.reverseBetween(head, 1, 2)
     while res:
         print res.val
         res = res.next
예제 #18
0
def test_solution_recursive_2():
    head = ListNode(1)
    head.next = ListNode(1)
    head.next.next = ListNode(2)
    head.next.next.next = ListNode(3)

    new_head = SolutionRecursive().deleteDuplicates(head)
    assert new_head.val == 2
    assert new_head.next.val == 3
예제 #19
0
def test_solution_2():
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)

    res = SolutionRecursive().reverse_list(head)
    assert res.val == 3
    assert res.next.val == 2
    assert res.next.next.val == 1
예제 #20
0
def test_solution_2():
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(2)
    head.next.next.next = ListNode(3)

    new_head = Solution().deleteDuplicates(head)
    assert new_head.val == 1
    assert new_head.next.val == 3
예제 #21
0
def test_solution():
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)

    res = Solution().reverseList(head)
    assert res.val == 3
    assert res.next.val == 2
    assert res.next.next.val == 1
예제 #22
0
def test():
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = ListNode(5)
    k = 2

    pudb.set_trace()
    rotate(head, k)
예제 #23
0
def test_2():
    sol = Solution()
    l1 = ListNode(1)
    p = ListNode(3); l1.next = p; q = p
    p = ListNode(5); q.next = p; q = p
    p = ListNode(5); q.next = p; q = p

    l2 = ListNode(0)
    p = ListNode(2); l2.next = p; q = p
    p = ListNode(4); q.next = p; q = p

    p = sol.mergeKLists([l2, l1])
    assert p.val == 0; p = p.next
    assert p.val == 1; p = p.next
    assert p.val == 2; p = p.next
    assert p.val == 3; p = p.next
    assert p.val == 4; p = p.next
    assert p.val == 5; p = p.next
    assert p.val == 5; p = p.next
    assert p is None
예제 #24
0
def test():
    root = ListNode(0)
    a = ListNode(1)
    b = ListNode(2)
    root.next = a
    root.next.next = b

    assert split(root, 1) == a
    assert root.next == None
    assert split(None, 1) == None
    assert split(None, 4) == None
예제 #25
0
파일: test.py 프로젝트: zhlinh/leetcode
def mkListNode(nums, i, dic):
    """
    :type: nums: List
    :rtype: ListNode
    """
    if nums[i] in dic:
        return dic[nums[i]]
    head = ListNode(nums[i])
    dic[nums[i]] = head
    if i + 1 < len(nums):
        head.next = mkListNode(nums, i + 1, dic)
    return head
예제 #26
0
파일: test.py 프로젝트: zhlinh/leetcode
def mkListNode(nums, i, dic):
    """
    :type: nums: List
    :rtype: ListNode
    """
    if nums[i] in dic:
        return dic[nums[i]]
    head = ListNode(nums[i])
    dic[nums[i]] = head
    if i + 1 < len(nums):
        head.next = mkListNode(nums, i + 1, dic)
    return head
예제 #27
0
def test():
    sol = Solution()
    head = ListNode(1)
    p = ListNode(2); head.next = p; q = p
    p = ListNode(3); q.next = p; q = p
    p = ListNode(4); q.next = p; q = p
    p = ListNode(5); q.next = p; q = p
    p = sol.removeNthFromEnd(head, 2)
    assert p.val == 1; p = p.next
    assert p.val == 2; p = p.next
    assert p.val == 3; p = p.next
    assert p.val == 5; p = p.next
    assert p is None
예제 #28
0
def test_4():
    sol = Solution()
    l1 = ListNode(1)
    p = ListNode(3); l1.next = p; q = p
    p = ListNode(5); q.next = p; q = p
    p = ListNode(5); q.next = p; q = p

    p = sol.mergeTwoLists(l1, None)
    assert p.val == 1; p = p.next
    assert p.val == 3; p = p.next
    assert p.val == 5; p = p.next
    assert p.val == 5; p = p.next
    assert p is None
예제 #29
0
def test_5():
    sol = Solution()
    l1 = ListNode(1)
    p = ListNode(3); l1.next = p; q = p
    p = ListNode(5); q.next = p; q = p
    p = ListNode(5); q.next = p; q = p

    p = sol.mergeKLists([None, l1])
    assert p.val == 1; p = p.next
    assert p.val == 3; p = p.next
    assert p.val == 5; p = p.next
    assert p.val == 5; p = p.next
    assert p is None
예제 #30
0
def test_solution():
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = ListNode(5)

    m = 2
    n = 4

    res = Solution().reverseBetween(head, m, n)
    assert res.val == 1
    assert res.next.val == 4
    assert res.next.next.val == 3
    assert res.next.next.next.val == 2
    assert res.next.next.next.next.val == 5
예제 #31
0
def run_test(test_input, expected, solution):
    head = None
    curr = None

    for val in test_input:
        if curr == None:
            curr = ListNode(val)
            head = curr
        else:
            curr.next = ListNode(val)
            curr = curr.next

    new_head = solution().swapPairs(head)

    for i, val in enumerate(expected):
        msg = "i:", i
        assert new_head.val == val, msg
        new_head = new_head.next
예제 #32
0
from solution import Solution, ListNode

s = Solution()
ln = ListNode(1)
a = ListNode(1)
ln.next = a
b = ListNode(2)
a.next = b
b.next = None

s.deleteDuplicates(ln)
m = ln
while m:
    print(m.val)
    m = m.next
예제 #33
0
cur.next.next = second

assert s.hasCycle(head)

# tail connects to 1
head = cur = ListNode(1)
cur.next = ListNode(2)
cur.next.next = head

assert s.hasCycle(head)

# without cycle
head = cur = ListNode(1)
for i in range(2, 6):
    cur.next = ListNode(i)
    cur = cur.next

assert not s.hasCycle(head)

# empty
assert not s.hasCycle(None)

# one list node
cur = ListNode(5)
assert not s.hasCycle(cur)

# cycle with one node
cur = ListNode(5)
cur.next = cur
assert s.hasCycle(cur)
예제 #34
0
# -*- coding: utf-8 -*-

from solution import Solution, ListNode

Anode1 = ListNode(1)
Anode2 = ListNode(4)
Anode3 = ListNode(5)
Anode1.next = Anode2
Anode2.next = Anode3
Bnode1 = ListNode(1)
Bnode2 = ListNode(3)
Bnode3 = ListNode(4)
Bnode1.next = Bnode2
Bnode2.next = Bnode3
Cnode1 = ListNode(2)
Cnode2 = ListNode(6)
Cnode1.next = Cnode2

lst = [Anode1, Bnode1, Cnode1]

sol = Solution()

print(sol.mergeKLists(lst))
예제 #35
0
# test 1
l = cur = ListNode(1)
for i in range(2, 6):
    cur.next = ListNode(i)
    cur = cur.next

result = s.reverseBetween(l, 2, 4)
expected = [1, 4, 3, 2, 5]
for el in expected:
    assert result.val == el
    result = result.next

# test 2
l = ListNode(3)
l.next = ListNode(5)

result = s.reverseBetween(l, 1, 1)
expected = [3, 5]

for el in expected:
    assert result.val == el
    result = result.next

# test 3
l = ListNode(3)
l.next = ListNode(5)

result = s.reverseBetween(l, 2, 2)
expected = [3, 5]
예제 #36
0
파일: test_.py 프로젝트: ftlka/problems
s = Solution()

# test 1
root = cur = ListNode(1)
for node in range(2, 6):
    cur.next = ListNode(node)
    cur = cur.next

assert s.middleNode(root).val == 3

# test 2
root = cur = ListNode(1)
for node in range(2, 7):
    cur.next = ListNode(node)
    cur = cur.next

assert s.middleNode(root).val == 4

# test 3
root = ListNode(1)
root.next = ListNode(2)

assert s.middleNode(root).val == 2

# test 4
assert not s.middleNode(None)

# test 5
assert s.middleNode(ListNode(1)).val == 1
예제 #37
0
    def test3(self):
        node1 = ListNode(1)
        node2 = ListNode(2)
        node3 = ListNode(3)
        node4 = ListNode(4)
        node5 = ListNode(5)
        node6 = ListNode(6)
        node7 = ListNode(7)
        node8 = ListNode(8)
        node9 = ListNode(9)
        node10 = ListNode(10)
        node11 = ListNode(11)
        node12 = ListNode(12)
        node13 = ListNode(13)
        node13 = ListNode(13)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        node5.next = node6
        node6.next = node1

        node7.next = node8
        node8.next = node9
        node9.next = node10
        node10.next = node11
        node11.next = node12
        node12.next = node13
        node12.next = node7

        s = Solution()
        res = s.is_interset(node1, node7)
        print res
        self.assertFalse(res)