def main(): linknode1 = LinkedNode() value_list = np.sort(np.random.randint(1, 7, 6)) for i in value_list: node = Node(i) linknode1.add(node) print('The LinkedNode1 is: ' + linknode1.__str__()) linknode2 = LinkedNode() value_list = np.sort(np.random.randint(1, 10, 6)) for i in value_list: node = Node(i) linknode2.add(node) print('The LinkedNode2 is: ' + linknode2.__str__()) linknode3 = Solution_25() result = linknode3.merge(linknode1.head, linknode2.head) print('After merge the two LinkedNode:') while result.next is not None: print(result.data, end=', ') result = result.next print(result.data, end=', ')
def add_node(self, value): node = LinkedNode(value) if self.__size == 0: self.__dummy.next_node = node self.__dummy.prev_node = node node.next_node = self.__dummy node.prev_node = self.__dummy else: node.next_node = self.__dummy.next_node node.prev_node = self.__dummy self.__dummy.next_node.prev_node = node self.__dummy.next_node = node self.__size += 1
def main(): linknode = LinkedNode() value_list = np.random.randint(1, 7, 6).tolist() for i in value_list: node = Node(i) linknode.add(node) print('The LinkedNode is: ' + linknode.__str__()) k = value_list[np.random.randint(1, 6)] print('The number to be deleted is: ' + str(k)) outputnode = Solution_22() outputnode.output_k_node_in_linkednode(linknode, k)
def test_insert(): list = LinkedNode(10) list += 30 list += 40 list += 50 assert repr(list) == '10' assert repr(list.next_node.key) == '30' assert print_list(list) == '10, 30, 40, 50'
def synchronize(nodeList1, nodeList2): d1 = nodeList1[0] d2 = nodeList2[0] log.info('----------------------------------------') log.info(' SYNCHRONIZE') log.info('----------------------------------------') log.debug('Before synchronization') log.debug(' List 1 (%d points):', len(nodeList1)) log.debug(' Start: %s', nodeList1[0].gps_ts) log.debug(' End : %s', nodeList1[-1].gps_ts) log.debug(' List 2 (%d points):', len(nodeList2)) log.debug(' Start: %s', nodeList2[0].gps_ts) log.debug(' End : %s', nodeList2[-1].gps_ts) ll1 = convert_tuples_to_linked_list([d.as_tuple() for d in nodeList1]) ll2 = convert_tuples_to_linked_list([d.as_tuple() for d in nodeList1]) if d1.gps_ts < d2.gps_ts: # Add points to start of list 2 log.debug('Extending list 2') newD = GPSDatum((d1.gps_ts, (d2.gps_la, d2.gps_lo), (d2.gps_x, d2.gps_y), d2.gps_a, d2.gps_s)) log.debug('New datum: %s', newD) firstNode = LinkedNode(newD, ll2) interpolate_list(firstNode) nodeList2 = firstNode.to_array() if d1.gps_ts > d2.gps_ts: # Add points to start of list 1 log.debug('Extending list 1') newD = GPSDatum((d2.gps_ts, (d1.gps_la, d1.gps_lo), (d1.gps_x, d1.gps_y), d1.gps_a, d1.gps_s)) log.debug('New datum: %s', newD) firstNode = LinkedNode(newD, ll1) interpolate_list(firstNode) nodeList1 = firstNode.to_array() log.debug('After synchronization:') log.debug(' List 1 (%d points):', len(nodeList1)) log.debug(' Start: %s', nodeList1[0].gps_ts) log.debug(' End : %s', nodeList1[-1].gps_ts) log.debug(' List 2 (%d points):', len(nodeList2)) log.debug(' Start: %s', nodeList2[0].gps_ts) log.debug(' End : %s', nodeList2[-1].gps_ts) return (nodeList1, nodeList2)
def test_assignment(self): node1 = LinkedNode(4) node2 = LinkedNode("string") #Checking default values self.assertEqual(node1.prev_node, None) self.assertEqual(node1.next_node, None) #Checking Node Assignment try: node1.next_node = node2 exception = False except ValueError: exception = True self.assertFalse(exception) with self.assertRaises(ValueError): node2.next_node = "Bad Value"
def main(): linknode = LinkedNode() value_list = np.random.randint(1, 7, 6).tolist() for i in value_list: node = Node(i) linknode.add(node) print('The LinkedNode is: ' + linknode.__str__()) reverselinkednode = Solution_24() reversenode = reverselinkednode.reverse_linkednode(linknode.head) reversehead = reversenode while reversehead.next is not None: print(reversehead.data, end=', ') reversehead = reversehead.next print(reversehead.data, end=', ')
def main(): linkedtable = LinkedNode() for i in np.random.randint(2, 15, 10).tolist(): node = Node(i) linkedtable.add(node) print('The linked node before modified is: ' + linkedtable.__str__()) delete_duplication(linkedtable) print('The linked node after modified is: ' + linkedtable.__str__())
def main(): linkedtable = LinkedNode() for i in np.random.randint(2, 15, 10).tolist(): node = Node(i) linkedtable.add(node) print('The linked node before modified is: ' + linkedtable.__str__()) listofnode = linkedtable.listofnode() print('The list of node is: ', listofnode) tobedeletednode = listofnode[9] deletenode(linkedtable, tobedeletednode) # print('The value of node to be deleted is %d, and the value of the next closing node is %s' % (tobedeletednode.data, tobedeletednode.next.data)) print('The linked node after modified is: ' + linkedtable.__str__())
def test_delete(): list = LinkedNode(10) list += 20 list += 30 list += 40 list = delete_item(list, 30) assert print_list(list) == '10, 20, 40' list = delete_item(list, 10) assert print_list(list) == '20, 40' list = delete_item(list, 50) assert print_list(list) == '20, 40' list = delete_item(list, 40) assert print_list(list) == '20' list = delete_item(list, 20) assert type(list) == NoneType
def main(): ls1 = [1, 2, 3, 4, 5, 6, 7, 8] # ls2 = [0, 5, 6, 7, 8] ls2 = [9, 10, 11, 12] # 创建链表 link1 = LinkedNode() link2 = LinkedNode() for i in ls1: link1.add(i) for j in ls2: link2.add(j) # print(link2.listofnode()) commom_node = Solution_52().find_first_common_node(link1.head, link2.head) if commom_node: print('Two list node\'s commom node are %d.' % commom_node.data) else: print('Two list node have not commom node.')
def test_create_empty(): list = LinkedNode(10) assert list.key == 10
def parse(data): initNode = None prevNode = None curNode = None lowerBound = {} upperBound = {} y_min = [float("inf") for _ in range(len(data[2].strip().split()))] y_max = [float("-inf") for _ in range(len(data[2].strip().split()))] for line in data: # This is a mode indicator if ',' in line or '->' in line or line.strip().isalpha() or len( line.strip()) == 1: insertData(curNode, lowerBound, upperBound) # There is new a transition if '->' in line: modeList = line.strip().split('->') prevNode = initNode for i in range(1, len(modeList) - 1): prevNode = prevNode.child[modeList[i]] curNode = prevNode.child.setdefault( modeList[-1], LinkedNode(modeList[-1], line)) else: curNode = LinkedNode(line.strip(), line) if not initNode: initNode = curNode else: curNode = initNode # Using dictionary becasue we want to concat data lowerBound = {} upperBound = {} LOWER = True else: line = map(float, line.strip().split()) if len(line) <= 1: continue if LOWER: LOWER = False # This data appered in lowerBound before, concat the data if line[0] in lowerBound: for i in range(1, len(line)): lowerBound[line[0]][i] = min(lowerBound[line[0]][i], line[i]) else: lowerBound[line[0]] = line for i in range(len(line)): y_min[i] = min(y_min[i], line[i]) else: LOWER = True if line[0] in upperBound: for i in range(1, len(line)): upperBound[line[0]][i] = max(upperBound[line[0]][i], line[i]) else: upperBound[line[0]] = line for i in range(len(line)): y_max[i] = max(y_max[i], line[i]) insertData(curNode, lowerBound, upperBound) return initNode, y_min, y_max
def interpolate_list(firstNode, deltaF, interF, deDup=True): ''' Interpolate over a linked list. Interpolation happens 'in-place', no object is returned. @param firstNode: the first node of the list. @param deltaF: a function used to calculate the delta between two nodes. @param interF: a function used to interpolate between two nodes. ''' global count_added, count_removed, counter log.debug('Interpolation start:') log.debug(' delta_F := %s', deltaF.__name__) log.debug(' interF := %s', interF.__name__) log.debug(' deDup := %s', deDup) # Reset counters count_added = count_removed = counter = 0 # Initialise with first node thisNode = firstNode while thisNode != None and thisNode.nxt != None: nextNode = thisNode.nxt # Calculate distance between points delta = deltaF(thisNode.obj, nextNode.obj) log.debug('Node %d: delta %d', counter, delta) if delta < 0: # Negative delta, remove point thisNode.nxt = nextNode.nxt log.warn('Negative time delta (%d) at node %d', delta, counter) # Increment counter count_removed += 1 counter += 1 if delta == 0 and deDup: # Duplicate, remove point thisNode.nxt = nextNode.nxt log.debug('Removed duplicate node at %d', counter) # Increment counter count_removed += 1 counter += 1 while delta > 1: # Interpolate a new point mediating this node and next node newNode = LinkedNode(interF(thisNode.obj, nextNode.obj)) # Set new node's next to next node newNode.nxt = nextNode # Set this node's next to new node thisNode.nxt = newNode log.debug('Created new node at %d:', counter) log.debug(' newNode := %s', newNode) # Recalculate delta nextNode = thisNode.nxt delta = deltaF(thisNode.obj, nextNode.obj) log.debug('Node %d: delta %d', counter, delta) # Increment counters count_added += 1 counter += 1 # Move to next node thisNode = nextNode log.info('Interpolation complete over %d points. %d points added; %d points removed.', counter, count_added, count_removed)