def tests():
    letters = ['a', 'c', 'd', 'f', 'g']
    output = contains(letters, 'a')
    assert_(expected=True, actual=output)

    output = contains(letters, 'b')
    assert_(expected=False, actual=output)
Beispiel #2
0
def tests():
    test_string = 'fedRTSersUXJ'
    output = case_specific_sorting(test_string)
    assert_(expected="deeJRSfrsTUX", actual=output)

    test_string = "defRTSersUXI"
    output = case_specific_sorting(test_string)
    assert_(expected="deeIRSfrsTUX", actual=output)
Beispiel #3
0
def tests():
    array = [0, 1, 3, -1, 2]
    merge(array, 0, 2, 3, 4)
    assert_(expected=[-1, 0, 1, 2, 3], actual=array)

    array = [0, 1, 3, -1, 2]
    merge_sort(array)
    assert_(expected=[-1, 0, 1, 2, 3], actual=array)
Beispiel #4
0
def test_binary_search(binary_search):
    array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    key = 6
    expected_index = 6
    actual = binary_search(array, key)
    assert_(expected_index, actual)

    array = [0, 1]
    key = 1
    expected_index = 1
    actual = binary_search(array, key)
    assert_(expected_index, actual)
def test():
    trie = Trie()
    trie.add("hello")
    trie.add("hey")
    trie.add("add")

    assert_(True, trie.exists("hello"))
    assert_(True, trie.exists("hey"))
    assert_(True, trie.exists("add"))
    assert_(False, trie.exists("RR"))

    node = trie.find("h")
    actual = node.suffixes()
    assert_(expected=["ello", "ey"], actual=actual)
Beispiel #6
0
def tests():
    node_u = GraphNode('U')
    node_d = GraphNode('D')
    node_a = GraphNode('A')
    node_c = GraphNode('C')
    node_i = GraphNode('I')
    node_t = GraphNode('T')
    node_y = GraphNode('Y')

    graph = Graph([node_u, node_d, node_a, node_c, node_i, node_t, node_y])
    graph.add_edge(node_u, node_a, 4)
    graph.add_edge(node_u, node_c, 6)
    graph.add_edge(node_u, node_d, 3)
    graph.add_edge(node_d, node_c, 4)
    graph.add_edge(node_a, node_i, 7)
    graph.add_edge(node_c, node_i, 4)
    graph.add_edge(node_c, node_t, 5)
    graph.add_edge(node_i, node_y, 4)
    graph.add_edge(node_t, node_y, 5)

    # Test-1
    start_node = node_u
    end_node = node_y
    output = dijkstra(start_node, end_node)
    end_node_value = None
    if end_node is not None:
        end_node_value = end_node.value
    print('Shortest Distance of ({} --> {}) is {} and path is {}'.format(
        start_node.value, end_node_value, output[1], output[0]))
    assert_(expected=(['U', 'C', 'I', 'Y'], 14), actual=output)

    # Test-2
    start_node = node_u
    end_node = None
    output = dijkstra(start_node, end_node)
    end_node_value = None
    if end_node is not None:
        end_node_value = end_node.value
    print('Shortest Distance of ({} --> {}) is {} and path is {}'.format(
        start_node.value, end_node_value, output[1], output[0]))
    assert_(expected=(['U', 'C', 'I', 'Y'], 14), actual=output)
def tests(first_and_last_index):
    array = [0, 1, 2, 3, 3, 3, 3, 4, 5, 6]
    key = 3
    assert_(expected=[3, 6], actual=first_and_last_index(array, key))

    array = [1]
    key = 1
    assert_(expected=[0, 0], actual=first_and_last_index(array, key))

    array = [0, 1, 2, 3, 4, 5]
    key = 5
    assert_(expected=[5, 5], actual=first_and_last_index(array, key))

    key = 6
    assert_(expected=[-1, -1], actual=first_and_last_index(array, key))
Beispiel #8
0
def tests():
    array = [4, 3, 1, 4, 3, 3, 2, 4]
    quick_sort(array)
    assert_(expected=[1, 2, 3, 3, 3, 4, 4, 4], actual=array)

    array = [8, 3, 1, 7, 0, 10, 2]
    quick_sort(array)
    assert_(expected=[0, 1, 2, 3, 7, 8, 10], actual=array)

    array = [1, 0]
    quick_sort(array)
    assert_(expected=[0, 1], actual=array)

    array = [96, 97, 98]
    quick_sort(array)
    assert_(expected=[96, 97, 98], actual=array)
def test_evaluate_post_fix():
    test_case = (["3", "1", "+", "4", "*"], 16)
    assert_(test_case[1], evaluate_post_fix(test_case[0]))

    test_case = (["4", "13", "5", "/", "+"], 6)
    assert_(test_case[1], evaluate_post_fix(test_case[0]))

    test_case = (["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"], 22)
    assert_(test_case[1], evaluate_post_fix(test_case[0]))
Beispiel #10
0
def tests():
    array = [3, 1, 2, 0, 2, -1]
    heap_sort(array)
    assert_(expected=[-1, 0, 1, 2, 2, 3], actual=array)

    array = [3, 7, 4, 6, 1, 0, 9, 8, 9, 4, 3, 5]
    solution = [0, 1, 3, 3, 4, 4, 5, 6, 7, 8, 9, 9]
    heap_sort(array)
    assert_(expected=solution, actual=array)

    array = [5, 5, 5, 3, 3, 3, 4, 4, 4, 4]
    solution = [3, 3, 3, 4, 4, 4, 4, 5, 5, 5]
    heap_sort(array)
    assert_(expected=solution, actual=array)

    array = [99]
    solution = [99]
    heap_sort(array)
    assert_(expected=solution, actual=array)

    array = [0, 1, 2, 5, 12, 21, 0]
    solution = [0, 0, 1, 2, 5, 12, 21]
    heap_sort(array)
    assert_(expected=solution, actual=array)
def tests():
    min_heap = MinHeap(2)
    min_heap.insert(1)
    min_heap.insert(3)
    min_heap.insert(2)
    assert_(expected=4, actual=min_heap.capacity)
    assert_(expected=[1, 3, 2], actual=min_heap.to_list())

    min_heap.remove()
    assert_(expected=[2, 3], actual=min_heap.to_list())
Beispiel #12
0
def tests():
    input_list = [2, 7, 11, 15]
    target = 9
    solution = [2, 7]
    assert_(expected=solution, actual=pair_sum(input_list, target))

    input_list = [0, 8, 5, 7, 9]
    target = 9
    solution = [0, 9]
    assert_(expected=solution, actual=pair_sum(input_list, target))

    input_list = [110, 9, 89]
    target = 9
    solution = [None, None]
    assert_(expected=solution, actual=pair_sum(input_list, target))
def tests():
    test_case = [0, 0, 2, 2, 2, 1, 1, 1, 2, 0, 2]
    sort_0s_1s_2s(test_case)
    assert_(expected=sorted(test_case), actual=test_case)

    test_case = [
        2, 1, 2, 0, 0, 2, 1, 0, 1, 0, 0, 2, 2, 2, 1, 2, 0, 0, 0, 2, 1, 0, 2, 0,
        0, 1
    ]
    sort_0s_1s_2s(test_case)
    assert_(expected=sorted(test_case), actual=test_case)

    test_case = [2, 2, 0, 0, 2, 1, 0, 2, 2, 1, 1, 1, 0, 1, 2, 0, 2, 0, 1]
    sort_0s_1s_2s(test_case)
    assert_(expected=sorted(test_case), actual=test_case)
Beispiel #14
0
def test_hash_map():
    hash_map = HashMap(2)

    # Test HashMap get and put
    key = "abcde"
    value = "ramiz"
    hash_map.put(key, value)
    output = hash_map.get(key)
    assert_(value, output)

    # Test size
    assert_(1, hash_map.size())

    # delete
    hash_map.delete("abcde")
    assert_(0, hash_map.size())

    # Test Rehash
    hash_map.put("mine", "mine")
    # this should trigger rehashing
    hash_map.put("hi", "hi")
    assert_(2, hash_map.size())
    print("All Tests Passed!")
Beispiel #15
0
def test():
    assert_(2, minimum_bracket_reversals("}}}}"))
    assert_(2, minimum_bracket_reversals("}}{{"))
    assert_(-1, minimum_bracket_reversals("}{{"))
    assert_(2, minimum_bracket_reversals("}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{"))
    assert_(1, minimum_bracket_reversals("}}{}{}{}{}{}{}{}{}{}{}{}{}{}{}"))
def test_function(test_case):
    arrival = test_case[0]
    departure = test_case[1]
    solution = test_case[2]
    output = min_platforms(arrival, departure)
    assert_(expected=solution, actual=output)
def test_function(test_case):
    num_islands = test_case[0]
    bridge_config = test_case[1]
    solution = test_case[2]
    output = get_minimum_cost_of_connecting(num_islands, bridge_config)
    assert_(expected=solution, actual=output)
Beispiel #18
0
def tests():
    graph = Graph()
    nodeA = GraphNode('A')
    nodeB = GraphNode("B")
    nodeC = GraphNode("C")
    nodeAA = GraphNode("AA")

    graph.add_node(nodeA)
    graph.add_node(nodeAA)
    graph.add_node(nodeB)
    graph.add_node(nodeC)

    graph.add_edge(nodeA, nodeC)
    graph.add_edge(nodeA, nodeAA)
    graph.add_edge(nodeC, nodeB)

    assert_(expected=['A', 'C', "AA", 'B'], actual=graph.BFS_traversal())
    assert_(expected=nodeB, actual=graph.BFS_search('B'))

    assert_(expected=["A", "C", "B", "AA"],
            actual=graph.DFS_traversal_iterative())
    assert_(expected=["A", "C", "B", "AA"],
            actual=graph.DFS_traversal_recursive())

    assert_(expected=nodeB, actual=graph.DFS_search_iterative('B'))
    assert_(expected=nodeB, actual=graph.DFS_search_recursive('B'))
def tests():
    array = [2, 1]
    inversions = count_inversions(array)
    assert_(expected=1, actual=inversions)

    array = [3, 1, 2, 4]
    inversions = count_inversions(array)
    assert_(expected=2, actual=inversions)

    array = [7, 5, 3, 1]
    inversions = count_inversions(array)
    assert_(expected=6, actual=inversions)

    array = [2, 5, 1, 3, 4]
    inversions = count_inversions(array)
    assert_(expected=4, actual=inversions)

    array = [54, 99, 49, 22, 37, 18, 22, 90, 86, 33]
    inversions = count_inversions(array)
    assert_(expected=26, actual=inversions)

    array = [1, 2, 4, 2, 3, 11, 22, 99, 108, 389]
    inversions = count_inversions(array)
    assert_(expected=2, actual=inversions)
def test_is_operator():
    assert_(True, is_operator("-"))
    assert_(False, is_operator("8"))
def test_evaluate_operator():
    assert_(3, evaluate_operator('2', '1', '+'))
    assert_(3, evaluate_operator('2', '-1', '-'))
    assert_(1, evaluate_operator('2', '1', '-'))
    assert_(2, evaluate_operator('2', '1', '*'))
    assert_(2, evaluate_operator('2', '1', '/'))
the first element will represent a list of comma-separated numbers sorted in ascending order, the second element
will represent a second list of comma-separated numbers (also sorted). Your goal is to return a comma-separated string
containing the numbers that occur in elements of strArr in sorted order. If there is no intersection,
return the string false.

Examples
Input: ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
Output: 1,4,13
Input: ["1, 3, 9, 10, 17, 18", "1, 4, 9, 10"]
Output: 1,9,10
"""
from asserts.asserts import assert_


def find_intersection(string):
    str1 = string[0]
    str2 = string[1]
    set1 = set([int(i) for i in str1.split(", ")])
    set2 = set([int(i) for i in str2.split(", ")])
    intersection = sorted(set2.intersection(set1))

    if len(intersection) == 0:
        return "false"

    return ",".join(map(str, intersection))


input1 = ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
output = find_intersection(input1)
assert_("1,4,13", output)
Beispiel #23
0
def tests():
    trie_node = TrieNode()
    trie_node.add("a")
    actual = "a" in trie_node
    assert_(expected=True, actual=actual)
Beispiel #24
0
def tests():
    tree = RedBlackTree()
    # Test insert
    tree.insert(2)
    tree.insert(3)
    tree.insert(1)
    tree.insert(0)

    in_order_expected = [0, 1, 2, 3]
    in_order_actual = tree.in_order_traversal()
    assert_(in_order_expected, in_order_actual)

    tree = RedBlackTree()
    tree.insert(9)
    tree.insert(6)
    tree.insert(19)
    tree.insert(13)
    tree.insert(16)
    # print(tree)
    in_order_expected = [6, 9, 13, 16, 19]
    in_order_actual = tree.in_order_traversal()
    assert_(expected=in_order_expected, actual=in_order_actual)

    assert_(expected=True, actual=tree.search(16))
    assert_(expected=True, actual=tree.search(19))
    assert_(expected=True, actual=tree.search(6))
    assert_(expected=False, actual=tree.search(31))