Пример #1
0
 def test_enter_smaller_value(self):
     t = bst.push(None, 5)
     t = bst.push(t, 3)
     left = node.make_node(None, 3, None)
     expected = node.make_node(left, 5, None)
     self.assertEqual(t, expected)
     self.assertEqual(node.count(t), 2)
Пример #2
0
 def test_enter_larger_value(self):
     t = bst.push(None, 3)
     t = bst.push(t, 5)
     right = node.make_node(None, 5, None)
     expected = node.make_node(None, 3, right)
     self.assertEqual(t, expected)
     self.assertEqual(node.count(t), 2)
Пример #3
0
def push(node, val, compare=_compare):
    """
    Add a new value to heap.
    Addes to value as a branch than switches added branch
    with its parent if needed.(push and swim)
    """
    if not node:
        node = make_node(None, val, None)
    else:
        # push new value to smaller branch
        if is_add_to_left(node):
            child = left(node)
            set_fun = set_left
        else:
            child = right(node)
            set_fun = set_right

        child = push(child, val)

        if compare(value(child), value(node)):
            child_value = value(child)
            child = set_value(child, value(node))
            node = set_value(node, child_value)

        node = set_fun(node, child)
    return node
Пример #4
0
    def node_from_component(component):
        kaldi_check(
            'name' in component and 'input' in component
            and 'type' in component,
            "'name', 'type' and 'input' are required in component: %s" %
            component)
        type = component['type']
        name = component['name']
        inputs = component['input']
        if not isinstance(inputs, list):
            inputs = [inputs]
        inputs = [
            input if isinstance(input, six.string_types) else str(input)
            for input in inputs
        ]

        attrs = {}
        if type in ATTRIBUTE_NAMES:
            attrs_names = ATTRIBUTE_NAMES[type]
            for key, value in component.items():
                if key in attrs_names:
                    attrs[key] = value

        consts = {}
        if type in CONSTS_NAMES:
            param_names = CONSTS_NAMES[type]
            for p_name in param_names:
                if p_name in component:
                    p_values = component[p_name]
                    p_tensor_name = name + '_' + p_name
                    consts[p_tensor_name] = p_values
                    inputs.append(p_tensor_name)
        return make_node(name, type, inputs, attrs, consts)
Пример #5
0
def update_value(node, val, compare=_compare):
    """
    updates value of a node.
    if childs are bigger than parent it switches child with parent
    """
    left_node = left(node)
    right_node = right(node)
    left_val = value(left_node) if left_node else None
    right_val = value(right_node) if right_node else None

    if left_node and compare(left_val, val):
        left_node = update_value(left_node, val)
        val = left_val

    if right_node and compare(right_val, val):
        right_node = update_value(right_node, val)
        val = right_val
    return make_node(left_node, val, right_node)
Пример #6
0
    def node_from_component(self, component):
        kaldi_check('name' in component
                    and 'input' in component
                    and 'type' in component,
                    "'name', 'type' and 'input'"
                    " are required in component: %s" % component)
        type = component['type']
        name = component['name']
        inputs = component['input']
        if not isinstance(inputs, list):
            inputs = [inputs]
        inputs = [input if isinstance(input, six.string_types)
                  else str(input)
                  for input in inputs]

        attrs = {}
        if type in ATTRIBUTE_NAMES:
            attrs_names = ATTRIBUTE_NAMES[type]
            for key, value in component.items():
                if key in attrs_names:
                    attrs[key] = value

        if type == KaldiOpType.ReplaceIndex.name:
            attrs['chunk_size'] = self._chunk_size
            attrs['left_context'] = self._left_context
            attrs['right_context'] = self._right_context
        if type == KaldiOpType.IfDefined.name:
            attrs['chunk_size'] = self._chunk_size

        consts = {}
        if type in CONSTS_NAMES:
            param_names = CONSTS_NAMES[type]
            for p_name in param_names:
                if p_name in component:
                    p_values = component[p_name]
                    p_tensor_name = name + '_' + p_name
                    consts[p_tensor_name] = p_values
                    inputs.append(p_tensor_name)
        return make_node(name, type, inputs, [name], attrs, consts)
Пример #7
0
import node as nd


def swap(head):
    if head == None:
        return None
    elif head.next == None:
        return head

    n_node = head.next

    # Swap node head and next
    head.next = swap(head.next.next)
    n_node.next = head
    return n_node


if __name__ == '__main__':
    c_node = nd.make_node([i for i in range(8)])
    c_node = swap(c_node)
    print(nd.get_data(c_node))
Пример #8
0

# Making the empty matrix
state = [[0 for x in xrange(n)] for x in xrange(n)]

# Minimax variables
maxvalue = -100000, state
minvalue = 100000, state


# Reading in the information from the file
with open(filename) as f:
    for i in range(n):
        for j in range(n):
            d = f.read(1)
            state[i][j] = node.make_node(d, 0) #fix to grab party
            space = f.read(1)


# Printing the matrix
print "matrix is: "
for i in range(n):
    for j in range(n):
        sys.stdout.write(state[i][j].party)
        sys.stdout.write(" ")
    print " "

# Populating list of unallocated nodes
for i in range(n):
    for j in range(n):
        if state[i][j].district is 0:
        return

    if head.next == None:
        return head

    new_head = reverseList(head.next)
    head.next.next = head
    head.next = None
    return new_head


# Iterative Solution O(1), Space of O(1)
def reverseList_iter(head):
    next = head.next
    head.next = None
    while next != None:
        cur = next
        next = next.next
        cur.next = head
        head = cur
    return head


if __name__ == '__main__':
    c_node = nd.make_node([0, 1, 2, 3])
    c_node = reverseList(c_node)
    print(nd.get_data(c_node))

    c_node = nd.make_node(3)
    c_node = reverseList_iter(c_node)
    print(nd.get_data(c_node))
Пример #10
0
def mergeTwoLists(list1, list2):
    """ Returns a merged list that is spliced from inputs """
    if list1 is None:
        return list2
    if list2 is None:
        return list1

    if list1.val < list2.val:
        node = list1
        out = mergeTwoLists(list1.next, list2)
    else:
        node = list2
        out = mergeTwoLists(list1, list2.next)

    node.next = out
    return node


if __name__ == '__main__':
    list1 = nd.make_node([1, 2, 4])
    list2 = nd.make_node([1, 3, 4])
    print(nd.get_data(mergeTwoLists(list1, list2)))

    list1 = nd.make_node([])
    list2 = nd.make_node([0])
    print(nd.get_data(mergeTwoLists(list1, list2)))

    list1 = nd.make_node([2])
    list2 = nd.make_node([1])
    print(nd.get_data(mergeTwoLists(list1, list2)))
Пример #11
0
    elif 'd' in arg:
        #Sum for d
        sumd = 0
        #First two rows
        sumd+=table[0][0]+table[1][0]+table[2][0]+table[3][0]
        sumd+=table[0][1]+table[1][1]+table[2][1]+table[3][1]
        #Second two rows
        sumd+=table[0][4]+table[1][4]+table[2][4]+table[3][4]
        sumd+=table[0][5]+table[1][5]+table[2][5]+table[3][5]
        print "Marginal Probability for d:"
        print "d=", sumd

"""
Create the nodes in the Bayes Net
"""
Pollution = node.make_node("N", .9)

Smoker = node.make_node("N", .3)

dictc = {'HT': .05, 'HF': .02, 'LT': .03, 'LF': .001}
Cancer = node.make_node(("P","S"), dictc)

dictx = {'True': .9, 'False': .2}
Xray = node.make_node("C", dictx)

dictd = {'True': .65, 'False': .3}
Dyspnoea = node.make_node("C", dictd)

"""
Table Creation
"""