def transform_bfs(start, target, h, t, flag):
    q = Queue()
    q.push(Word(start, True))
    counter = 0

    while not q.is_empty() and counter <= t:
        word = q.pop()
        options = generate_options(word.str)
        for opt in options:
            if opt in h:
                for candidate in h[opt]:
                    if candidate.visited != flag:
                        if candidate.visited == False:
                            if compare(candidate.str, target) == 1:
                                return word.ancestry + [
                                    word.str, candidate.str, target
                                ]
                            else:
                                candidate.visited = flag
                                candidate.ancestry = word.ancestry + [word.str]
                                q.push(candidate)
                                counter += 1
                        else:
                            return word.ancestry + [
                                word.str, candidate.str
                            ] + candidate.ancestry[::-1]
    return None
Esempio n. 2
0
def bfs(land, r, c, rows, cols):
    q = Queue()
    land[r][c] = False
    q.push((r, c))
    while not q.is_empty():
        x, y = q.pop()
        for i, j in [(x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y)]:
            if i in range(rows) and j in range(cols) and land[i][j]:
                land[i][j] = False
                q.push((i, j))
    return
Esempio n. 3
0
    def find_nearest_shop(self):
        visited = []
        queue = Queue()
        queue.push(self.__start_index)

        while queue.size() != 0:
            current = queue.pop()
            if current is not None:
                if self.__shop_lists[current] != 0:
                    return current
                for e in range(len(self.__map[current])):
                    if self.__map[current][e] != 0 and e not in visited:
                        queue.push(e)
            visited.append(current)
        return None
Esempio n. 4
0
class AnimalShelter:
    def __init__(self):
        self.dogs = Queue()
        self.cats = Queue()
        self.counter = 0

    def enqueue(self, *, name, species):
        if species in Animal.VALID_SPECIES:
            self.counter += 1
            getattr(self, f"{species}s").push(
                Animal(name=name, species=species, counter=self.counter))
        else:
            raise AttributeError("INVALID TYPE")

    def dequeue_cat(self):
        if self.cats.is_empty():
            raise IndexError("EMPTY CAT QUEUE")
        else:
            return self.cats.pop().name

    def dequeue_dog(self):
        if self.dogs.is_empty():
            raise IndexError("EMPTY DOG QUEUE")
        else:
            return self.dogs.pop().name

    def dequeue_any(self):
        if self.dogs.is_empty() and self.cats.is_empty():
            raise IndexError("EMPTY QUEUE")
        elif self.dogs.is_empty():
            return self.dequeue_cat()
        elif self.cats.is_empty():
            return self.dequeue_dog()
        else:
            return self.dequeue_cat() if self.dogs.peek(
            ).counter > self.cats.peek().counter else self.dequeue_dog()
def paint_fill_bfs(screen, x, y, new):
    old = screen[x][y]
    if old == new:
        return
    q = Queue()
    q.push((x, y))
    screen[x][y] = new

    while q.length > 0:
        r, c = q.pop()
        surrounding = [(r - 1, c), (r, c + 1), (r + 1, c), (r, c - 1)]
        for t, s in surrounding:
            if (t in range(len(screen)) and s in range(len(screen[0]))
                    and screen[t][s] == old):
                screen[t][s] = new
                q.push((t, s))
    return
Esempio n. 6
0
class Stack:

    def __init__(self):
        self.__stack = Queue()

    # Adds value to the end of the Stack.
    # Complexity: O(1)
    # push(value)
    def push(self, value):
        self.__stack.push(value)

    # Returns value from the end of the Stack and removes it.
    # Complexity: O(1)
    # pop()
    def pop(self, pop_type=None):
        new_stack = Queue()
        while self.__stack.size() > 0:
            current_value = self.__stack.pop()
            if self.__stack.size() == 0:
                if pop_type is not None:
                    new_stack.push(current_value)
                self.__stack = new_stack
                return current_value
            new_stack.push(current_value)

    # Returns value from the end of the Stack without removing it.
    # Complexity: O(1)
    def peek(self):
        return self.pop(1)

    # Returns the number of elements in the Queue.
    # Complexity: O(1)
    def size(self):
        return self.__stack.size()

    def getStack(self):
        return self.__stack.getQueue()