Exemplo n.º 1
0
 def test_permutations(self):
     test([1, 2, 3],
          result=[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2],
                  [3, 2, 1]])
     test([1], result=[[1]])
     test([], result=[[]])
     test([1, 1, 2], result=[[1, 1, 2], [1, 2, 1], [2, 1, 1]])
     test([2, 2, 1, 1],
          result=[[1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2],
                  [2, 1, 2, 1], [2, 2, 1, 1]])
Exemplo n.º 2
0
 def test_two_sum1(self) -> None:
     test([2, 7, 11, 15], 9, result=[0, 1])
     test([-1, 0, 9, -5], -6, result=[0, 3])
     test([0, 0], 0, result=[0, 1])
Exemplo n.º 3
0
 def test_add_two_numbers(self) -> None:
     test(List([2, 4, 3]).root,
          List([5, 6, 4]).root,
          result=List([7, 0, 8]).root)
     test(List([2, 4, 3]).root, List([0]).root, result=List([2, 4, 3]).root)
     test(List([0]).root, List([5, 6, 4]).root, result=List([5, 6, 4]).root)
     test(List([1, 1, 1]).root,
          List([2, 3]).root,
          result=List([3, 4, 1]).root)
     test(List([1, 1, 1]).root,
          List([9, 8, 8, 9]).root,
          result=List([0, 0, 0, 0, 1]).root)
     test(List([5]).root, List([6]).root, result=List([1, 1]).root)
     test(List([0]).root, List([0]).root, result=List([0]).root)
Exemplo n.º 4
0
 def test_add_two_numbers2(self) -> None:
     test(List([2, 4, 3]).root,
          List([5, 6, 4]).root,
          result=List([8, 0, 7]).root)
     test(List([2, 4, 3]).root, List([0]).root, result=List([2, 4, 3]).root)
     test(List([0]).root, List([5, 6, 4]).root, result=List([5, 6, 4]).root)
     test(List([1, 1, 1]).root,
          List([2, 3]).root,
          result=List([1, 3, 4]).root)
     test(List([1, 1, 1]).root,
          List([9, 8, 8, 9]).root,
          result=List([1, 0, 0, 0, 0]).root)
     test(List([5]).root, List([6]).root, result=List([1, 1]).root)
     test(List([0]).root, List([0]).root, result=List([0]).root)
Exemplo n.º 5
0
 def test_merge_two_sorted_arrays(self) -> None:
     test([1, 2, 4, 0, 0, 0],    3,  [1, 3, 4],      3,  _1=[1, 1, 2, 3, 4, 4])
     test([1, 1, 1, 1, 1, 1],    3,  [1, 1, 1],      3,  _1=[1, 1, 1, 1, 1, 1])
     test([-2, 0, 2, -1, 0, 1],  3,  [-4, -3, -1],   3,  _1=[-4, -3, -2, -1, 0, 2])
     test([1, 2, 3, 3, 2, 1],    3,  [4, 5, 6],      3,  _1=[1, 2, 3, 4, 5, 6])
     test([4, 5, 6, 0, 0, 0],    3,  [1, 2, 3],      3,  _1=[1, 2, 3, 4, 5, 6])
     test([4, 5, 6, 7, 8],       3,  [1, 2],         2,  _1=[1, 2, 4, 5, 6])
     test([1, 2, 3],             3,  [],             0,  _1=[1, 2, 3])
     test([0, 0, 0],             0,  [1, 2, 3],      3,  _1=[1, 2, 3])
     test([],                    0,  [],             0,  _1=[])
Exemplo n.º 6
0
 def test_longest_common_prefix(self) -> None:
     test(["flower", "flow", "flight"], result="fl")
     test(["dog", "racecar", "car"], result="")
     test(["flower", "flow", "slide"], result="")
     test(["flower", "flow", ""], result="")
     test(["flower", "flow"], result="flow")
     test([""], result="")
     test([], result="")
Exemplo n.º 7
0
 def test_remove_duplicates_from_sorted_array(self) -> None:
     test([1, 1, 2], result=2, _1=[1, 2])
     test([0, 0, 1, 1, 1, 2, 2, 3, 3, 4], result=5, _1=[0, 1, 2, 3, 4])
     test([1, 1, 1, 1], result=1, _1=[1])
     test([0], result=1, _1=[0])
     test([], result=0, _1=[])
Exemplo n.º 8
0
 def test_merge_two_sorted_lists(self) -> None:
     test(List([1, 2, 4]).root,  List([1, 3, 4]).root,       result=List([1, 1, 2, 3, 4, 4]).root)
     test(List([1 ,1 ,1]).root,  List([1, 1, 1]).root,       result=List([1, 1, 1, 1, 1, 1]).root)
     test(List([-2, 0, 2]).root, List([-4, -3, -1]).root,    result=List([-4, -3, -2, -1, 0, 2]).root)
     test(List([1, 2, 3]).root,  List([4, 5, 6]).root,       result=List([1, 2, 3, 4, 5, 6]).root)
     test(List([4, 5, 6]).root,  List([1, 2, 3]).root,       result=List([1, 2, 3, 4, 5, 6]).root)
     test(List([4, 5, 6]).root,  List([1, 2]).root,          result=List([1, 2, 4, 5, 6]).root)
     test(List([1, 2, 3]).root,  List([]).root,              result=List([1, 2, 3]).root)
     test(List([]).root,         List([1, 2, 3]).root,       result=List([1, 2, 3]).root)
     test(List([]).root,         List([]).root,              result=List([]).root)
Exemplo n.º 9
0
 def test_length_of_longest_substr(self) -> None:
     test('abcabcbb', result=3)
     test('bbbbb', result=1)
     test('pwwkew', result=3)
     test('abcadcbb', result=4)
     test('abcdefa', result=6)
     test('abcdefg', result=7)
     test('aab', result=2)
     test('a', result=1)
     test('', result=0)
Exemplo n.º 10
0
 def test_strstr(self) -> None:
     test("hello", "ll", result=2)
     test("aaaaa", "bba", result=-1)
     test("abbaba", "ab", result=0)
     test("abbaba", "aba", result=3)
     test("aaababa", "aaba", result=1)
     test("test", "", result=0)
     test("", "test", result=-1)
     test("", "", result=0)
Exemplo n.º 11
0
 def test_valid_parentheses(self) -> None:
     test("()",          result=True)
     test("()[]{}",      result=True)
     test("(]",          result=False)
     test("([)]",        result=False)
     test("{[]}",        result=True)
     test("",            result=True)
     test(")()",         result=False)
     test("(())((())))", result=False)
Exemplo n.º 12
0
 def test_find_disappeared_numbers(self) -> None:
     test([4, 3, 2, 7, 8, 2, 3, 1], result=[5, 6])
     test([8, 8, 8, 8, 8, 8, 8, 8], result=[1, 2, 3, 4, 5, 6, 7])
     test([4, 2, 5, 7, 8, 6, 3, 1], result=[])
     test([1, 2], result=[])
     test([2, 1], result=[])
     test([1, 1], result=[2])
     test([1], result=[])
     test([], result=[])
Exemplo n.º 13
0
    def test_max_area_of_island(self) -> None:
        test([[0, 0, 0, 0, 0]], result=0)

        test([[0, 0, 1, 1, 1], [0, 0, 0, 0, 0]], result=3)

        test([[0, 0, 1, 0, 0], [1, 1, 0, 0, 0]], result=2)

        test([[0, 0, 1, 0, 0], [1, 0, 1, 1, 0], [1, 1, 0, 0, 0]], result=3)

        test([[1, 1, 0, 0, 0], [1, 1, 0, 0, 1], [0, 0, 1, 1, 0],
              [1, 0, 0, 1, 0], [0, 0, 0, 0, 0]],
             result=4)

        test([[0, 0, 1, 0, 1], [1, 1, 1, 0, 1], [1, 0, 0, 1, 1],
              [1, 0, 0, 1, 0], [1, 1, 1, 1, 0]],
             result=15)

        test([[0, 0, 1, 0, 1], [1, 1, 1, 1, 1], [1, 0, 1, 1, 1],
              [1, 0, 0, 1, 0], [1, 1, 1, 1, 0]],
             result=17)

        test([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]],
             result=25)

        test([[1, 0, 1, 0, 1], [0, 1, 0, 1, 0], [1, 0, 1, 0, 1],
              [0, 1, 0, 1, 0], [1, 0, 1, 0, 1]],
             result=1)

        test([[1]], result=1)
Exemplo n.º 14
0
 def test_two_sum2(self) -> None:
     test([2, 7, 11, 15], 9, result=[1, 2])
     test([-1, 0, 9, -5], -6, result=[1, 4])
     test([0, 0], 0, result=[1, 2])
Exemplo n.º 15
0
 def test_remove_element(self) -> None:
     test([3, 2, 2, 3], 3, result=2, _1=[2, 2])
     test([0, 1, 2, 2, 3, 0, 4, 2], 2, result=5, _1=[0, 1, 4, 0, 3])
     test([1, 1, 1], 1, result=0, _1=[])
     test([3, 4], 0, result=2, _1=[3, 4])
     test([1], 0, result=1, _1=[1])
     test([0], 0, result=0, _1=[])
     test([], 0, result=0, _1=[])
Exemplo n.º 16
0
 def test_two_sum4(self) -> None:
     test(BinarySearchTree([5, 3, 6, 2, 4, 7]).root, 9, result=True)
     test(BinarySearchTree([5, 3, 6, 2, 4, 7]).root, 3, result=False)
     test(BinarySearchTree([2, 3, 4, 5, 6, 7]).root, 2, result=False)
     test(BinarySearchTree([2, 3, 4, 5, 6, 7]).root, 7, result=True)
     test(BinarySearchTree([2, 3, 4, 5, 6, 7]).root, 5, result=True)
     test(BinarySearchTree([2, 3, 4, 5, 6, 7]).root, 9, result=True)
     test(BinarySearchTree([2, 3, 4, 5, 6, 7]).root, 13, result=True)
     test(BinarySearchTree([7, 6, 5, 4, 3, 2]).root, 2, result=False)
     test(BinarySearchTree([7, 6, 5, 4, 3, 2]).root, 7, result=True)
     test(BinarySearchTree([7, 6, 5, 4, 3, 2]).root, 5, result=True)
     test(BinarySearchTree([7, 6, 5, 4, 3, 2]).root, 9, result=True)
     test(BinarySearchTree([7, 6, 5, 4, 3, 2]).root, 13, result=True)
     test(BinarySearchTree([0, 1]).root, 1, result=True)
     test(BinarySearchTree([5]).root, 5, result=False)
     test(BinarySearchTree([]).root, 0, result=False)
Exemplo n.º 17
0
 def test_remove_linked_list_elements(self) -> None:
     test(List([1, 2, 6, 3, 4, 5, 6]).root,
          6,
          result=List([1, 2, 3, 4, 5]).root)
     test(List([1, 1, 0, 3, 0]).root, 1, result=List([0, 3, 0]).root)
     test(List([1, 1]).root, 1, result=List([]).root)
     test(List([3, 4]).root, 1, result=List([3, 4]).root)
     test(List([1]).root, 1, result=List([]).root)
     test(List([0]).root, 1, result=List([0]).root)
     test(List([]).root, 1, result=List([]).root)
Exemplo n.º 18
0
 def test_merge_k_sorted_lists(self) -> None:
     test([List([1, 4, 5]).root, List([1, 3, 4]).root,   List([2, 6]).root],         result=List([1, 1, 2, 3, 4, 4, 5, 6]).root)
     test([List([]).root,        List([-1, 0, 1]).root,  List([8, 10, 154]).root],   result=List([-1, 0, 1, 8, 10, 154]).root)
     test([List([1, 1, 1]).root, List([1, 1, 1]).root,   List([1]).root],            result=List([1, 1, 1, 1, 1, 1, 1]).root)
     test([List([0]).root,       List([-3]).root,        List([5]).root],            result=List([-3, 0, 5]).root)
     test([List([]).root,        List([]).root,          List([8, 9, 11]).root],     result=List([8, 9, 11]).root)
     test([List([]).root,        List([]).root,          List([]).root],             result=List([]).root)
Exemplo n.º 19
0
 def test_array_pair_sum1(self) -> None:
     test([1, 4, 3, 2], result=4)
     test([0, 0], result=0)
     test([0, 0, 0, 0, 0, 0], result=0)
     test([4, 2, -2, -4], result=-2)
     test([1, -1, -1, 1], result=0)
Exemplo n.º 20
0
 def test_summary_ranges(self) -> None:
     test([0, 1, 2, 4, 5, 7],    result=['0->2', '4->5', '7'])
     test([0, 2, 3, 4, 6, 8, 9], result=['0', '2->4', '6', '8->9'])
     test([-3, -2, 0, 1, 2],     result=['-3->-2', '0->2'])
     test([-2, -1, 0, 4, 6],     result=['-2->0', '4', '6'])
     test([-1, 0, 1, 2, 4],      result=['-1->2', '4'])
     test([0, 1, 2, 3, 4, 5],    result=['0->5'])
     test([1],                   result=['1'])
     test([],                    result=[])
Exemplo n.º 21
0
 def test_search_insert_position(self):
     test([1, 3, 5, 6],      5,  result=2)
     test([1, 3, 5, 6],      2,  result=1)
     test([1, 3, 5, 6],      7,  result=4)
     test([1, 3, 5, 6],      0,  result=0)
     test([1, 3, 5, 6, 8],   3,  result=1)
     test([1, 3, 5, 6, 8],   7,  result=4)
     test([],                0,  result=0)