Ejemplo n.º 1
0
def test_merge_sorted_linked_list():
    data = [((LinkedNode.generate_ascending([7, 5, 9, 3, 1]),
              LinkedNode.generate_ascending([6, 4, 8, 2, 10, 0])),
             LinkedNode.generate_ascending([i for i in range(11)])),
            ((LinkedNode.generate_ascending([6, 7, 10, 8, 9]),
              LinkedNode.generate_ascending([3, 4, 1, 5, 2, 0])),
             LinkedNode.generate_ascending([i for i in range(11)])),
            ((LinkedNode.generate_ascending([5, 3, 1]),
              LinkedNode.generate_ascending([6, 2, 5, 4])),
             LinkedNode.generate_ascending([5, 6, 2, 5, 3, 1, 4])),
            ((LinkedNode.generate_ascending([7, 5, 9, 3, 1]), None),
             LinkedNode.generate_ascending([7, 5, 9, 3, 1])),
            ((None, LinkedNode.generate_ascending([7, 5, 9, 3, 1])),
             LinkedNode.generate_ascending([7, 5, 9, 3, 1])),
            ((None, None), None)]

    def eql_ln(l1, l2):
        p1 = l1
        p2 = l2
        while p1 or p2:
            if (p1 and not p2) or (p2 and not p1):
                return False
            if p1.value != p2.value:
                return False
            p1 = p1.next
            p2 = p2.next
        return True

    test_fixture(merge_sorted_linked_list, data, comp=eql_ln, hide_input=True)
Ejemplo n.º 2
0
def test_kruskal():
    sg_data1 = {
        ('a', 'b'): 3,
        ('a', 'c'): 4,
        ('a', 'd'): 5,
        ('a', 'f'): 1,
        ('b', 'c'): 3,
        ('b', 'd'): 8,
        ('c', 'd'): 6,
        ('b', 'e'): 4,
        ('d', 'e'): 3,
        ('f', 'c'): 7
    }
    test_data = [
        ((sg_data1, ), ([('a', 'f'), ('a', 'b'), ('b', 'c'), ('d', 'e'),
                         ('b', 'e')], 14)),
    ]

    def cmpr(r, e):
        if r[1] != e[1]:
            return False
        if len(r[0]) != len(e[0]):
            return False
        num_match = 0
        for a, b in r[0]:
            if (a, b) in e[0] or (b, a) in e[0]:
                num_match += 1
        return num_match == len(r[0])

    print('test krustal')
    test_fixture(Kruska, test_data, comp=cmpr)
Ejemplo n.º 3
0
def test():
    data = [
        ((4, ), 2),
        ((8, ), 2),
        ((10, ), 3),
        ((1, ), 1),
        ((0, ), 0),
    ]
    s = Solution()
    test_fixture(s.mySqrt, data)
Ejemplo n.º 4
0
def test_zigzag_str():
    s = 'abcdefghijklmn'
    data = [
        ((s, 1), 'abcdefghijklmn'),
        ((s, 2), 'acegikmbdfhjln'),
        ((s, 3), 'aeimbdfhjlncgk'),
        ((s, 4), 'agmbfhlnceikdj'),
    ]
    test_fixture(zigzag_str, data)
    test_fixture(zigzag_str2, data, hide_input=True)
Ejemplo n.º 5
0
def test():
    data = [
        (('interview', ), 'n'),
        (('racecar', ), 'e'),
        (('aaaaaaa', ), None),
        (('aaaaaaba', ), 'b'),
        (('ababababab', ), None),
        (('a', ), 'a'),
    ]
    test_fixture(find_first_non_repeat, data)
Ejemplo n.º 6
0
def test_perfect_number():
    data = [
        ((28, ), True),
        ((6, ), True),
        ((496, ), True),
        ((8128, ), True),
        ((2, ), False),
        ((8, ), False),
        ((28853, ), False),
    ]
    test_fixture(perfect_num3, data)
Ejemplo n.º 7
0
def test():
    data = [
        (([-1], ), 1),
        (([1, -1], ), 2),
        (([1, 4, -1, 3, 2], ), 4),
        # ((, ), ),
        # ((, ), ),
    ]

    s = Solution()
    test_fixture(s.buildLinkedList, data)
Ejemplo n.º 8
0
def test_findMiddle():
    print('test_findMiddle')
    data = [
        (([1], ), -1),
        (([1, 2], ), -1),
        (([3, 1, 3], ), 1),
        (([1, 2, 3, 4, 8, 5, 5], ), 4),
        (([2, -1, 3, 0, 4, -1, 1, -3, 0], ), 2),
        (([2, -1, 3, 0, 4, -1, 1, -2, 0], ), -1),
    ]
    s = Solution()
    test_fixture(s.findMiddle, data)
def test():
    data = [
        (([10,9,2,5,3,7,101,18],),4),
        (([0,1,0,3,2,3],),4),
        (([7,7,7,7,7,7,7],),1),
        (([7,6,5,4,3,2,1],),1),
        (([3],),1),
        (([10,9,2,5,3,7,101,18,77,42,138,90,99,100],),8),
        (([1,3,6,7,9,4,10,5,6],),6)
    ]
    s = Solution()
    test_fixture(s.lengthOfLIS, data)
Ejemplo n.º 10
0
def test():
    data = [
        (([0,1,0,3,12],), [1,3,12,0,0]),
        (([0],), [0]),
        (([1],), [1]),
        (([1,0],), [1,0]),
        (([0,1],), [1,0]),
        (([1,0,2,0,3,0],), [1,2,3,0,0,0]),
        (([31,0,1,0,3,12,31,4,1,41,0,9,0,0,14,94,0,0,91,4,1,3,4,0,0,5],), [31,1,3,12,31,4,1,41,9,14,94,91,4,1,3,4,5,0,0,0,0,0,0,0,0,0]),
        (([0,0]+[i for i in range(1,1000)],), [i for i in range(1,1000)]+[0,0]),
    ]
    s = Solution()
    test_fixture(s.test_stub, data)
Ejemplo n.º 11
0
def test():
    data = [
        (("leetcode", ["leet","code"]), True),
        (("applepenapple", ["apple","pen"]), True),
        (("catsandog",["cats","dog","sand","and","cat"]), False),
        (("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
          ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]), False),
        (("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
          ["aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa","ba"]), False),
        (("ababababababababababababababababababababababab", ["ab","cd","aba"]), True),
    ]
    s = Solution()
    test_fixture(s.wordBreak, data)
def test():
    data = [
        (([[1, 5], [10, 14], [16, 18]], [[2, 6], [8, 10], [11, 20]]), [[1, 6],
                                                                       [8,
                                                                        20]]),
        (([[1, 3]], [[2, 4]]), [[1, 4]]),
        (([[1, 3], [5, 10]], [[3, 8], [12, 17], [18, 20]]), [[1, 10], [12, 17],
                                                             [18, 20]]),
        (([[1, 3], [4, 8]], [[1, 2], [4, 9], [11, 13]]), [[1, 3], [4, 9],
                                                          [11, 13]]),
        (([[1, 10], [14, 15]], [[2, 3], [5, 6], [8, 9]]), [[1, 10], [14, 15]]),
        # ((, ), ),
    ]

    test_fixture(merge_sorted_interval, data)
    test_fixture(merge_sorted_interval2, data)
Ejemplo n.º 13
0
def test():
    data = [
        ((4,), 2),
        ((8,), sqrt(8)),
        ((10,), sqrt(10)),
        ((1,), 1),
        ((0,), 0),
        ((0.25,), 0.5),
    ]

    precision = 4
    
    def cmpr(r, e):
        if abs(r-e) < pow(0.1, precision):
            return True
        return False
    test_fixture(Solution.sqrt, data, cmpr)
Ejemplo n.º 14
0
def test():
    data = [
        (([[1, 3], [2, 6], [8, 10], [15, 18]], ), [[1, 6], [8, 10], [15, 18]]),
        (([[1, 4], [4, 5]], ), [[1, 5]]),
        (([[1, 10]], ), [[1, 10]]),
        (([[3, 5], [4, 5], [7, 10], [2, 4]], ), [[2, 5], [7, 10]]),
        (([[2, 5], [3, 3], [5, 5], [4, 9], [10, 12], [14, 18]], ), [[2, 9],
                                                                    [10, 12],
                                                                    [14, 18]]),
        (([[3, 5], [6, 9], [17, 100], [2000, 2004]], ), [[3, 5], [6, 9],
                                                         [17, 100],
                                                         [2000, 2004]]),
        (([[3, 5], [4, 5], [3, 5], [2, 4], [5, 5], [2, 3], [4, 5]], ), [[2,
                                                                         5]]),
    ]
    s = Solution()
    test_fixture(s.merge, data)
Ejemplo n.º 15
0
def test():
    data = [
        (("a", ), True),
        (("aa", ), True),
        (("ab", ), True),
        (("abca", ), True),
        (("abc", ), False),
        (("aba", ), True),
        (("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
          ), True),
        (("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb",
          ), False),
        (("yd", ), True),
        # ((,), ),
        # ((,), ),
    ]
    s = Solution()
    test_fixture(s.validPalindrome, data)
Ejemplo n.º 16
0
def test():
    data = [
        (("abc", ), "abc"),
        (("3[ab]", ), "ababab"),
        (("10[a]b", ), "aaaaaaaaaab"),
        (("2[2[ab]]", ), "abababab"),
        (("3[a10[bc]]", ),
         "abcbcbcbcbcbcbcbcbcbcabcbcbcbcbcbcbcbcbcbcabcbcbcbcbcbcbcbcbcbc"),
        (("a2[2[b]c]n", ), "abbcbbcn"),
        (("3[a]2[bc]", ), "aaabcbc"),
        (("2[abc]3[cd]ef", ), "abcabccdcdcdef"),
        (("3[a2[c]]", ), "accaccacc"),
        (("3[2[c]]", ), "cccccc"),
        (("abc3[cd]xyz", ), "abccdcdcdxyz"),
        (("ab2[cd3[e]]f2[g]xyz", ), "abcdeeecdeeefggxyz"),
        (("3[a]2[bc]ef3[2[ab2[xy]n]]ww", ),
         "aaabcbcefabxyxynabxyxynabxyxynabxyxynabxyxynabxyxynww"),
        (("3[a10[bc]]2[d]e3[2[ab2[xy]n]]w", ),
         "abcbcbcbcbcbcbcbcbcbcabcbcbcbcbcbcbcbcbcbcabcbcbcbcbcbcbcbcbcbcddeabxyxynabxyxynabxyxynabxyxynabxyxynabxyxynw"
         ),
    ]
    S = Solution()
    test_fixture(S.decodeString, data)
Ejemplo n.º 17
0
def test_pwddb():
    users = {
        'Bob': '1234',
        'John': 'big_joke@me',
        'Stephanie': 'you guess',
    }
    pwdDb = PwdDB()
    pwdDb.set_user('Bob', '0000')
    for k, v in users.items():
        pwdDb.set_user(k, v)
    data = {
        ((
            'Bob',
            '1234',
        ), True),
        (('John', 'big_joke@me'), True),
        ((
            'Stephanie',
            'you guess',
        ), True),
        (('NoName', 'Invalid pass'), False),
        (('Bob', 'Wrong password'), False),
    }
    test_fixture(pwdDb.check_pwd, data)