def stopwords_init(self): fileQueue = LinkedQueue() fp = open("StopWords.txt", '+r') for line in fp.readlines(): key = (line.rstrip('\r\n')) fileQueue.enqueue(key) fp.close() i = 0 for q in fileQueue: self.stopwordsTST.put(str(q.element), i, None) i += 1 self.stopwordsTST.validation() for q in fileQueue: self.stopwordsBST.insert(str(q.element), None) i = 0 for q in fileQueue: self.stopwordsTrie.put(str(q.element), i, None) i += 1 self.stopwordsTrie.validation() i = 0 for q in fileQueue: self.stopwordsSCHashST[i] = q.element i += 1
class LinkedQueueTest(object): def __init__(self): self.queue = LinkedQueue() def _enqueue_dequeue_test(self): """ enqueue和dequeue方法测试 :return: None """ print("enqueue和dequeue方法测试中....") print("队列:", self.queue) size = 10 print("开始enqueue元素%d次" % size) for i in range(size): self.queue.enqueue(i) print("push结束,当前队列为:", self.queue) print("当前队列长度为:", len(self.queue)) print("顶部元素为:", self.queue.first()) print("开始dequeue元素%d次" % size) for i in range(size): self.queue.dequeue() print("dequeue结束,当前队列为:", self.queue) print("当前应该为空:", self.queue.is_empty()) def run(self): """ 执行测试方法 :return: None """ self._enqueue_dequeue_test()
class LinkedQueueTest(unittest.TestCase): def setUp(self): self._queue_e0 = LinkedQueue() self._queue_e2 = LinkedQueue() self._queue_e2.enqueue("one") self._queue_e2.enqueue("two") def tearDown(self): pass def test_emptyStack(self): """check empty queuee""" q = copy.deepcopy(self._queue_e0) self.assertTrue(q.is_empty()) def test_enqueue(self): q = copy.deepcopy(self._queue_e0) q.enqueue("one") self.assertEqual(q.first(), "one") q.enqueue("two") self.assertEqual(len(q), 2) def test_dequeue(self): q = copy.deepcopy(self._queue_e2) res = q.dequeue() self.assertEqual(res, "one") self.assertEqual(len(q), 1)
def breadthfirst(self): if not self.is_empty(): fringe=LinkedQueue() fringe.enqueue(self.root()) while not fringe.is_empty(): p=fringe.dequeue() yield p for c in self.children(p): fringe.enqueue(c)
def breadthfirst(self): if not self.is_empty(): q = LinkedQueue() q.enqueue(self.root()) while not q.is_empty(): p = q.dequeue() yield p for c in self.children(p): q.enqueue(c)
def _breadth(self, posi): fringe = LinkedQueue() fringe.enqueue(posi) while not fringe.is_empty(): p = fringe.dequeue() yield p for child in self.children(p=p): fringe.enqueue(child)
def breathfirst(self): """ Generate a breath first iteration of positions in the tree """ if not self.is_empty(): q = LinkedQueue() q.enqueue(self.root()) while not q.is_empty(): p = q.dequeue() yield p for c in self.children(p): q.enqueue(c)
def breadthfirst(self): '''Generate a breadtg-first iteration of the positions of tree.''' if not self.is_empty(): fringe = LinkedQueue() # known positions not yet yielded fringe.enqueue(self.root()) #starting with the root while not fringe.is_empty(): p = fringe.dequeue() # remove from front of queue yield p # report this position for c in self.children(p): fringe.enqueue(c) # add children to back of queue
def breadthfirst(self): """Generate a breadthfirst iteration of the positions of the tree""" if not self.is_empty(): fringe = LinkedQueue() fringe.enqueue(self.root()) while not fringe.is_empty(): p = fringe.dequeue() yield p for c in self.children(p): fringe.enqueue(c)
def keysWithPrefix(self, prefix): if prefix is None: raise Exception("call keysWithPrefix() with None argument") queue = LinkedQueue() x = self.root x = self.get(x, prefix, 0) if x is None: return queue if x.value is not None: queue.enqueue(prefix) self.collect(x.mid, str(prefix), queue) return queue
def breadthfirst(self): """ Generate a breadth-first iteration of the positions of the tree. :return: """ if not self.is_empty(): fringe = LinkedQueue() fringe.enqueue(self.root()) while not fringe.is_empty(): p = fringe.dequeue() yield p for c in self.children(p): fringe.enqueue(c)
def merge_sort1(S): n = len(S) if n < 2: return S1 = LinkedQueue() S2 = LinkedQueue() while len(S1) < n // 2: S1.enqueue(S.dequeue()) while not S.is_empty(): S2.enqueue(S.dequeue()) merge_sort1(S1) merge_sort1(S2) merge1(S1, S2, S)
def BFS(self, source): i = source q = LinkedQueue() visited = [0] * self._vertices print(i, end=" - ") visited[i] = 1 q.enqueue(i) while not q.is_empty(): i = q.dequeue() for j in range(self._vertices): if self._adjMat[i][j] == 1 and visited[j] == 0: print(j, end=" - ") visited[j] = 1 q.enqueue(j)
def bufferizer(self, iterable, length=10, seprator=", "): buffer_txt = None for txt in iterable: if buffer_txt is None: buffer_txt = LinkedQueue() # ------------------------------------- projectile ------------------------------------- buffer_txt.enqueue(txt) if len(buffer_txt) > length: self.projectile_pane(buffer_txt, seprator=seprator) buffer_txt = None if buffer_txt is not None: self.projectile_pane(buffer_txt, seprator=seprator) self.area.append("--------------------------------------------------------------------------\n")
def merge_sort(S): """ Sort the elements of queue S using the merge-sort algorithm :param S: :return: """ n = len(S) if n < 2: return S1 = LinkedQueue() S2 = LinkedQueue() while len(S1) < n // 2: S1.enqueue(S.dequeue()) while not S.is_empty(): S2.enqueue(S.dequeue()) merge_sort(S1) merge_sort(S2) merge(S1, S2, S)
def card_shuffle(L): """ :param L: LinkedQueue :return: """ assert isinstance(L, LinkedQueue) if not L.is_empty(): n = len(L) n1 = n // 2 L1, L2 = LinkedQueue(), LinkedQueue() for i in range(n): if i < n1: L1.enqueue(L.dequeue()) else: L2.enqueue(L.dequeue()) while not L1.is_empty() and not L2.is_empty() : L.enqueue(L1.dequeue()) L.enqueue(L2.dequeue()) if n - n1 > n1: L.enqueue(L2.dequeue()) # len(L) is an odd number, 1 left in L2 return L
def merge_sort_linked(S): """ sort the elements fo queue S using the merge sort algorithm :param S: :return: """ n = len(S) if n < 2: # already sorted return # divide S1 = LinkedQueue() S2 = LinkedQueue() while len(S1) < n//2: # move the first n//2 elements to S1 S1.enqueue(S.dequeue()) while not S.is_empty(): # move the rest to S2 S2.enqueue(S.dequeue()) # conquer with recursion merge_sort_linked(S1) # sort first half merge_sort_linked(S2) # sort second half merge_linked(S1, S2, S) # merge sorted halves back into S
def quick_sort(s): """”””Sort the elements of queue S using the quick-sort algorithm""" n = len(s) if n < 2: # base case, return immediately return lt = LinkedQueue() eq = LinkedQueue() gt = LinkedQueue() pivot = s.front() while not s.is_empty(): if s.front() < pivot: # put smaller elements in the lt lt.enqueue(s.dequeue()) elif s.front() > pivot: # put larger elements in the gt gt.enqueue(s.dequeue()) else: # put equal elements in the eq eq.enqueue(s.dequeue()) # conquer (with recursion) quick_sort(lt) # sort elements less than p quick_sort(gt) # sort elements greater than p # concatenate results while not lt.is_empty(): s.enqueue(lt.dequeue()) while not eq.is_empty(): s.enqueue(eq.dequeue()) while not gt.is_empty(): s.enqueue(gt.dequeue())
def quick_sort(S): """ Sort the elements of queue S using the quick-sort algorithm. :param S: :return: """ n = len(S) if n < 2: return p = S.first() L = LinkedQueue() E = LinkedQueue() G = LinkedQueue() while not S.is_empty(): if S.first() < p: L.enqueue(S.dequeue()) elif p < S.first(): G.enqueue(S.dequeue()) else: E.enqueue(S.dequeue()) quick_sort(L) quick_sort(G) while not L.is_empty(): S.enqueue(L.dequeue()) while not E.is_empty(): S.enqueue(E.dequeue()) while not G.is_empty(): S.enqueue(G.dequeue())
def quick_sort(S): """ sort the elements of queue S using the quick-sort algorithm :param S: :return: """ n = len(S) if n < 2: return # list is already sorted # divide pivot = S.first() # using first as arbitrary pivot L = LinkedQueue() E = LinkedQueue() G = LinkedQueue() while not S.is_empty(): # divide S into L, E and G if S.first() < pivot: L.enqueue(S.dequeue()) elif S.first() > pivot: G.enqueue(S.dequeue()) else: # S.first() must equal pivot E.enqueue(S.dequeue()) # conquer with recursion quick_sort(L) # sort elements less than pivot quick_sort(G) # sort elements greater than pivot # concatenate results while not L.is_empty(): S.enqueue(L.dequeue()) while not E.is_empty(): S.enqueue(E.dequeue()) while not G.is_empty(): S.enqueue(G.dequeue())
def levelorder(self): Q = LinkedQueue() t = self._root print(t._element, end="--") Q.enqueue(t) while not Q.is_empty(): t = Q.dequeue() if t._left: print(t._left._element, end="--") Q.enqueue(t._left) if t._right: print(t._right._element, end="--") Q.enqueue(t._right) return
class TestLinkedQueue(unittest.TestCase): def setUp(self): self.lq = LinkedQueue() self.lq.enqueue('A') self.lq.enqueue('B') def test_instantiation(self): print('Can create an instance') self.assertIsInstance(self.lq, LinkedQueue) def test_is_empty_method(self): print('Can check if the queue is empty') self.lq.dequeue() self.lq.dequeue() self.assertEqual(self.lq.is_empty(), True) self.assertNotEqual(len(self.lq), 1) def test_first_method(self): print('Can return the first element from the queue') self.lq.enqueue('C') self.lq.enqueue('D') self.assertEqual(self.lq.first(), 'A') def test_enqueue_method(self): print('Can add element to the end of the queue') self.lq.enqueue('E') self.lq.enqueue('F') self.assertEqual(len(self.lq), 4) def test_dequeue_method(self): print('Can remove element from the front of the queue') self.lq.dequeue() self.assertEqual(len(self.lq), 1) def test_exception_raising(self): self.lq.dequeue() with self.assertRaises(Empty): self.lq.dequeue() self.lq.first()
def quick_sort(S): n = len(S) if n < 2: return p = S.first() L = LinkedQueue() E = LinkedQueue() G = LinkedQueue() while not S.is_empty(): if S.first() < p: L.enqueue(S.dequeue()) elif p < S.first(): G.enqueue(S.dequeue()) else: E.enqueue(S.dequeue()) quick_sort(L) quick_sort(G) while not L.is_empty(): S.enqueue(L.dequeue()) while not E.is_empty(): S.enqueue(E.dequeue()) while not G.is_empty(): S.enqueue(G.dequeue())
def quick_sort(A): n = len(A) if n < 2: return pivot = A.first() L = LinkedQueue() E = LinkedQueue() G = LinkedQueue() while not A.is_empty(): if A.first() < pivot: L.enqueue(A.dequeue()) elif p < A.first(): G.enqueue(A.dequeue()) else: E.enqueue(A.dequeue()) quick_sort(L) quick_sort(G) while not L.is_empty(): A.enqueue(L.dequeue()) while not E.is_empty(): A.enqueue(E.dequeue()) while not G.is_empty(): A.enqueue(G.dequeue())
class TrieST: R = 256 class Node: def __init__(self): self.value = str() self.doc_list = [] self._next = [None] * TrieST.R def __init__(self): self.root = self.Node() self.number_of_keys = 0 self.valid_words = LinkedQueue() self.docs = LinkedList() def __sizeof__(self): return self.number_of_keys def __len__(self): return self.__sizeof__() def is_empty(self): return self.__sizeof__() == 0 def __getitem__(self, key, get_doc=None, trav=False): x = self.get(self.root, key, 0) if x is None: return None if get_doc is None and trav is False: return str(x.value) elif get_doc is not None: return x.doc_list elif trav: return x def __contains__(self, key): return self.__getitem__(key) is not None def get(self, x, key, d): if x is None: return None if d == len(key): return x char = key[d] return self.get(x._next[int(ord(char))], key, d + 1) def put(self, key, value, set_doc): if value is None: del key else: self.root = self.set(self.root, key, value, 0, set_doc) def set(self, x, key, value, d, set_doc): if x is None: x = self.Node() if d == len(key): if x.value is None: self.number_of_keys += 1 x.value = value if set_doc is not None: x.doc_list.append(set_doc) return x char = key[d] x._next[int(ord(char))] = self.set(x._next[int(ord(char))], key, value, d + 1, set_doc) return x def keys(self): return self.keysWithPrefix("") def keysWithPrefix(self, prefix): result = LinkedQueue() x = self.get(self.root, prefix, 0) self.collect(x, str(prefix), result) return result def collect(self, x, prefix, result): if x is None: return if x.value is not None: result.enqueue(str(prefix)) for i in range(self.R): prefix += chr(i) self.collect(x._next[i], prefix, result) prefix = prefix[:-1] def keysThatMatch(self, pattern): result = LinkedQueue() self.patternMatching(self.root, str(), pattern, result) return result def patternMatching(self, x, prefix, pattern, result): if x is None: return d = len(prefix) if d == len(pattern) and x.value is not None: result.enqueue(str(prefix)) if d == len(pattern): return char = pattern[d] if char == '.': for i in range(self.R): prefix += str(i) self.patternMatching(x._next[i], prefix, pattern, result) prefix = prefix[:-1] else: prefix += str(char) self.patternMatching(x._next[int(ord(char))], prefix, pattern, result) prefix = prefix[:-1] def longestPrefix(self, query): length = self.longestPrefixOf(self.root, query, 0, -1) if length == -1: return None else: return query[:length] def longestPrefixOf(self, x, query, d, length): if x is None: return length if x.value is not None: length = d if d == len(query): return length char = query[d] return self.longestPrefixOf(x._next[int(ord(char))], query, d + 1, length) def add_doc(self, doc_name): self.docs.append(doc_name) def traverse(self): query = self.keys() for q in query: if self[q.element] != '': yield(q.element) def validation(self): for v in self.traverse(): self.valid_words.enqueue(v) def delete(self, key, x=None, d=None): if x is None and d is None: self.root = self.delete(key, x=self.root, d=0) if x is None: return None if d == len(key): if x.value is not None: self.number_of_keys -= 1 x.value = None else: char = key[d] x._next[int(ord(char))] = self.delete(x._next[int(ord(char))], key, d + 1) # remove subtrie rooted at x if it is completely empty if x.value is not None: return x for i in range(self.R): if x._next[i] is not None: return x return None
""" find the second-to-last node in a singly linked queue or stack :param SL: singly linked queue or stack :return: """ assert isinstance(SL, (LinkedQueue, LinkedStack)), 'should be a singly linked queue or stack' current = None for i in SL: pre_current = current current = i return pre_current q = LinkedQueue() s = LinkedStack() for i in range(5): q.enqueue(i) s.push(i) print(q, s) print(second_to_last(q), second_to_last(s)) # R-7.2 # Describe a good algorithm for concatenating two singly linked lists L and # M, given only references to the first node of each list, into a single list LL # that contains all the nodes of L followed by all the nodes of M. def concatenate_linked_list(L, M): """ :param L: singly linked lists :param M: singly linked lists :return:
def queue_quick_sort(S: LinkedQueue): ''' running time is O(n^2) worst case in this implementation... In practice however, when L and G are similar size, the performance is O(nLogn) ''' n = len(S) if n < 2: return # pivot p = S.first() # less than pivot L = LinkedQueue() # equal to pivot E = LinkedQueue() # greater than pivot G = LinkedQueue() while not S.is_empty(): if S.first() < p: L.enqueue(S.dequeue()) elif p < S.first(): G.enqueue(S.dequeue()) else: E.enqueue(S.dequeue()) # recursion queue_quick_sort(L) queue_quick_sort(G) # concatenate results, less than, equal to, and then greater than in that order while not L.is_empty(): S.enqueue(L.dequeue()) while not E.is_empty(): S.enqueue(E.dequeue()) while not G.is_empty(): S.enqueue(G.dequeue()) # return sorted sequence return S
if S.first() < p: L.enqueue(S.dequeue()) elif p < S.first(): G.enqueue(S.dequeue()) else: E.enqueue(S.dequeue()) # recursion queue_quick_sort(L) queue_quick_sort(G) # concatenate results, less than, equal to, and then greater than in that order while not L.is_empty(): S.enqueue(L.dequeue()) while not E.is_empty(): S.enqueue(E.dequeue()) while not G.is_empty(): S.enqueue(G.dequeue()) # return sorted sequence return S testQueue = LinkedQueue([1, 2, 3]) for i in range(10, 0, -1): testQueue.enqueue(i) print(testQueue) queue_quick_sort(testQueue) print(testQueue)
return # divide S1 = LinkedQueue() S2 = LinkedQueue() while len(S1) < n//2: # move the first n//2 elements to S1 S1.enqueue(S.dequeue()) while not S.is_empty(): # move the rest to S2 S2.enqueue(S.dequeue()) # conquer with recursion merge_sort_linked(S1) # sort first half merge_sort_linked(S2) # sort second half merge_linked(S1, S2, S) # merge sorted halves back into S l = [1,4,9,6,3,7,8] L = LinkedQueue() for i in l: L.enqueue(i) merge_sort_linked(L) print(L) # a bottom-up (non-recursive) merge sort # faster than recursive merge-sort: avoids the extra overheads of recursive calls and temporary memory at each level # P 571 def merge_bottom_up(src, result, start, inc): """ merge src[start:(start+inc)] and src[(start+inc):(start+2*inc)] into result :param src: :param result: :param start: :param inc: :return:
def validation(self): for v in self.traverse(): self.valid_words.append(v) if __name__ == '__main__': tst = TST() tstStp = TST() fileQueue = LinkedQueue() fp = open("StopWords.txt", '+r') for line in fp.readlines(): key = (line.rstrip('\n\r')) fileQueue.enqueue(key) fp.close() i = 0 for q in fileQueue: tstStp.put(str(q.element), i, None) i += 1 tstStp.validation() counter = 0 for subdir, dirs, files in os.walk("/home/maometto/Documents/d"): for file in files: if file.endswith('.txt'): fp = open(os.path.join(subdir, file), 'r+') DATA = fp.read().replace('\n', ' ') for key in re.findall(r"[\w']+", DATA): if len(tstStp.keysThatMatch(key)) == 0: