class TestMyQueue(unittest.TestCase): def setUp(self): print "=========== Running Queue Test ===========" self.q = MyQueue() def tearDown(self): print "===========================================" def test_non_integer_raises_exception(self): # Ensure exception is thrown on non integer add to queue # State before: Empty Queue # State after: Empty Queue with self.assertRaises(Exception) as context: self.q.put("a") self.assertTrue(context.exception) def test_single_put_and_get(self): # Unit test single element can be added and removed # State before: Empty Queue # State after: Empty Queue self.q.put(1) self.assertEqual(self.q.get(),1) def test_10_elements(self): # Comprehensive test - add 10 elements and remove them, ensure they are in the correct order # State before: Empty Queue # State after: Empty Queue (again) in_elements = [] out_elements = [] for i in range(0,10): print "Putting %d in queue" % i self.q.put(i) in_elements.append(i) for i in range(0,10): popped = self.q.get() out_elements.append(popped) print "Popping %d from queue" % popped self.assertEqual(in_elements, out_elements)
class TestMyQueue(unittest.TestCase): def setUp(self): print "=========== Running Queue Test ===========" self.q = MyQueue() def tearDown(self): print "===========================================" def test_non_integer_raises_exception(self): # Ensure exception is thrown on non integer add to queue # State before: Empty Queue # State after: Empty Queue with self.assertRaises(Exception) as context: self.q.put("a") self.assertTrue(context.exception) def test_single_put_and_get(self): # Unit test single element can be added and removed # State before: Empty Queue # State after: Empty Queue self.q.put(1) self.assertEqual(self.q.get(), 1) def test_10_elements(self): # Comprehensive test - add 10 elements and remove them, ensure they are in the correct order # State before: Empty Queue # State after: Empty Queue (again) in_elements = [] out_elements = [] for i in range(0, 10): print "Putting %d in queue" % i self.q.put(i) in_elements.append(i) for i in range(0, 10): popped = self.q.get() out_elements.append(popped) print "Popping %d from queue" % popped self.assertEqual(in_elements, out_elements)
def __init__(self): self.currentTime = 0 self.eventList = [] self.statistics = Statistics() self.source = Source(self) self.cua_unica = MyQueue("cua_unica") self.caixer_cua_unica1 = Server(self, "caixer_cua_unica1") self.caixer_cua_unica2 = Server(self, "caixer_cua_unica2") self.caixer_cua_unica3 = Server(self, "caixer_cua_unica3") self.cua_unica.crearConnexio([ self.caixer_cua_unica1, self.caixer_cua_unica2, self.caixer_cua_unica3 ]) self.caixer_cua_unica1.crearConnexio(self.cua_unica) self.caixer_cua_unica2.crearConnexio(self.cua_unica) self.caixer_cua_unica3.crearConnexio(self.cua_unica) self.cua_caixer4 = MyQueue("cua_caixer4") self.caixer4 = Server(self, "caixer4") self.cua_caixer4.crearConnexio([self.caixer4]) self.caixer4.crearConnexio(self.cua_caixer4) self.cua_caixer5 = MyQueue("cua_caixer5") self.caixer5 = Server(self, "caixer5") self.cua_caixer5.crearConnexio([self.caixer5]) self.caixer5.crearConnexio(self.cua_caixer5) self.cua_caixer6 = MyQueue("cua_caixer6") self.caixer6 = Server(self, "caixer6") self.cua_caixer6.crearConnexio([self.caixer6]) self.caixer6.crearConnexio(self.cua_caixer6) self.source.crearConnexio([ self.cua_unica, self.cua_caixer4, self.cua_caixer5, self.cua_caixer6 ]) self.simulationStart = Event(self, TYPE_EVENT['start'], self.currentTime, None) self.eventList.append(self.simulationStart)
def shortest_path_search(start, successors, is_goal): if is_goal(start): return [start] explored = set([start]) frontier = MyQueue() frontier.enqueue([start]) while frontier: path = frontier.dequeue() s = path[-1] for (state, action) in successors(s).items(): if state not in explored: explored.add(state) path2 = path + [action, state] if is_goal(state): return path2 else: frontier.enqueue(path2) return [] # Fail
def setUp(self): print "=========== Running Queue Test ===========" self.q = MyQueue()
def __init__(self): self.size = 0 self.queue1 = MyQueue() self.queue2 = MyQueue()
class TwoQueStack(object): def __init__(self): self.size = 0 self.queue1 = MyQueue() self.queue2 = MyQueue() def empty(self): if self.size == 0: return True return False def push(self, arg): if self.queue1.isEmpty() and self.queue2.isEmpty(): target = self.queue1 else: target = self.queue1 if not self.queue1.isEmpty() else self.queue2ssss target.offer(arg) self.size += 1 def pop(self): if self.empty(): return if self.queue1.isEmpty(): i = self.size while i > 1: self.queue1.offer(self.queue2.pull()) i -= 1 self.queue2.pull() self.size -= 1 else: i = self.size while i > 1: self.queue2.offer(self.queue1.pull()) i -= 1 self.queue1.pull() self.size -= 1 def top(self): if self.empty(): return if self.queue1.isEmpty(): i = self.size while i > 1: self.queue1.offer(self.queue2.pull()) i -= 1 print self.queue2.peek() self.queue1.offer(self.queue2.pull()) else: i = self.size while i > 1: self.queue2.offer(self.queue1.pull()) i -= 1 print self.queue1.peek() self.queue2.offer(self.queue1.pull())
def a_star(self): que = MyQueue() # kolejka wierzchołków do sprawdzenia finished = MyQueue() # kolejka skończonych counter = 0 added_to_frontier = [] que.insert( Vertex(index=self.start, previous=-1, graph_distance=0, heuristic_distance=self.manhattan(self.start, self.target), path='')) added_to_frontier.append(self.start) while not que.is_empty(): current = que.remove() counter += 1 current.considered = counter if current.index != self.target: for child in self.adjacency_list[current.index]: if not que.contains(child[0]) and not finished.contains( child[0]): que.insert( Vertex( index=child[0], previous=current.index, graph_distance=current.graph_distance + int(child[1]), heuristic_distance=self.manhattan( child[0], self.target), path= f'{current.path}{LETTER_INDEXES[current.index]} -> ' )) added_to_frontier.append(child[0]) elif que.contains(child[0]): if current.distance + child[1] < que[ child[0]].distance: que.set_distance(child[0], current.distance + child[1]) que.set_previous( child[0], current.index) # aktualizuj poprzednika que.set_path( child[0], f'{current.path}{LETTER_INDEXES[current.index]} -> ' ) # aktualizuj sciezke finished.insert(current) else: while not que.is_empty(): finished.insert(que.remove()) for index in added_to_frontier: print(f'{finished[index].considered}, {finished[index]}') print( f'najkrótsza znaleziona ścieżka z {LETTER_INDEXES[self.start]} do {LETTER_INDEXES[self.target]}:' ) print(str(current.path) + str(LETTER_INDEXES[self.target])) return str(current.path) + str(LETTER_INDEXES[self.target]) for index in added_to_frontier: print(f'{finished[index].considered}, {finished[index]}') print('ścieżka nie została odnaleziona') return 'brak ścieżki'
def main(): print("Main zyje!") # Tworzymy kolejki ktore beda synchronizowaly nasze watki i pozwala nam na komunikacje miedzy nimi # s1 r1 - wyslanie/pobranie dla klienta 1 # s1 r1 - wyslanie/pobranie dla klienta 2 s1 = MyQueue() r1 = MyQueue() s2 = MyQueue() r2 = MyQueue() # odpalamy watki odpowiadajace za komunikacje t1 = _thread.start_new_thread(HOST.start, (s1, r1, s2, r2)) # tworzymy dodatkowe 2 kolejki tym razem do komunikacji bezposiernio z gra control1 = MyQueue() control2 = MyQueue() # do tej kolejki bedzie trafialo wyjscie z serwera output = MyQueue() # odpalamy gre t2 = _thread.start_new_thread(game.game, (control1, control2, output)) # ponizej sprawdzamy czy w kolejce pojawily sie jakies odebrane dane jesli tak dekodujemy je i wysylamy dalej while True: try: data = r1.get(block=False) # sprawdzamy czy klient 1 coś wysłał new_control = pickle.loads(data) control1.put( new_control) # wstawiamy dane z klienta 1 do kolejki gry except queue.Empty: pass try: data = r2.get(block=False) # sprawdzamy czy klient 2 coś wysłał new_control = pickle.loads(data) control2.put( new_control) # wstawiamy dane z klienta 2 do kolejki gry except queue.Empty: pass try: data = output.get(block=False) # sprawdzamy kolejke gry binary = pickle.dumps(data) binary = bytes(f"{len(binary):<{HEADERSIZE}}", 'utf-8') + binary s1.put(binary) # wysyłamy do klienta 1 s2.put(binary) # wysyłamy do klienta 2 except queue.Empty: pass