Exemplo n.º 1
0
def find_primes(maximum, _primes_=None, _changes_=0, _recursions_=0):
    #if on the first call, there will be no recursion
    if _primes_ == None:
        pass
    else:
        _recursions_ = _recursions_ + 1

    #If the max is 2 or 3, the algorithm following this section will not work.
    if (maximum == 2):
        result = Queues.Queue(1)
        result.enqueue(2)
        return result.queue
    elif (maximum == 3):
        result = [2, 3]
        return result

    #create return queue
    _queue2_ = Queues.Queue(maximum - _changes_ - 1)

    #if this is the first call in the recursion cycle, populate the queue
    if _primes_ == None:
        _primes_ = Queues.Queue(maximum - 1)
        for x in range(2, maximum + 1):
            _primes_.enqueue(x)

    #grab the first item from the queue
    first_item = _primes_.dequeue()
    done = True

    #the primary loop
    while (True):
        next_item = _primes_.dequeue()
        if fact.is_factor(next_item, first_item) == True:
            done = False
            _changes_ = _changes_ + 1
        else:
            _queue2_.enqueue(next_item)

        if (_primes_.isEmpty() == True):
            break
        else:
            pass
    #if a change was made above
    if (done == False):
        value = find_primes(maximum, _queue2_, _changes_, _recursions_)
        value.enqueue(first_item)
        if _recursions_ == 0:
            result = sort.stack_sort(value.queue)
            return result
        else:
            return value
    else:
        if _recursions_ == 0:
            result = sort.stack_sort(_queue2_.queue)
            return result
        else:
            _queue2_.enqueue(first_item)
            unsorted = _queue2_
            return unsorted
Exemplo n.º 2
0
    def __init__(self, name):
        # Fila de processos requisitando este recurso
        self.queue = Queues.Queue()

        # Processo usando o recurso atualmente
        self.process = None

        # Nome do recurso
        self.name = name
Exemplo n.º 3
0
def solvePrefix(expression):
    alphabet = [
        "a", "b", "c", "d", "e", "f", "g", "h", 'i', 'j', 'k', 'l', 'm', 'n',
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    ]
    numerals = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
    signQ = qz.Queue(len(expression))
    letters = stk.stack(len(expression))

    x = 0
    while (x < len(expression)):
        if (expression[x] == "*" or expression[x] == "-"
                or expression[x] == "+" or expression[x] == "/"
                or expression[x] == "^"):
            signQ.enqueue(expression[x])
        else:
            pass
        x = x + 1
    temp = ''
    for x in expression:
        #print("x = " + str(x))
        if (x.lower() in alphabet):
            letters.push(x)
        elif (x in numerals):
            temp = temp + str(x)
        else:
            if (temp != ''):
                letters.push(temp)
            if (x != ' '):
                v1 = letters.pop()
                v2 = letters.pop()

                sign = signQ.dequeue()
                try:
                    if (sign == "+"):
                        answer = int(v2) + int(v1)
                    elif (sign == "-"):
                        answer = int(v2) - int(v1)
                    elif (sign == "*"):
                        answer = int(v2) * int(v1)
                    elif (sign == "/"):
                        answer = int(v2) / int(v1)
                    elif (sign == "^"):
                        answer = int(v2)**int(v1)
                    else:
                        pass
                    letters.push(answer)
                except:
                    if (sign == "+" or sign == "-" or sign == "/"
                            or sign == "^"):
                        answer = str(v2) + ' ' + str(v1) + sign
                    elif (sign == "*"):
                        answer = str(v2) + ' ' + str(v1) + sign
                    letters.push(answer)

            temp = ''
    return letters.pop()
def Circles(item, num):
    q = Queues.Queue()
    for i in item:
        q.enqueue(i)
        # print(q.dequeue())
    while q.size() > 1:
        for i in range(num):
            q.enqueue(q.dequeue())
        q.dequeue()
    return q.dequeue()
def simulation(num_seconds, pages_per_minute):
    lab_printer = Printer.Printer(pages_per_minute)
    print_queue = Queues.Queue()
    waiting_times = []
    for current_second in range(num_seconds):
        if new_print_task():
            task = Task.Task(current_second)
            print_queue.enqueue(task)
        if (not lab_printer.busy()) and (not print_queue.is_empty()):
            next_task = print_queue.dequeue()
            waiting_times.append(next_task.wait_time(current_second))
            lab_printer.start_next(next_task)
        lab_printer.task()
    average_wait = sum(waiting_times) / len(waiting_times)
    print("Average Wait %6.2f secs %3d tasks remaining." %
          (average_wait, print_queue.size()))
Exemplo n.º 6
0
def main():
    frontier = Queues.Queue()
    cameFrom = {}
    start = Node(0)
    frontier.enqueue(start)
    cameFrom[start] = None

    while frontier.length() is not 0:
        current = frontier.dequeue()
        for next in graph.neighbors(current):
            if next not in cameFrom:
                cameFrom[next] = current
                frontier.enqueue(next)
    
    goal = Node(1)
    generatePath(goal,cameFrom, start)
Exemplo n.º 7
0
    def BuscaEmLargura(self, inicial, metas):
        fronteira = Queues.Queue()
        fronteira.put(inicial)
        veio_de = {}
        veio_de[inicial] = None

        while not fronteira.empty():
            self.explorados += 1
            atual = fronteira.get()

            if atual in metas:
                break

            for prox in self.g.grafo[atual]:
                if prox not in veio_de:
                    fronteira.put(prox)
                    veio_de[prox] = atual
        return atual, veio_de
def main():
    frontier = Queues.Queue()
    visited = {}
    start = Node(0)
    frontier.enqueue(start)
    visited[start] = True
    goal = Node(2)

    while frontier.length() is not 0:
        current = frontier.dequeue()
    
        # We can stop expanding the frontier as soon as we’ve found our goal. 
        if current == goal:
            break

        for next in graph.neighbors(current):
            if next not in visited:
                visited[next] = True
                frontier.enqueue(next)
Exemplo n.º 9
0
def solvePrefix(expression):
    alphabet = [
        "a", "b", "c", "d", "e", "f", "g", "h", 'i', 'j', 'k', 'l', 'm', 'n',
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    ]
    numerals = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
    signQ = qz.Queue(len(expression))
    letters = qz.Queue(len(expression))

    x = 0
    while (x < len(expression)):
        if (expression[x] == "*" or expression[x] == "-"
                or expression[x] == "+" or expression[x] == "/"
                or expression[x] == "^"):
            signQ.enqueue(expression[x])
        else:
            pass
        x = x + 1
    temp = ''
    for x in expression:
        print(letters.queue)
        print("x = " + str(x))
        if (x.lower() in alphabet):
            letters.enqueue(x)
        elif (x in numerals):
            temp = temp + str(x)
        else:
            if (temp != ''):
                letters.enqueue(temp)
            if (x != ' '):
                store = qz.Queue(letters.getLength() + 1)
                print(store.queue)
                for z in letters.queue:
                    if (z != None):
                        store.enqueue(z)
                value = len(letters.queue) - 1
                for y in range(0, letters.getLength()):
                    if (y != 0 and y != 1):
                        letters.dequeue()
                v1 = letters.dequeue()
                v2 = letters.dequeue()
                sign = signQ.dequeue()

                try:
                    print("AHGHDHGSJGH")
                    print("AHGJSFKHGJKLSF = " + str(x))
                    if (sign == "+"):
                        print("v1 = " + str(v1))
                        print("v2 = " + str(v2))
                        answer = int(v2) + int(v1)
                        print("answer = " + str(answer))
                        print("+")
                    elif (sign == "-"):
                        answer = int(v2) - int(v1)
                        print("-")
                    elif (sign == "*"):
                        answer = int(v2) * int(v1)
                        print("*")
                    elif (sign == "/"):
                        answer = int(v2) / int(v1)
                        print("/")
                    elif (sign == "^"):
                        answer = int(v2)**int(v1)
                        print("^")
                    else:
                        pass
                    print(store.queue)
                    for t in store.queue:
                        if (t != None):
                            print("OOF = " + str(t))
                            letters.enqueue(x)
                    letters.enqueue(answer)
                except:
                    if (sign == "+" or sign == "-" or sign == "/"
                            or sign == "^"):
                        answer = str(v2) + ' ' + str(v1) + sign
                    elif (sign == "*"):
                        answer = str(v2) + ' ' + str(v1) + sign
                    else:
                        pass
                    for x in store.queue:
                        if (x != None):
                            letters.enqueue(x)
                    letters.enqueue(answer)

            temp = ''
    return letters.dequeue()