def minmax_decision(prob): global no_of_nodes_mm, memory def minmax_value(node): global no_of_nodes_mm if terminal_test(node.state): return utility_func(node.state) elif node.player == 'MAX': v = -float('inf') for child in node.expand(prob): no_of_nodes_mm += 1 v = max(v, minmax_value(child)) return v else: v = float('inf') for child in node.expand(prob): no_of_nodes_mm += 1 # nstate = prob.nstate(a,state) v = min(v, minmax_value(child)) return v node = Node(prob.instate, player='MAX') values = [] children = node.expand(prob) for child in children: no_of_nodes_mm += 1 values.append((child, minmax_value(child))) result_node = max(values, key=lambda x: x[1])[0] memory = sys.getsizeof(result_node) # no_of_nodes_mm += result_node.depth*4 return result_node.action
def add_two_numbers(head1, head2): result = None end = result carry = 0 while head1 or head2: sum = 0 if head1: sum += head1.val head1 = head1.next if head2: sum += head2.val head2 = head2.next sum += carry carry = 0 if sum >= 10: carry = 1 sum -= 10 temp = Node(sum) if end is None: result = temp else: end.next = temp end = temp if carry == 1: end.next = Node(1) return result
def stack_sum(h1, h2): stack1 = list() stack2 = list() while h1: stack1.append(h1.val) h1 = h1.next while h2: stack2.append(h2.val) h2 = h2.next prev = None carry = 0 while stack1 or stack2: sum = 0 if len(stack1) > 0: sum += stack1.pop() if len(stack2) > 0: sum += stack2.pop() sum += carry carry = 0 if sum >= 10: sum -= 10 carry = 1 temp = Node(sum, prev) prev = temp if carry == 1: temp = Node(1, prev) prev = temp return prev
def reverse(head): y = Node(head.val) head = head.next x = y while head: temp = Node(head.val, x) head = head.next x = temp return x
def node_generator(): """Creates nodes for testing functions""" Node.h_method = 'manhattan' Node.final_grid = [1,2,3,8,0,4,7,6,5] Node.size = 3 Node1 = Node(grid = [5, 8, 6, 2, 0, 7, 4, 1, 3]) Node2 = Node(grid = [0, 8, 5, 3, 1, 2, 7, 4, 6]) return [Node1, Node2]
def build_tree(arr): if arr is None: return None elif len(arr) == 1: temp = Node(arr[0]) return temp else: mid = len(arr) // 2 left_child = build_tree(arr[0:mid]) right_child = build_tree(arr[mid + 1:]) node = Node(arr[mid], left_child, right_child) return node
def build_tree_2(inorder, postorder): if not inorder: return if len(inorder) == 1: return Node(inorder[0]) root_val = postorder[-1] left_tree = inorder[:inorder.index(root_val)] left_node = build_tree_2(left_tree, postorder[:len(left_tree)]) right_node = build_tree_2(inorder[len(left_tree) + 1:], postorder[len(left_tree):-1]) root_node = Node(root_val, left_node, right_node) return root_node
def test_clone_linked_list(self): head = self.creating_list() new_head = Node.clone_linked_list(head) # getting the two lists for comparison old_list = Node.get_linked_list(head) new_list = Node.get_linked_list(new_head) self.assertEqual(new_list[1].data, 6) self.assertEqual(new_list[1].next, new_list[2]) self.assertEqual(new_list[1].rnd, new_list[3]) self.assertEqual(old_list, new_list) self.assertIsNot(old_list, new_list)
def main(): l1 = Node.test_list() l2 = Node.test_list_1() Node.print_list(l1) Node.print_list(l2) l = add_two_numbers_2(l1, l2) Node.print_list(l)
def duplicate_1(head): dic = dict() dic[None] = None p = head while p: dic[p] = Node(p.val) p = p.next p = head while p: dic[p].next = dic[p.next] dic[p].random = dic[p.random] p = p.next return dic[head]
def make_nodes(self): '''Make a search space for the nodes''' guid = 0 for i in range(0, self.dimension.x_pos): for j in range(0, self.dimension.y_pos): _node = Node(Vector2(i, j), guid) guid = guid + 1 self.nodes.append(_node)
def insert(self, item, index): current = self.head previous = None counter = self.length() - 1 while counter > index: previous = current current = current.getNext() counter -= 1 temp = Node(item) if previous is None: self.head = temp previous.setNext(temp) temp.setNext(current) self.len += 1
def add_tail(self, list_head, val): endNode = Node(val) if list_head is None: list_head = endNode return LastNode = list_head while LastNode.next: LastNode = LastNode.next LastNode.next = endNode
def add_tail(self, list_head, val): endNode = Node(val) if list_head is None: list_head = endNode return lastNode = list_head while (lastNode.next): lastNode = lastNode.next lastNode.next = endNode
def append(self, item): current = self.head while current.getNext(): current = current.getNext() temp = Node(item) current.setNext(temp) self.len += 1
def insert(self, cargo): node = Node(cargo) ## creates a Node with cargo, no .next if self.length == 0: ## if this is the first item self.head = self.last = node ## the node is the first AND last item else: last = self.last ## find the last node last.next = node ## append the new node self.last = node ## the node is now the last in the Q self.length += 1
def duplicate(head): if head is None: return None result = Node(head.val) temp = head.next temp1 = result old_to_new = {} old_to_new[head] = result while temp: x = Node(temp.val) old_to_new[temp] = x temp1.next = x temp1 = x temp = temp.next temp = head while temp: if temp.random: old_to_new[temp].random = old_to_new[temp.random] temp = temp.next return result
def insert(self, cargo): node = Node(cargo) if self.head is None: # If list is empty the new node goes first self.head = node else: # Find the last node in the list last = self.head while last.next: last = last.next # Append the new node last.next = node self.length += 1
def creating_list() -> Node: # initiating nodes with data n1, n2, n3, n4 = Node(5), Node(6), Node(7), Node(8) # connecting each node with its Successor n1.next, n2.next, n3.next, n4.next = n2, n3, n4, None # connecting each node with its random node n1.rnd = n3 n2.rnd = n4 n3.rnd = n2 n4.rnd = n1 # returning linked list head node return n1
def __init__(self, topic): self.keyboard = InlineKeyboardMarkup(inline_keyboard=[ [InlineKeyboardButton(text="/list", callback_data='l')], [InlineKeyboardButton(text="/question", callback_data='q')], [InlineKeyboardButton(text="/report", callback_data='r')], [InlineKeyboardButton(text="/start", callback_data='s')], [InlineKeyboardButton(text="/revision", callback_data='rv')], [InlineKeyboardButton(text="/change_lang", callback_data='cl')], [ InlineKeyboardButton(text="/delete_subscription", callback_data='ds') ] ]) database = Database() self.students = database.get_stud_ids(topic) self.teachers = database.get_teach_ids(topic) self.collaborators = database.get_coll_ids(topic) self.node = Node(topic) self.banned_user = database.get_banned_stud(topic) self.singleton = BotId() super().__init__(database.get_topic_token(topic), self.message, self.query) self.singleton.set_bot(self.node.get_topic_name(), super().get_bot()) self.singleton.reset_key_id(topic)
def main(): parse_data_file_args() training_set, training_labels, test_set, test_labels = preprocess_train_data( ) #set up and build dt from imported node class print("Building decision tree... \n") n = Node(training_set, training_labels, str(args.impurity), str(args.nlevels)) dt = n.build_decision_tree(training_set, training_labels, str(args.impurity), int(str(args.nlevels)), float(str(args.pthrd))) #given test data, get classifications from dt print("Classifying test data... \n") test_data_classifications = classify(test_set, str(args.pred_file), dt) #calc and print accuracy of labels a = accuracy(test_data_classifications, test_labels) print("Overall classification accuracy: " + str(a)) print("Overall error rate: " + str(1.0 - a) + "\n") #BONUS: class 1 binary classification calculation method call a = confusion_matrix(test_data_classifications, test_labels)
def build_tree(data): lst = return_frequency(data) nodes_list = [] for node_value in lst: node = Node(node_value) nodes_list.append(node) while len(nodes_list) != 1: first_node = nodes_list.pop() second_node = nodes_list.pop() val1, char1 = first_node.value val2, char2 = second_node.value node = Node((val1 + val2, char1 + char2)) node.set_left_child(second_node) node.set_right_child(first_node) sort_values(nodes_list, node) root = nodes_list[0] tree = Tree() tree.root = root return tree
def abprune_decision(pro): global memory def abprune_value(node, a, b): global no_of_nodes # node = Node(pro.instate,'MAX') if terminal_test(node.state): return utility_func(node.state), node if node.player == 'MAX': v = -float('inf') c = [] children = node.expand(pro) for child in children: # p = pro(child.state) no_of_nodes += 1 v = max(v, abprune_value(child, a, b)[0]) c.append((child, v)) a = max(a, v) if a > b: break return v, max(c, key=lambda x: x[1])[0] elif node.player == 'MIN': v = float('inf') c = [] children = node.expand(pro) for child in children: no_of_nodes += 1 # c = child # p = pro(child.state) v = min(v, abprune_value(child, a, b)[0]) c.append((child, v)) b = min(b, v) if a > b: break return v, min(c, key=lambda x: x[1])[0] node = Node(pro.instate, 'MAX') memory = sys.getsizeof(node) _, c = abprune_value(node, -float('inf'), float('inf')) return c.action
def put(self, key, value): if self.item_cache[key]: temp = self.item_cache[key] prev = temp.prev nex = temp.next prev.next = nex nex.prev = prev self.end.next = temp temp.prev = self.end self.end = temp else: if self.length > 8: # assuming the cache can hold 8 items self.head.next.prev = None self.head = self.head.next temp = Node(value, previous=self.end) self.item_cache[key] = temp if self.end: self.end.next = temp self.end = temp self.length += 1 if self.head is None: self.head = temp
def constructMaximumBinaryTree(nums): size = len(nums) if (size != 0): max_index = max(nums) root = Node(nums[max_index]) if (max_index > 0): leftarr = nums[0:max_index] root.left = constructMaximumBinaryTree(leftarr) else: root.left = None if (max_index < size - 1): rightarr = nums[max_index + 1:size] root.right = constructMaximumBinaryTree(rightarr) else: root.right = None return root return None
def main(grid, args): grid_size = int(sqrt(len(grid))) Node.final_grid = create_success_grid(grid_size) Node.size = grid_size Node.h_method = args.method Node.h_algo = args.algo initial_node = Node(grid=grid, g=0, parent=None) start = time.time() if args.algo == 'ida_star': algo = Idastar() ida_star(initial_node, algo) else: algo = Algo() search_algo(initial_node, algo) end = time.time() algo.time = end - start if args.visu: visu = Visu(algo) visu.play() else: print_results(algo, args.detail) return None
def add(self, item): current = self.head previous = None stop = False while current is not None and not stop: if current.getData() > item: stop = True else: previous = current current = current.getNext() temp = Node(item) if previous is None: temp.setNext(self.head) self.head = temp else: temp.setNext(current) previous.setNext(temp) self.len += 1
def add(self, item): temp = Node(item) temp.setNext(self.head) self.head = temp self.len += 1
from node_class import Node # defing of all the nodes one by one. # example: # node1 = Node('node1', 'C:\\Windows\\Node 10.0.4.86') node1 = Node('first node', 'path_to_the_node') # user - is a name of server user = "******" # an array of <Node> objects. Do not forget to add them all list_of_nodes = [ node1, ]
def test_get_linked_list(self): head = self.creating_list() result_list = Node.get_linked_list(head) self.assertEqual(result_list[2].data, 7) self.assertEqual(result_list[2].next, result_list[3]) self.assertEqual(result_list[2].rnd, result_list[1])