예제 #1
0
파일: test.py 프로젝트: romain-li/leetcode
class TestCase(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()

    def assertListNodeEqual(self, l1, l2):
        if l1 is None and l2 is None:
            return
        self.assertEqual(l1.val, l2.val)
        self.assertListNodeEqual(l1.next, l2.next)

    def test_factory(self):
        l = ListNodeFactory.create(1, 2, 3)
        self.assertEqual(l.val, 1)
        self.assertEqual(l.next.val, 2)
        self.assertEqual(l.next.next.val, 3)

    def test_solution1(self):
        l1 = ListNodeFactory.create(2, 4, 3)
        l2 = ListNodeFactory.create(5, 6, 4)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(7, 0, 8))

    def test_solution2(self):
        l1 = ListNodeFactory.create(1, 2, 3, 4, 5)
        l2 = ListNodeFactory.create(9, 8, 7, 6, 5, 4)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(0, 1, 1, 1, 1, 5))

    def test_solution2(self):
        l1 = ListNodeFactory.create(5)
        l2 = ListNodeFactory.create(5)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(0, 1))
예제 #2
0
class TestCase(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()

    def assertListNodeEqual(self, l1, l2):
        if l1 is None and l2 is None:
            return
        self.assertEqual(l1.val, l2.val)
        self.assertListNodeEqual(l1.next, l2.next)

    def test_factory(self):
        l = ListNodeFactory.create(1, 2, 3)
        self.assertEqual(l.val, 1)
        self.assertEqual(l.next.val, 2)
        self.assertEqual(l.next.next.val, 3)

    def test_solution1(self):
        l1 = ListNodeFactory.create(2, 4, 3)
        l2 = ListNodeFactory.create(5, 6, 4)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(7, 0, 8))

    def test_solution2(self):
        l1 = ListNodeFactory.create(1, 2, 3, 4, 5)
        l2 = ListNodeFactory.create(9, 8, 7, 6, 5, 4)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer,
                                 ListNodeFactory.create(0, 1, 1, 1, 1, 5))

    def test_solution2(self):
        l1 = ListNodeFactory.create(5)
        l2 = ListNodeFactory.create(5)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(0, 1))
예제 #3
0
def main():
    s1 = buildSequeneces([2, 4, 3, 9])
    s2 = buildSequeneces([5, 6, 6])
    sol = Solution()
    result = sol.addTwoNumbers(s1, s2)
    printResult(s1, s2, result)
    pass
예제 #4
0
def test_original():
    l1 = Solution.number_to_linked(342)
    l2 = Solution.number_to_linked(465)
    solution = Solution()
    result = solution.addTwoNumbers(l1, l2)
    assert Solution.linked_to_list(result) == [7, 0, 8]
    assert Solution.linked_to_number(result) == 807
예제 #5
0
def test_zero():
    l1 = Solution.number_to_linked(0)
    l2 = Solution.number_to_linked(0)
    solution = Solution()
    result = solution.addTwoNumbers(l1, l2)
    assert Solution.linked_to_list(result) == [0]
    assert Solution.linked_to_number(result) == 0
예제 #6
0
def test_custom():
    l1 = Solution.number_to_linked(564)
    l2 = Solution.number_to_linked(743)
    solution = Solution()
    result = solution.addTwoNumbers(l1, l2)
    assert Solution.linked_to_list(result) == [7, 0, 3, 1]
    assert Solution.linked_to_number(result) == 1307
예제 #7
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))
예제 #8
0
def test_2():
    """
    7 + 6 = 13
    """
    sol = Solution()
    l1 = ListNode(7)
    l2 = ListNode(6)
    l = sol.addTwoNumbers(l1, l2)
    p = l
    assert p.val == 3
    p = p.next
    assert p.val == 1
    p = p.next
    assert not p
예제 #9
0
def test_3():
    """
    8 + 94 = 102
    """
    sol = Solution()
    l1 = ListNode(8)
    l2 = ListNode(9)
    l2 = insert_ll(l2, 4)
    l = sol.addTwoNumbers(l1, l2)
    p = l
    assert p.val == 2
    p = p.next
    assert p.val == 0
    p = p.next
    assert p.val == 1
    p = p.next
    assert not p
예제 #10
0
def test_1():
    """
    243 + 564 = 807
    """
    sol = Solution()
    l1 = ListNode(2)
    l1 = insert_ll(l1, 4)
    l1 = insert_ll(l1, 3)
    l2 = ListNode(5)
    l2 = insert_ll(l2, 6)
    l2 = insert_ll(l2, 4)
    l = sol.addTwoNumbers(l1, l2)
    p = l
    assert p.val == 7
    p = p.next
    assert p.val == 0
    p = p.next
    assert p.val == 8
    p = p.next
    assert not p
예제 #11
0
def mkListNode(nums):
    """
    :type: nums: List
    :rtype: ListNode
    """
    head = lnp = ListNode(0)
    for i in range(len(nums)):
        lnp.next = ListNode(nums[i])
        lnp = lnp.next
    return head.next


def lsListNode(head):
    """
    :type: head: ListNode
    :rtype: List
    """
    lnp = head
    rList = []
    while lnp != None:
        rList.append(lnp.val)
        lnp = lnp.next
    return rList


inpt1 = mkListNode([2, 4, 3])
inpt2 = mkListNode([5, 6, 4])
sol = Solution()
res = sol.addTwoNumbers(inpt1, inpt2)
print(lsListNode(res))
예제 #12
0
파일: test.py 프로젝트: zzu-andrew/arts
    """
    l = ListNode(0)
    node = l

    for x in nums:
        node.next = ListNode(x)
        node = node.next

    return l.next


def assertEqual(l1, l2):
    """
    :type l1 ListNode
    :type l2 ListNode
    """
    while l1 != None and l2 != None:
        if l1.val != l2.val:
            break
        else:
            l1 = l1.next
            l2 = l2.next
    assert l1 == None and l2 == None


s = Solution()
assertEqual(s.addTwoNumbers(listnum([7, 8, 9]), listnum([0, 9, 3])),
            listnum([7, 7, 3, 1]))
assertEqual(s.addTwoNumbers(listnum([1]), listnum([9, 9, 9])),
            listnum([0, 0, 0, 1]))