def test_bs_dll_result(): data_n = DoubleLinkedList() for i in data: data_n.push(i) #print(data) #print(data_n.dump()) assert bs_dll_result(data_n, 5) == (5, 2) assert bs_dll_result(data_n, 1) == (1, 0) assert bs_dll_result(data_n, 20) == (20, len(data) - 1) assert bs_dll_result(data_n, 9) == (9, 4) assert bs_dll_result(data_n, 100) is None assert bs_dll_result(data_n, -1) is None
class DLListRouter(URLRouter): def __init__(self): self.obj = DoubleLinkedList() # super().__init__() # self.url = url # if url: # self.obj.push(URLNode(url, url)) # else: # pass def add(self, key, value): self.obj.push(URLNode(key, value)) def find_exact_match(self, key): node = self.obj.begin while node: if node.value.key == key: return node.value # Find only first matched occurance else: # pass node = node.next return None def find_all(self, key): matched_nodes_list = [] node = self.obj.begin while node: if key in node.value.key: matched_nodes_list.append(node.value) else: pass node = node.next return matched_nodes_list def find_part(self, key): matched_nodes_list = [] i = 0 #node = self.obj.begin while i < len(key) and matched_nodes_list == []: matched_nodes_list = self.find_all(key[:len(key)-i]) i = i + 1 print(i, key[:len(key)-i]) p = sorted([x for x in matched_nodes_list], key=lambda x: len(x.key)) print([_.key for _ in p]) return p[0] if p != [] else None
def bs_dll(dll, key): '''Sorted doubly linked list needed for the input argument''' result = None ll = dll.count() tmp = DoubleLinkedList() midpt = ll//2 # Note the convention of the midpoint will have an influence # to the following binary division(L53~L65). if ll > 0: mid = dll.get(midpt) i = 0 midnode = dll.begin while i < midpt: midnode = midnode.next i = i + 1 else: return None if key == mid: return mid elif ll > 1: if key > mid: while midnode: tmp.push(midnode.value) midnode = midnode.next result = bs_dll(tmp, key) else: midnode = midnode.prev # Backforwd once needed firstly since Line 37 # for the conversion of the midpoint index # Otherwise, infinit function calls occur while midnode: tmp.shift(midnode.value) midnode = midnode.prev result = bs_dll(tmp, key) return result
def __init__(self): self.obj = DoubleLinkedList()
def random_list(count): numbers = DoubleLinkedList() for i in range(count, 0, -1): numbers.shift(randint(0, 10000)) return numbers