def radixSort(alist): #intialize variables bins = [] largeQueue = Queue() result = [] for i in alist: """this loop populates 'largeQueue' with 'alist' values.""" largeQueue.enqueue(i) for x in xrange(10): """this loop populates 'bins' list with 10 queues.""" q = Queue() bins.append(q) for lsd in xrange(1,len(str(max(alist)))+1): """finds largest number in 'alist' to loop over correct number of times to sort all numbers contained in 'alist'. lsd is also used in calculations for modulo and division to extract each digit from pieces of elements within 'alist'.""" for index in xrange(len(alist)): """dequeues item from 'largeQueue' to be examined and put into proper "bin" in 'bins'.""" item = largeQueue.dequeue() element = item%(10**lsd) if lsd == 1: element = element else: element = element/(10**(lsd-1)) bins[element].enqueue(item) for y in xrange(10): """re-populates 'largeQueue' with elements of 'bins'.""" while len(bins[y]) > 0: largeQueue.enqueue(bins[y].dequeue()) for z in xrange(len(largeQueue)): """populates 'result' list from 'largeQueue'.""" result.append(largeQueue.dequeue()) return result
class Stack: def __init__(self): self._theItems = Queue() def isEmpty(self): return self._theItems.isEmpty() def __len__(self): return len(self._theItems) def peek(self): assert not self.isEmpty(), "Cannot peek at an empty stack" val = 0 n = len(self._theItems) for i in range(n): # pop and push to end val = self._theItems.dequeue() self.push(val) return val def pop(self): assert not self.isEmpty(), "Cannot pop from an empty stack" k = len(self._theItems) - 1 for i in range(k): self.push(self._theItems.dequeue()) return self._theItems.dequeue() def push(self, item): self._theItems.enqueue(item)
def bfs(m: maze): vis = set() # 访问过的点组成的集合,以防止重复访问 dis = dict() # 存储每个点距离源点的距离 q = Queue() dis[m.s] = 0 vis.add(m.s) q.push(m.s) while not q.empty(): now = q.pop() if now in m.gate: nextPoint = m.gate[now] if nextPoint not in vis: dis[nextPoint] = dis[now] vis.add(nextPoint) pre[nextPoint] = now q.push(nextPoint) # continue if now == m.t: # 到终点啦 recordPath(pre[m.t], m.s, m) # 打印路径,并保存在m.solution中 m.solution[m.t[0]][m.t[1]] = 2 # 终点加入路径 m.path.append((m.t[0], m.t[1])) m.dis = dis[now] + 1 return "Find Path!" for i in direction: nextPoint = (now[0] + i[0], now[1] + i[1]) if m.grid[nextPoint[0]][nextPoint[1]] == 0 or out(nextPoint,m): continue if nextPoint not in vis: dis[nextPoint] = dis[now] + 1 vis.add(nextPoint) pre[nextPoint] = now q.push(nextPoint) # end while return "Cannot find path"
def bfs(g,start): start.setDistance(0) start.setPred(None) vertQueue = Queue() vertQueue.enqueue(start) while (vertQueue.size() > 0): currentVert = vertQueue.dequeue() for nbr in currentVert.getConnections(): if (nbr.getColor() == 'white'): nbr.setColor('gray') nbr.setDistance(currentVert.getDistance() + 1) nbr.setPred(currentVert) vertQueue.enqueue(nbr) currentVert.setColor('black')
import webbrowser import os from dataStructures import Node, Queue view = Blueprint("view", __name__) secret = os.urandom(16) url_timestamp = {} url_viewtime = {} prev_url = "" start_date = time.ctime() start_day_number = start_date.split()[2] start = True inital = None hold = None q = Queue() CORS(app) def url_strip(url): if "http://" in url or "https://" in url: url = url.replace("https://", "").replace("http://", "").replace('\"', "") if "/" in url: url = url.split("/", 1)[0] if "." in url: url = url.replace(".", "_") return url @view.route('/send_url', methods=['POST'])
def __init__(self): self._theItems = Queue()
class Stack: ''' Stack ADT ''' ## Stack () produces an empty stack. ## __init__: None -> Stack def __init__(self): self.stack = Queue () ## isEmpty(self) returns True if the Stack is empty, False otherwise ## isEmpty: Stack -> Bool def isEmpty(self): return self.stack.isEmpty () ## len(self) returns the number of items in the stack. ## __len__: Stack -> Int def __len__(self): return self.stack.__len__() ## peek(self) returns a reference to the top item of the Stack without ## removing it. Can only be done on when Stack is not empty and does ## not modify the stack contents. ## peek: Stack -> Any ## Requires: Stack cannot be empty def peek(self): assert len(self.stack) > 0, "Cannot peek from empty Stack" delta = len(self.stack) - 1 while delta != 0: item = self.stack.dequeue() self.stack.enqueue(item) delta -= 1 item = self.stack.dequeue() self.stack.enqueue(item) return item ## pop(self) removes and returns the top (most recent) item of the Stack, ## if the Stack is not empty. The next (2nd most recent) item on the ## Stack becomes the new top item. ## pop: Stack -> Any ## Requires: Stack cannot be empty ## Effects: The next (2nd most recent) item on the Stack becomes the new ## top item. def pop(self): assert len(self.stack) > 0, "Cannot pop from Empty Stack." delta = len(self.stack) - 1 while delta != 0: item = self.stack.dequeue() self.stack.enqueue(item) delta -= 1 return self.stack.dequeue() ## push(self,item) adds the given item to the top of the Stack ## push: Stack Any -> None ## Effects: adds the given item to the top of the Stack def push(self, item): self.stack.enqueue(item) ## print(self) prints the items in the Stack (for testing purposes) ## __str__: Stack -> Str ## Requires: Stack cannot be empty def __str__(self): assert not self.isEmpty(), "Cannot print from an empty Stack." return self.stack.__str__()
def __init__(self): self.stack = Queue ()
from __future__ import print_function from dataStructures import Queue a = [] b = range(10) qa = Queue(a) qb = Queue(b) print("An empty queue: ", qa) print(type(qa)) print("Empty = ", qa.is_empty(), "Head = ", qa.get_head()) qa.showq() print("Push in: a") qa.enqueue('a') qa.showq() pop = qa.dequeue() print("Pop out: ", pop) qa.showq() print("An randon queue: ", qb) print("Empty = ", qb.is_empty(), "Head = ", qb.get_head(), "Tail = ", qb.get_tail()) qb.showq() print("Push in: a") qb.enqueue('a') qb.showq() pop = qb.dequeue()