def test_minOperations(self): solution = Solution() cases = [[["d1/", "d2/", "../", "d21/", "./"], 2], [["d1/", "d2/", "./", "d3/", "../", "d31/"], 3], [["d1/", "../", "../", "../"], 0], [["./", "../", "./"], 0]] for logs, expected in cases: assert solution.minOperations(logs) == expected
class SolutionTest(unittest.TestCase): def setUp(self): self.sol = Solution() def test_1(self): self.assertEqual( self.sol.searchRange([5, 7, 7, 8, 8, 10], 8), [3, 4] ) def test_2(self): self.assertEqual( self.sol.searchRange([5, 7, 7, 8, 8, 10], 6), [-1, -1] ) def test_3(self): self.assertEqual( self.sol.searchRange([1], 1), [0, 0] ) def test_4(self): self.assertEqual( self.sol.searchRange([2, 2], 3), [-1, -1] )
def test_longestCommonPrefix(self): solution = Solution() cases = [[["flower", "flow", "flight"], "fl"], [["dog", "racecar", "car"], ""], [[], ""], [[""], ""], [["a"], "a"]] for args, expected in cases: assert solution.longestCommonPrefix(args) == expected
def test_multithread_write(self): """ Test multithread writing to database :return: None """ def thread_task(solution, action): solution.add_action(action) test_solution = Solution() thread_1 = threading.Thread(target=thread_task, args=(test_solution, '{"action":"jump", "time":100}')) thread_2 = threading.Thread(target=thread_task, args=(test_solution, '{"action":"run", "time":75}')) thread_1.start() thread_2.start() thread_1.join() thread_2.join() stats = test_solution.get_stats() assert len(stats) > 1
def test_merge(self): solution = Solution() cases = [ [[1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3, [1, 2, 2, 3, 5, 6]] ] for nums1, m, nums2, n, expected in cases: solution.merge(nums1, m, nums2, n) assert nums1 == expected
def test_case2(self): """ Check if the encrypt_text() runs as expected """ solution = Solution() self.assertEqual( solution.encrypt_text( "The wizard quickly jinxed the gnomes before they vaporized."), "- .... . .-- .. --.. .- .-. -.. --.- ..- .. -.-. -.- .-.. -.-- .--- .. -. -..- . -.. - .... . --. -. --- -- . ... -... . ..-. --- .-. . - .... . -.-- ...- .- .--. --- .-. .. --.. . -.. .-.-.- " )
def testProductSum(self): test = Solution() nums = [1,2,3,4,5] actual = [120,60,40,30,1] expected = test.productArrayExceptItself(nums) self.assertEqual(expected,actual)
def test_removeElement(self): solution = Solution() cases = [ [[3, 2, 2, 3], 3, 2], [[0, 1, 2, 2, 3, 0, 4, 2], 2, 5], ] for nums, val, expected in cases: assert solution.removeElement(nums, val) == expected
def test_case1(self): """ Check if the entire program runs as expected """ solution = Solution() self.assertEqual( solution.run( True, "- .... . .-- .. --.. .- .-. -.. --.- ..- .. -.-. -.- .-.. -.-- .--- .. -. -..- . -.. - .... . --. -. --- -- . ... -... . ..-. --- .-. . - .... . -.-- ...- .- .--. --- .-. .. --.. . -.. .-.-.-" ), "the wizard quickly jinxed the gnomes before they vaporized.")
class SolutionTest(unittest.TestCase): def setUp(self): self.sol = Solution() def test_1(self): self.assertEqual(self.sol.search([4, 5, 6, 7, 0, 1, 2], 0), 4) def test_2(self): self.assertEqual(self.sol.search([4, 5, 6, 7, 0, 1, 2], 3), -1)
def test_rotate_dice_wisely(): d = Dice(1, 4, 2) r = Solution().rotate_dice_wisely([0, 0], [8, 8], d) assert r[0], r[1] == (8, 8) assert r[2].state == (1, 4, 2) r = Solution().rotate_dice_wisely([8, 0], [0, 8], d) assert r[0], r[1] == (8, 8) assert r[2].state == (1, 2, 3)
def test_containsDuplicate(self): solution = Solution() cases = [ [[1, 2, 3, 1], True], [[1, 2, 3, 4], False], [[1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True] ] for nums, expected in cases: assert solution.containsDuplicate(nums) == expected
def test_climbStairs(self): solution = Solution() cases = [ [2, 2], [3, 3], [5, 8], ] for n, expected in cases: assert solution.climbStairs(n) == expected
class SolutionTest(unittest.TestCase): def setUp(self): self.sol = Solution() def test_1(self): self.assertEqual(self.sol.longestValidParentheses("(()"), 2) def test_2(self): self.assertEqual(self.sol.longestValidParentheses(")()())"), 4)
def test_twoSum(self): solution = Solution() cases = [ [[2, 7, 11, 15], 9, [0, 1]], [[3, 2, 4], 6, [1, 2]], #[[3, 3], 6, [0, 1]], ] for arg, target, expected in cases: assert solution.twoSum(arg, target) == expected
class SolutionTest(unittest.TestCase): def setUp(self): self.sol = Solution() def test_1(self): self.assertEqual(self.sol.strStr("hello", "ll"), 2) def test_2(self): self.assertEqual(self.sol.strStr("aaaaa", "bba"), -1)
def test_runningSum(self): solution = Solution() cases = [ [[1, 2, 3, 4], [1, 3, 6, 10]], [[1, 1, 1, 1, 1], [1, 2, 3, 4, 5]], [[3, 1, 2, 10, 1], [3, 4, 6, 16, 17]], ] for nums, expected in cases: assert solution.runningSum(nums) == expected
def test_reverse(self): solution = Solution() cases = [ [123, 321], [-123, -321], [120, 21], [1534236469, 0] ] for arg, expected in cases: assert solution.reverse(arg) == expected
def test_specialArray(self): solution = Solution() cases = [ [[3, 5], 2], [[0, 0], -1], [[0, 4, 3, 0, 4], 3], [[3, 6, 7, 7, 0], -1], ] for nums, expected in cases: assert solution.specialArray(nums) == expected
def test_add(self): """ Test add function creates new item in database. :return: None """ test_solution = Solution() action_item = '{"action":"jump", "time":100}' test_solution.add_action(action_item) assert test_solution.database != {}
def test_get_stats(self): """ Test get_stats is a string formatted json object with correct items. :return: None """ test_solution = Solution() action_item = '{"action":"jump", "time":100}' test_solution.add_action(action_item) action_item = '{"action":"jump", "time":200}' test_solution.add_action(action_item) action_item = '{"action":"run", "time":75}' test_solution.add_action(action_item) stats = test_solution.get_stats() assert type(stats) == str stats_json = json.loads(stats) assert type(stats_json) == list for stat in stats_json: if "action" in stat and stat["action"] == "jump": assert stat["avg"] == 150 elif "action" in stat and stat["action"] == "run": assert stat["avg"] == 75
class SolutionTest(unittest.TestCase): def setUp(self): self.sol = Solution() def test_1(self): self.assertEqual( self.sol.longestCommonPrefix(["flower", "flow", "flight"]), "fl") def test_2(self): self.assertEqual( self.sol.longestCommonPrefix(["dog", "racecar", "car"]), "")
def test_kidsWithCandies(self): solution = Solution() cases = [ [[2, 3, 5, 1, 3], 3, [True, True, True, False, True]], [[4, 2, 1, 1, 2], 1, [True, False, False, False, False]], [[12, 1, 12], 10, [True, False, True]], [[2, 8, 7], 1, [False, True, True]], ] for candies, extraCandies, expected in cases: assert solution.kidsWithCandies(candies, extraCandies) == expected
def test_romanToInt(self): solution = Solution() cases = [ ["III", 3], ["IV", 4], ["IX", 9], ["LVIII", 58], ["MCMXCIV", 1994], ] for arg, expected in cases: assert solution.romanToInt(arg) == expected
def test_findABSteps(): r = Solution().findABSteps([0, 0], [8, 8], Dice(1, 4, 2)) assert r == 45 r = Solution().findABSteps([3, 3], [0, 0], Dice(1, 4, 2)) assert r == 12 r = Solution().findABSteps([3, 3], [0, 0], Dice(1, 4, 2).rotate_clockwise().rotate_clockwise()) assert r == 10
def test_solution_sum_solution(): sol = Solution() x = [ -4, 3, 4, -7, 1, 6, ] result = sol.max_sub_array(x) assert result == 7
class SolutionTest(unittest.TestCase): def setUp(self): self.sol = Solution() def test_1(self): self.assertEqual(self.sol.divide(10, 3), 3) def test_2(self): self.assertEqual(self.sol.divide(7, -3), -2) def test_3(self): self.assertEqual(self.sol.divide(-5, 2), -2)
def setup_test_case_two(): list_one = LinkedList() list_one.push(0) list_two = LinkedList() list_two.push(0) solution = Solution() result_list = (solution.add_two_numbers(list_one, list_two)) return result_list
def test_addTwoNumbers(self): solution = 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) lsum = ListNode(7) lsum.next = ListNode(0) lsum.next.next = ListNode(8) solution.addTwoNumbers(l1, l2) == lsum
def test(self): n = random.randint(0, 16) k = random.randint(0, n) print('n', n, 'k', k) s = Solution() result = s.combine(n, k) self.assertEqual(len(result), CombineTest.cnk(n, k)) for array in result: self.assertFalse(CombineTest.is_number_repeated(array)) for i in range(len(result)): for j in range(i): self.assertNotEqual(set(result[i]), set(result[j]))
#!/usr/bin/env python # encoding: utf-8 from main import Solution solution = Solution() s = "" t = "" print solution.isAnagram(s, t)
#!/usr/bin/env python # encoding: utf-8 from main import Solution solution = Solution() n = 230 count = solution.countDigitOne(n) print count
#!/usr/bin/env python # encoding: utf-8 from main import Solution solution = Solution() array = [2,3,2,3,5,3,2] majority = solution.majorityElement(array) print majority
#!/usr/bin/env python # encoding: utf-8 from main import Solution solution = Solution() test = [9, 8, 6, 55, 34, 12] # test = [2, 1] print test result = solution.rob(test) print result
#!/usr/bin/env python # encoding: utf-8 from main import Solution num = 1 solution = Solution() print solution.isHappy(num)