def main(): matrix = [[1, 3, 5, 7]] equal(Solution().searchMatrix(matrix, 3), True) matrix = [[22, 2, 2, 2, 2, 2]] equal(Solution().searchMatrix(matrix, 3), False) matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]] equal(Solution().searchMatrix(matrix, 16), True) matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]] equal(Solution().searchMatrix(matrix, 3), True) matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]] equal(Solution().searchMatrix(matrix, 13), False)
def simplify_items(items): simplified = copy.deepcopy(items) for itemid in simplified: item = simplified[itemid] for prop in simplify_item_props: if prop in item: del item[prop] for prop in sorted(item): if util.equal(item[prop], ''): del item[prop] return simplified
def main(): board = [["5", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], [".", "9", "8", ".", ".", ".", ".", "6", "."], ["8", ".", ".", ".", "6", ".", ".", ".", "3"], ["4", ".", ".", "8", ".", "3", ".", ".", "1"], ["7", ".", ".", ".", "2", ".", ".", ".", "6"], [".", "6", ".", ".", ".", ".", "2", "8", "."], [".", ".", ".", "4", "1", "9", ".", ".", "5"], [".", ".", ".", ".", "8", ".", ".", "7", "9"]] Solution().solveSudoku(board) expected_board = [["5", "3", "4", "6", "7", "8", "9", "1", "2"], ["6", "7", "2", "1", "9", "5", "3", "4", "8"], ["1", "9", "8", "3", "4", "2", "5", "6", "7"], ["8", "5", "9", "7", "6", "1", "4", "2", "3"], ["4", "2", "6", "8", "5", "3", "7", "9", "1"], ["7", "1", "3", "9", "2", "4", "8", "5", "6"], ["9", "6", "1", "5", "3", "7", "2", "8", "4"], ["2", "8", "7", "4", "1", "9", "6", "3", "5"], ["3", "4", "5", "2", "8", "6", "1", "7", "9"]] equal(board, expected_board)
def main(): board = [['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']] word = "ABCCED" equal(Solution().exist(board, word), True) board = [['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']] word = "SEE" equal(Solution().exist(board, word), True) board = [['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']] word = "ABCB" equal(Solution().exist(board, word), False) board = [["b", "a", "a", "b", "a", "b"], ["a", "b", "a", "a", "a", "a"], ["a", "b", "a", "a", "a", "b"], ["a", "b", "a", "b", "b", "a"], ["a", "a", "b", "b", "a", "b"], ["a", "a", "b", "b", "b", "a"], ["a", "a", "b", "a", "a", "b"]] word = "aabbbbabbaababaaaabababbaaba" equal(Solution().exist(board, word), True) board = [["C", "A", "A"], ["A", "A", "A"], ["B", "C", "D"]] word = "AAB" equal(Solution().exist(board, word), True)
def main(): equal(Solution().sortArrayByParity([0, 1]), [0, 1]) equal(Solution().sortArrayByParity([3, 1, 2, 4]), [4, 2, 1, 3])
def main(): equal(Solution().search([4, 5, 6, 7, 0, 1, 2], 6), 2) equal(Solution().search([4, 5, 6, 7, 0, 1, 2], 4), 0) equal(Solution().search([4, 5, 6, 7, 0, 1, 2], 2), 6) equal(Solution().search([4, 5, 6, 7, 0, 1, 2], 3), -1) equal(Solution().search([4, 5, 6, 7, 0, 1, 2], 0), 4) equal(Solution().search([5, 1, 3], 2), -1) equal(Solution().search([5, 1, 3], 3), 2)
state = [[0 for i in range(len(p) + 1)] for j in range(len(s) + 1)] state[0][0] = 1 for i in range(1, len(p) + 1): if p[i - 1] == '*': if i >= 2: state[0][i] = state[0][i - 2] for i in range(1, len(s) + 1): for j in range(1, len(p) + 1): if p[j - 1] == '.': state[i][j] = state[i - 1][j - 1] elif p[j - 1] == '*': state[i][j] = state[i][j - 1] or state[i][j - 2] or ( state[i - 1][j] and (s[i - 1] == p[j - 2] or p[j - 2] == '.')) else: state[i][j] = state[i - 1][j - 1] and s[i - 1] == p[j - 1] return state[len(s)][len(p)] == 1 if __name__ == '__main__': from util import equal t = Solution() print('start') equal(t.isMatch("", "a"), False) equal(t.isMatch("aa", ""), False) equal(t.isMatch("aa", "a"), False) equal(t.isMatch("aa", "a*"), True) equal(t.isMatch("a", "ab*a"), True) equal(t.isMatch("aab", "c*a*b"), True) equal(t.isMatch("mississippi", "mis*is*p*."), False)
def main(): equal(Solution().findMin([5, 1, 3]), 1) equal(Solution().findMin([1, 3, 5]), 1) equal(Solution().findMin([2, 2, 2, 2, 2, 0, 1]), 0) equal(Solution().findMin([1, 3, 3]), 1)
def main(): equal(Solution().totalNQueens(4), 2)
def main(): equal(Solution().peakIndexInMountainArray([0, 1, 0]), 1) equal(Solution().peakIndexInMountainArray([0, 2, 1, 0]), 1)
def main(): equal(Solution().sortArrayByParityII([0, 1]), [0, 1]) equal(Solution().sortArrayByParityII([3, 4]), [4, 3]) equal(Solution().sortArrayByParityII([4, 2, 5, 7]), [4, 5, 2, 7])
def main(): equal(Solution().search([3, 1], 3), True) equal(Solution().search([1, 3], 3), True) equal(Solution().search([1, 1], 1), True) equal(Solution().search([2], 0), False) equal(Solution().search([], 0), False) equal(Solution().search([2, 5, 6, 0, 0, 1, 2], 0), True) equal(Solution().search([2, 5, 6, 0, 0, 1, 2], 3), False)
def main(): equal(Solution().findMin([3, 4, 5, 1, 2]), 1) equal(Solution().findMin([4, 5, 6, 7, 0, 1, 2]), 0)