Esempio n. 1
0
    def test3(self):
        node1 = ListNode(1)
        node2 = ListNode(2)
        node3 = ListNode(3)
        node4 = ListNode(4)
        node5 = ListNode(5)
        node6 = ListNode(6)
        node7 = ListNode(7)
        node8 = ListNode(8)
        node9 = ListNode(9)
        node10 = ListNode(10)
        node11 = ListNode(11)
        node12 = ListNode(12)
        node13 = ListNode(13)
        node13 = ListNode(13)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        node5.next = node6
        node6.next = node1

        node7.next = node8
        node8.next = node9
        node9.next = node10
        node10.next = node11
        node11.next = node12
        node12.next = node13
        node12.next = node7

        s = Solution()
        res = s.is_interset(node1, node7)
        print res
        self.assertFalse(res)
Esempio n. 2
0
def test_0():
    sol = Solution()
    assert sol.longestValidParentheses('') == 0
    assert sol.longestValidParentheses('(') == 0
    assert sol.longestValidParentheses(')') == 0
    assert sol.longestValidParentheses('()') == 2
    assert sol.longestValidParentheses(')(') == 0
Esempio n. 3
0
 def test1(self):
     array = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
     k = 5
     exp = 5
     sol = Solution()
     res = sol.kth_smallset(array, k)
     self.assertEqual(res, exp)
Esempio n. 4
0
def test_2():
    sol = Solution()
    matrix = [['1', '1', '1', '0', '0'],
              ['1', '1', '1', '1', '1'],
              ['1', '1', '1', '1', '1'],
              ['1', '0', '0', '1', '0']]
    assert sol.maximalSquare(matrix) == 9
Esempio n. 5
0
def test_0():
    sol = Solution()
    assert sol.uniquePaths(0, 0) == 0
    assert sol.uniquePaths(2, 0) == 0
    assert sol.uniquePaths(0, 2) == 0
    assert sol.uniquePaths(1, 3) == 1
    assert sol.uniquePaths(3, 1) == 1
Esempio n. 6
0
def test_1():
    sol = Solution()
    triangle = [[2],
                [3, 4],
                [6, 5, 7],
                [4, 1, 8, 3]]
    assert sol.minimumTotal(triangle) == 11
Esempio n. 7
0
 def test1(self):
     s = Solution()
     input_str = 'aa'
     p = 'a'
     res = s.isMatch(input_str, p)
     print res
     self.assertFalse(res)
Esempio n. 8
0
 def test4(self):
     s = Solution()
     input_str = 'aaaaaaaaaaaaab'
     p = 'a*a*a*a*a*a*a*a*a*a*c'
     res = s.isMatch(input_str, p)
     print res
     self.assertFalse(res)
Esempio n. 9
0
 def test3(self):
     s = Solution()
     input_str = 'aab'
     p = 'c*a*b'
     res = s.isMatch(input_str, p)
     print res
     self.assertTrue(res)
Esempio n. 10
0
 def test1(self):
     s = Solution()
     node1 = TreeNode(1)
     node2 = TreeNode(2)
     node1.left = node2
     root = s.recoverTree(node1)
     self._inorder(root)
Esempio n. 11
0
def test_2():
    sol = Solution()
    matrix = [['1', '1', '1', '0', '0'],
              ['1', '1', '1', '1', '1'],
              ['1', '1', '1', '1', '1'],
              ['1', '0', '0', '1', '0']]
    assert sol.maximalRectangle(matrix) == 10
Esempio n. 12
0
def test_2():
    sol = Solution()
    m = n = 50
    for _ in xrange(5):
        matrix = np.random.randint(-100, 100, (m, n)).tolist()
        k = np.random.randint(-400, 400)
        assert sol.maxSumSubmatrix(matrix, k) == maxSumSubmatrix(matrix, k)
Esempio n. 13
0
    def test1(self):
        node1 = Node(1)
        node2 = Node(2)
        node3 = Node(3)
        node4 = Node(4)
        node5 = Node(5)
        node6 = Node(6)
        node7 = Node(7)
        node8 = Node(8)
        node9 = Node(9)
        node10 = Node(10)
        node11 = Node(11)
        node1.left = node2
        node1.right = node3
        node2.left = node4
        node2.right = node5
        node3.left = node6
        node3.right = node7
        node6.left = node8
        node6.right = node9
        node7.left = node10
        node7.right = node11

        s = Solution()
        res = s.compress(node1)
        print res
        root = s.decompress(res)
        res2 = s.compress(root)
        print res2
        self.assertEqual(res, res2)
Esempio n. 14
0
def test_1():
    sol = Solution()
    for n in xrange(1, 100):
        a = np.random.choice(100, n).tolist()
        b = sorted(a)
        k = 1 + np.random.randint(n)
        assert sol.findKthLargest(a, k) == b[-k]
Esempio n. 15
0
class TestCase(unittest.TestCase):

    def setUp(self):
        self.solution = Solution()

    def testSolution01(self):
        a = (1, 3, 5, 7, 9, 11)
        b = (2, 4, 6, 8, 10)
        self.assertEqual(self.solution.findMedianSortedArrays(a, b), 6)

    def testSolution02(self):
        a = (1, 3, 4, 5, 7, 9)
        b = (2, 6, 8, 10)
        self.assertEqual(self.solution.findMedianSortedArrays(a, b), 5.5)

    def testSolution03(self):
        a = (1, 2, 3, 4, 5, 9)
        b = (6, 7, 8, 10, 11)
        self.assertEqual(self.solution.findMedianSortedArrays(a, b), 6)

    def testSolution04(self):
        a = (1, 2, 3, 4, 5, 6)
        b = (7, 8, 9, 10, 11)
        self.assertEqual(self.solution.findMedianSortedArrays(a, b), 6)

    def testSolution05(self):
        a = (1, 4, 8, 9, 10, 11)
        b = (2, 3, 5, 6, 7)
        self.assertEqual(self.solution.findMedianSortedArrays(a, b), 6)
Esempio n. 16
0
def test_1():
    sol = Solution()
    intervals = [Interval(1, 2), Interval(3, 5), Interval(6, 7),
                 Interval(8, 10), Interval(12, 16)]
    new = Interval(4, 9)
    ans = [Interval(1, 2), Interval(3, 10), Interval(12, 16)]
    assert sol.insert(intervals, new) == ans
Esempio n. 17
0
def test_1():
    sol = Solution()
    buildings = [[2, 9, 10], [3, 7, 15], [5, 12, 12],
                 [15, 20, 10], [19, 24, 8]]
    tgt = [[2, 10], [3, 15], [7, 12], [12, 0],
           [15, 10], [20, 8], [24, 0]]
    assert sol.getSkyline(buildings) == tgt
Esempio n. 18
0
 def test5(self):
     s = Solution()
     beginWord = "a"
     endWord = "c"
     wordDict = set(["a", "b", "c"])
     res = s.findLadders(beginWord, endWord, wordDict)
     print res
Esempio n. 19
0
 def test1(self):
     s = Solution()
     beginWord = "hit"
     endWord = "cog"
     wordDict = set(["hot","dot","dog","lot","log"])
     res = s.findLadders(beginWord, endWord, wordDict)
     print res
Esempio n. 20
0
 def test001(self):
     s = Solution()
     numbers = [5,2,5,1,4,3,6,4,3,2,6]
     n = s.singleNumber(numbers)
     print "input:\t", numbers
     print "expect:\t", 1
     print "output:\t", n
Esempio n. 21
0
    def test2(self):
        head = Node()
        head.buff = [1, 2, 3]
        head.count = 3

        n1 = Node()
        n1.buff = [4]
        n1.count = 1

        n2 = Node()
        n2.buff = [5, 6]
        n2.count = 2

        head.next = n1
        n1.next = n2

        sol = Solution()
        res = sol.get_idx_at(head, 0)
        exp = 1
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 1)
        exp = 2
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 4)
        exp = 5
        self.assertEqual(res, exp)

        res = sol.get_idx_at(head, 5)
        exp = 6
        self.assertEqual(res, exp)
Esempio n. 22
0
    def test_parameter_types(self):
        s = Solution('test_data/example/info.yaml')
        params = s.get_parameter_types(None)

        self.assertEqual(len(params), 5)

        self.assertEqual(params[0]['name'], 'floating-network-id')
        self.assertEqual(params[0]['type'], 'comma_delimited_list')
        self.assertIn(params[0]['default'],
                      params[0]['constraints'][0]['allowed_values'])
        self.assertIn('_mapping', params[0])
        self.assertEqual(params[0]['_mapping'], {'first_network': '11',
                                                 'second_network': '22',
                                                 'third_network': '33'})
        self.assertEqual(s.map_parameter(params, 'floating-network-id',
                                         'second_network'), '22')

        self.assertEqual(params[1]['name'], 'flavor')
        self.assertEqual(params[1]['type'], 'comma_delimited_list')
        self.assertIn(params[1]['default'], 'm1.small')

        self.assertEqual(params[2]['name'], 'image')
        self.assertEqual(params[2]['type'], 'comma_delimited_list')
        self.assertIn(params[2]['default'],
                      params[2]['constraints'][0]['allowed_values'])

        self.assertEqual(params[3]['name'], 'image-count')
        self.assertEqual(params[3]['type'], 'number')

        self.assertEqual(params[4]['name'], 'keyname')
        self.assertEqual(params[4]['type'], 'comma_delimited_list')
        self.assertIn(params[4]['default'],
                      params[4]['constraints'][0]['allowed_values'])
Esempio n. 23
0
def test_1():
    sol = Solution()
    assert sol.findMin([0, 1, 2, 3, 4, 5, 6, 7]) == 0
    assert sol.findMin([7, 0, 1, 2, 3, 4, 5, 6]) == 0
    assert sol.findMin([6, 7, 0, 1, 2, 3, 4, 5]) == 0
    assert sol.findMin([5, 6, 7, 0, 1, 2, 3, 4]) == 0
    assert sol.findMin([4, 5, 6, 7, 0, 1, 2]) == 0
Esempio n. 24
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))
Esempio n. 25
0
def test_0():
    sol = Solution()
    assert sol.minSubArrayLen(1, []) == 0
    assert sol.minSubArrayLen(1, [1]) == 1
    assert sol.minSubArrayLen(1, [2]) == 1
    assert sol.minSubArrayLen(2, [2, 1, 1]) == 1
    assert sol.minSubArrayLen(3, [1, 1]) == 0
Esempio n. 26
0
def test_0():
    sol = Solution()
    assert not sol.isInterleave('', '', 'a')
    assert sol.isInterleave('', 'bc', 'bc')
    assert not sol.isInterleave('', 'cb', 'bc')
    assert sol.isInterleave('a', 'b', 'ab')
    assert sol.isInterleave('b', 'a', 'ab')
Esempio n. 27
0
def test_0():
    sol = Solution()
    assert sol.uniquePathsWithObstacles([]) == 0
    assert sol.uniquePathsWithObstacles([[0, 0, 0]]) == 1
    assert sol.uniquePathsWithObstacles([[0, 1, 0]]) == 0
    assert sol.uniquePathsWithObstacles([[0], [0], [0]]) == 1
    assert sol.uniquePathsWithObstacles([[0], [1], [0]]) == 0
Esempio n. 28
0
 def test2(self):
     A = [-50,-50,-49,-48,-47,-47,-47,-46,-45,-43,-42,-41,-40,-40,-40,-40,-40,-40,-39,-38,-38,-38,-38,-37,-36,-35,-34,-34,-34,-33,-32,-31,-30,-28,-27,-26,-26,-26,-25,-25,-24,-24,-24,-22,-22,-21,-21,-21,-21,-21,-20,-19,-18,-18,-18,-17,-17,-17,-17,-17,-16,-16,-15,-14,-14,-14,-13,-13,-12,-12,-10,-10,-9,-8,-8,-7,-7,-6,-5,-4,-4,-4,-3,-1,1,2,2,3,4,5,6,6,7,8,8,9,9,10,10,10,11,11,12,12,13,13,13,14,14,14,15,16,17,17,18,20,21,22,22,22,23,23,25,26,28,29,29,29,30,31,31,32,33,34,34,34,36,36,37,37,38,38,38,39,40,40,40,41,42,42,43,43,44,44,45,45,45,46,47,47,47,47,48,49,49,49,50]
     output_list = [-50,-50,-49,-48,-47,-47,-46,-45,-43,-42,-41,-40,-40,-39,-38,-38,-37,-36,-35,-34,-34,-33,-32,-31,-30,-28,-27,-26,-26,-25,-25,-24,-24,-22,-22,-21,-21,-20,-19,-18,-18,-17,-17,-16,-16,-15,-14,-14,-13,-13,-12,-12,-10,-10,-9,-8,-8,-7,-7,-6,-5,-4,-4,-3,-1,1,2,2,3,4,5,6,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,16,17,17,18,20,21,22,22,23,23,25,26,28,29,29,30,31,31,32,33,34,34,36,36,37,37,38,38,39,40,40,41,42,42,43,43,44,44,45,45,46,47,47,48,49,49,50]
     num_output = len(output_list)
     s = Solution()
     rtn = s.removeDuplicates(A)
     self.assertEqual(rtn, num_output)
Esempio n. 29
0
 def test1(self):
     s = Solution()
     height = [2, 4, 1, 3]
     res = s.maxArea(height)
     answer = 6
     self.assertEqual(answer, res)
     print res
Esempio n. 30
0
def test_1():
    sol = Solution()
    nums = [1, 2, 3]
    ans = [[1, 2, 3], [1, 3, 2],
           [2, 1, 3], [2, 3, 1],
           [3, 1, 2], [3, 2, 1]]
    assert sorted(ans) == sorted(sol.permute(nums))
Esempio n. 31
0
def test_solution():
    s = Solution()
    assert s.getImportance([[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1) == 11
Esempio n. 32
0
def solve(x: str, y: str):
    return Solution().findAnagrams(x, y)
Esempio n. 33
0
def test_solution():
    s = Solution()
    assert s.findMaxConsecutiveOnes([1, 1, 0, 1, 1, 1]) == 3
    assert s.findMaxConsecutiveOnes([1, 1, 1, 1, 1, 0, 1, 1, 1]) == 5
Esempio n. 34
0
 def setUp(self):
     self.pairs = []
     self.pairs.append((5, Solution().maxProfit([7, 1, 5, 3, 6, 4])))
     self.pairs.append((0, Solution().maxProfit([7, 6, 4, 3, 1])))
Esempio n. 35
0
def test_revese(test_input, expected):
    assert Solution().singleNumber(test_input) == expected
Esempio n. 36
0
def test_solution():
    s = Solution()
    assert s.isOneBitCharacter([1, 0, 0]) == True
    assert s.isOneBitCharacter([1, 1, 1, 0]) == False
Esempio n. 37
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from solution import Solution

#  n = 4
#  edges = [[1, 0], [1, 2], [1, 3]]
n = 6
edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
sol = Solution()
res = sol.findMinHeightTrees(n, edges)
print(res)
Esempio n. 38
0
# -*- coding: utf-8 -*-

from solution import Solution, ListNode

Anode1 = ListNode(1)
Anode2 = ListNode(4)
Anode3 = ListNode(5)
Anode1.next = Anode2
Anode2.next = Anode3
Bnode1 = ListNode(1)
Bnode2 = ListNode(3)
Bnode3 = ListNode(4)
Bnode1.next = Bnode2
Bnode2.next = Bnode3
Cnode1 = ListNode(2)
Cnode2 = ListNode(6)
Cnode1.next = Cnode2

lst = [Anode1, Bnode1, Cnode1]

sol = Solution()

print(sol.mergeKLists(lst))
Esempio n. 39
0
    def test_numDecodings(self):
        s = Solution()

        result = s.numDecodings("12")
        self.assertEqual(result, 2)
Esempio n. 40
0
def test_solution():
    s = Solution()
    assert sorted(s.letterCombinations('23')) == sorted(["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"])
Esempio n. 41
0
from solution import Solution
s = Solution()
assert s.lengthOfLongestSubstring("helloworld") == 5
assert s.lengthOfLongestSubstring("dvdf") == 3
assert s.lengthOfLongestSubstring("abcabcbb") == 3
Esempio n. 42
0
def test_solution():
    nums = [3, 5, 8]
    s = Solution()
    print(s.sortedArrayToBST(nums))
Esempio n. 43
0
    def test_missingNumberXOR_short(self):
        s = Solution()

        actual = s.missingNumber([3, 0, 1])

        self.assertEqual(actual, 2)
Esempio n. 44
0
    def test_missingNumber(self):
        s = Solution()

        actual = s.missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1])

        self.assertEqual(actual, 8)
Esempio n. 45
0
 def test_1(self):
     self.assertEqual(Solution().search([4, 5, 6, 7, 0, 1, 2], 3), -1)
Esempio n. 46
0
from functions import *
from solution import Solution
from algorithms import *

s = Solution(2, -5.12, 5.12, 0.1, sphere, hill_climbing)
print(s.find_minimum(aargs=(100, 10)))
s.save_anim()
Esempio n. 47
0
 def test_193(self):
     self.assertEqual(Solution().search([4, 5, 6, 7, 8, 1, 2, 3], 8), 4)
Esempio n. 48
0
 def test_sample_1(self):
     self.assertEqual(Solution().search([4, 5, 6, 7, 0, 1, 2], 0), 4)
Esempio n. 49
0
 def test_186(self):
     self.assertEqual(Solution().search([5, 1, 3], 5), 0)
Esempio n. 50
0
 def test_191(self):
     self.assertEqual(Solution().search([6, 7, 8, 1, 2, 3, 4, 5], 6), 0)
Esempio n. 51
0
 def test_170(self):
     self.assertEqual(Solution().search([1, 3], 3), 1)
Esempio n. 52
0
 def test_181(self):
     self.assertEqual(Solution().search([3, 1], 3), 0)
Esempio n. 53
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from solution import Solution

strs = ["", "", "eat", "tea", "tan", "ate", "nat", "bat"]
sol = Solution()
res = sol.groupAnagrams(strs)
print(res)
Esempio n. 54
0
 def test_168(self):
     self.assertEqual(Solution().search([1, 3], 1), 0)
Esempio n. 55
0
import sys

import graph_tool.all as gt
import numpy as np

from solution import Solution, Neighbourhood

f_p = sys.argv[1]

from grasp import grasp

per = 0.5

sol = Solution(lambda x: per * x, f_p)
sol2 = sol.copy()
sol3 = sol.copy()

vertices = [0, 9, 4, 3]
sol.batch_vertex_upgrade(vertices, update_mst=True)

for v in vertices:
    sol2.upgrade_vertex(v)

state = np.zeros(sol3.globals.N, dtype=bool)
state[vertices] = True
sol3.cleanse_to_state(state)

assert sol.edge_upgrade_level.__str__() == sol2.edge_upgrade_level.__str__(), \
        "both approaches should take you to the same solution"

assert sol.edge_upgrade_level.__str__() == sol3.edge_upgrade_level.__str__(), \
Esempio n. 56
0
 def test_longestCommonPrefix(self):
     s = Solution()
     self.assertEqual(s.longestCommonPrefix(["flower", "flow", "flight"]),
                      "fl")
Esempio n. 57
0
 def test_empty_input_list(self):
     self.assertEquals(0, Solution().maxProfit([]))
Esempio n. 58
0
 def test_length_one_list(self):
     self.assertEquals(0, Solution().maxProfit([2]))
Esempio n. 59
0
    def test_numDecodings_longer(self):
        s = Solution()

        result = s.numDecodings("226")
        self.assertEqual(result, 3)
Esempio n. 60
0
        q.append(left)
        idx += 1
        if idx == len(tree):
            break
        right = constructOne(tree[idx])
        idx += 1
        tn.right = right
        q.append(right)
    return root

def printNode(tn, indent):
    sb = ""
    for i in range(indent):
        sb += "\t"
    sb += str(tn.val)
    print(sb)

def printTree(root, indent):
    if not root:
        return
    printTree(root.right, indent + 1)
    printNode(root, indent)
    printTree(root.left, indent + 1)

n = 3
sol = Solution()
res = sol.generateTrees(n)
for tree in res:
    printTree(tree, 0)
    print("-----------------")