Example #1
0
    def empty(self):
        self.mutex.acquire()
        while self._qsize() and self.queue[0] == End:
            self._runlevel -= 1
            Queue._get(self)
        self.mutex.release()

        return Queue.empty(self)
Example #2
0
    def empty(self):
        self.mutex.acquire()
        while self._qsize() and self.queue[0] == End:
            self._runlevel -= 1
            Queue._get(self)
        self.mutex.release()

        return Queue.empty(self)
Example #3
0
 def _get(self):
     eval_sess, is_reeval = Queue._get(self)
     if is_reeval:
         assert self._curr_eval_sess is eval_sess
     else:
         self._curr_eval_sess = eval_sess
     return eval_sess, is_reeval
Example #4
0
def getClosestFood(currentGameState, successorGameState,posX,posY):
    dict = {(posX,posY):1}
    posList = Queue()
    posList._put(((posX,posY),0))
    walls = successorGameState.getWalls()
    food = currentGameState.getFood()
    if food[posX][posY] == True:
        return 1
    check = False
    closestFood = 99999
    max = 20
    current = 0
    #from the position of pacman, expands on all sides with a valid path. efficient for dense food positions
    while not posList.empty():
        ((currX, currY),x) = posList._get()
        current = x+1
        if(food[currX][currY] == True):
            check = True
            closestFood = min(closestFood,current)
        if check == False and (currX-1,currY) not in dict.keys() and walls[currX-1][currY] != True:
            posList._put(((currX-1,currY),current))
            dict[(currX-1,currY)] = 1
        if check == False and (currX,currY-1) not in dict.keys()and walls[currX][currY-1] != True:
            posList._put(((currX,currY-1),current))
            dict[(currX,currY-1)] = 1
        if check == False and (currX+1,currY) not in dict.keys() and walls[currX+1][currY] != True:
            posList._put(((currX+1,currY),current))
            dict[(currX+1,currY)] = 1
        if check == False and(currX,currY+1) not in dict.keys() and walls[currX][currY+1] != True:
            posList._put(((currX,currY+1),current))
            dict[(currX,currY+1)] = 1
    return 1.0/closestFood
def find_max_sum_level(root):
    li = Queue()
    level = cl = 1
    sum = max_sum = 0
    li._put(root)
    li._put(None)
    while li.empty() != True:
        root = li._get()
        if root == None:
            cl += 1
            if sum > max_sum:
                max_sum = sum
                level = cl
            sum = 0
            if li.empty() != True:
                li._put(None)
        else:
            sum += root.data
            if root.left:
                li._put(root.left)
            if root.right:
                li._put(root.right)

    print max_sum
    print level
Example #6
0
 def _get(self):
     eval_sess, is_reeval = Queue._get(self)
     if is_reeval:
         assert self._curr_eval_sess is eval_sess
     else:
         self._curr_eval_sess = eval_sess
     return eval_sess, is_reeval
Example #7
0
    def _get(self):
        """
        A thread-safe implementation override of dequeueing.
        When queue pops an item, it is also removed from the set

        """
        item = Queue._get(self)
        self.all_items.remove(item)
        return item
Example #8
0
def lot(root):
    li = Queue()
    li._put(root)
    while li.empty() != True:
        root = li._get()
        print root.data
        if root.left:
            li._put(root.left)
        if root.right:
            li._put(root.right)
def mirror_tree_non_r(root, root2):
    s1 = Queue()
    s1._put(root)
    s2 = Queue()
    s2._put(root2)
    while s1.empty() != True and s2.empty() != True:
        if s1.empty() or s2.empty():
            return False
        root = s1._get()
        root2 = s2._get()
        if root.data != root2.data:
            return False
        if root.left:
            s1._put(root.left)
        if root2.right:
            s2._put(root2.right)
        if root.right:
            s1._put(root.right)
        if root2.left:
            s2._put(root2.left)
    return True
 def level_order_traversal(self, root):
     s = Queue()
     s._put(root)
     while (True):
         root = s._get()
         print root.data
         if root.left is not None:
             s._put(root.left)
         if root.right is not None:
             s._put(root.right)
         if s.empty():
             break
Example #11
0
def getClosestFood(currentGameState, successorGameState, posX, posY):
    dict = {(posX, posY): 1}
    posList = Queue()
    posList._put(((posX, posY), 0))
    walls = successorGameState.getWalls()
    food = currentGameState.getFood()
    if food[posX][posY] == True:
        return 1
    check = False
    closestFood = 99999
    max = 20
    current = 0
    while not posList.empty():
        #print "entered"
        ((currX, currY), x) = posList._get()
        current = x + 1
        # if current >10:
        #     break
        if (food[currX][currY] == True):
            check = True
            closestFood = min(closestFood, current)
        if check == False and (
                currX - 1,
                currY) not in dict.keys() and walls[currX - 1][currY] != True:
            posList._put(((currX - 1, currY), current))
            dict[(currX - 1, currY)] = 1
        if check == False and (
                currX, currY -
                1) not in dict.keys() and walls[currX][currY - 1] != True:
            posList._put(((currX, currY - 1), current))
            dict[(currX, currY - 1)] = 1
        if check == False and (
                currX + 1,
                currY) not in dict.keys() and walls[currX + 1][currY] != True:
            posList._put(((currX + 1, currY), current))
            dict[(currX + 1, currY)] = 1
        if check == False and (
                currX, currY +
                1) not in dict.keys() and walls[currX][currY + 1] != True:
            posList._put(((currX, currY + 1), current))
            dict[(currX, currY + 1)] = 1
    # if closestFood <3:
    #     closestFood = 7-closestFood
    # elif closestFood < 10:
    #     closestFood = 5-closestFood/2
    #
    # elif closestFood >10:
    #     closestFood = 5-closestFood/4
    return 1.0 / closestFood
def print_reverse_level_order(root):
    li = Queue()
    li._put(root)
    sl = []
    height = 0
    while li.empty() != True:
        root = li._get()
        sl.append(root.data)
        if root.right:
            li._put(root.right)
        if root.left:
            li._put(root.left)

    print "height is " + str(height)

    print sl
 def insert_node(self, x):
     node = Node(x)
     if self.root is None:
         self.root = node
         return
     q = Queue()
     q._put(self.root)
     while (True):
         curr = q._get()
         if curr.left is None:
             curr.left = node
             return
         elif curr.right is None:
             curr.right = node
             return
         else:
             q._put(curr.left)
             q._put(curr.right)
Example #14
0
def getClosestFood(currentGameState, successorGameState,posX,posY):
    dict = {(posX,posY):1}
    posList = Queue()
    posList._put(((posX,posY),0))
    walls = successorGameState.getWalls()
    food = currentGameState.getFood()
    if food[posX][posY] == True:
        return 1
    check = False
    closestFood = 99999
    max = 20
    current = 0
    while not posList.empty():
        #print "entered"
        ((currX, currY),x) = posList._get()
        current = x+1
        # if current >10:
        #     break
        if(food[currX][currY] == True):
            check = True
            closestFood = min(closestFood,current)
        if check == False and (currX-1,currY) not in dict.keys() and walls[currX-1][currY] != True:
            posList._put(((currX-1,currY),current))
            dict[(currX-1,currY)] = 1
        if check == False and (currX,currY-1) not in dict.keys()and walls[currX][currY-1] != True:
            posList._put(((currX,currY-1),current))
            dict[(currX,currY-1)] = 1
        if check == False and (currX+1,currY) not in dict.keys() and walls[currX+1][currY] != True:
            posList._put(((currX+1,currY),current))
            dict[(currX+1,currY)] = 1
        if check == False and(currX,currY+1) not in dict.keys() and walls[currX][currY+1] != True:
            posList._put(((currX,currY+1),current))
            dict[(currX,currY+1)] = 1
    # if closestFood <3:
    #     closestFood = 7-closestFood
    # elif closestFood < 10:
    #     closestFood = 5-closestFood/2
    #
    # elif closestFood >10:
    #     closestFood = 5-closestFood/4
    return 1.0/closestFood
Example #15
0
def getClosestFood(currentGameState, successorGameState, posX, posY):
    dict = {(posX, posY): 1}
    posList = Queue()
    posList._put(((posX, posY), 0))
    walls = successorGameState.getWalls()
    food = currentGameState.getFood()
    if food[posX][posY] == True:
        return 1
    check = False
    closestFood = 99999
    max = 20
    current = 0
    #from the position of pacman, expands on all sides with a valid path. efficient for dense food positions
    while not posList.empty():
        ((currX, currY), x) = posList._get()
        current = x + 1
        if (food[currX][currY] == True):
            check = True
            closestFood = min(closestFood, current)
        if check == False and (
                currX - 1,
                currY) not in dict.keys() and walls[currX - 1][currY] != True:
            posList._put(((currX - 1, currY), current))
            dict[(currX - 1, currY)] = 1
        if check == False and (
                currX, currY -
                1) not in dict.keys() and walls[currX][currY - 1] != True:
            posList._put(((currX, currY - 1), current))
            dict[(currX, currY - 1)] = 1
        if check == False and (
                currX + 1,
                currY) not in dict.keys() and walls[currX + 1][currY] != True:
            posList._put(((currX + 1, currY), current))
            dict[(currX + 1, currY)] = 1
        if check == False and (
                currX, currY +
                1) not in dict.keys() and walls[currX][currY + 1] != True:
            posList._put(((currX, currY + 1), current))
            dict[(currX, currY + 1)] = 1
    return 1.0 / closestFood
Example #16
0
 def _get(self):
     item = Queue._get(self)
     return item
 def _get(self):
     item = Queue._get(self)
     pkg_name = item['name']
     self.all_items.discard(pkg_name)
     return item
Example #18
0
 def _get(self):
     item = Queue._get(self)
     self._get_done()
     return item
Example #19
0
 def _get(self):
     item = Queue._get(self)
     if item:
         self.all_items.remove(item)
     return item
Example #20
0
 def _get(self):
     item = Queue._get(self)
     self._get_done()
     return item
Example #21
0
	def _get(self):
		Queue._get(self)
		if self._qsize():
			self.emit(qtcore.SIGNAL("datainqueue()"))