Exemplo n.º 1
0
def main():
    line = lambda: stdin.readline().split()
    n, k = map(int, line())

    # Patients
    patients = []
    # Accidents
    accidents = {}
    # Times
    times = []
    # Output list for fast printing
    outs = []

    # Read in input
    for _ in range(n):
        inp = line()
        if inp[0] == "1":
            _, t, name, s = inp
            patients.append(tup(int(s), int(t), name))
        elif inp[0] == "2":
            times.append(int(inp[1]))
        else:
            _, t, name = inp
            accidents[name] = int(t)

    # t values given in increasing order
    # Check the last time value to get the severity cost of EVERY patient
    # Because extra time added after the first time they can be served is equal for all patients,
    # Adding them as soon as they are available alleviates the need to update the cost later
    last_time = times[-1]
    for patient in patients:
        patient.s += (last_time - patient.t) * k

    # Use a heap to store all patients who can currently be served
    # Update the heap at each new serve-time
    q = []
    # Index for the current patient
    i = 0
    for j in range(len(times)):
        curr_time = times[j]

        # Find all patients who can now be served at this time
        while (i < len(patients) and patients[i].t <= curr_time):
            push(q, patients[i])
            i += 1

        # Find the best patient to serve, or take a break if there are none
        while (q and q[0].name in accidents
               and accidents[q[0].name] <= curr_time):
            # Remove patients who have had accidents and must leave the clinic
            pop(q)

        if q:
            best = pop(q)
            outs.append(best.name)
        else:
            outs.append("doctor takes a break")

    # Print all output at once, helps for large output sizes
    print("\n".join(outs))
Exemplo n.º 2
0
    def minMeetingRooms(self, intervals: List[List[int]]) -> int:

        # base case - If there is no meeting to schedule then no room needs to be allocated.
        if not intervals:
            return 0

        # The heap initialization
        free_rooms = []

        # Sort the meetings in increasing order of their start time.
        intervals.sort(key=lambda x: x[0])

        # Add the first meeting. We have to give a new room to the first meeting.
        push(free_rooms, intervals[0][1])

        # For all the remaining meeting rooms
        for i in intervals[1:]:
            # end time in heap <= start time of interval
            if free_rooms[0] <= i[0]:
                pop(free_rooms)

            # for the existing room or the new room
            push(free_rooms, i[1])

        return len(free_rooms)
Exemplo n.º 3
0
def func():
    t = int(input())
    res_for_test_cases = []
    for i in range(t):
        n = int(input())
        jobs = []
        for j in range(n):
            jobtemp = input().split()
            jobs.append([int(i) for i in jobtemp])
        totrunningtime = 0
        totalmoney = 0
        jobs = jobs.sorted(key=sortbydeadline)
        priorityq = []
        heapq.heapify(priorityq)
        for job in jobs:
            # heapq.push((job[-1],job[0],job[1]))
            totrunningtime += jobs[1]
            if totrunningtime < job[-1]:
                heapq.push(priorityq, (job[-1], job[0], job[1]))
            else:
                highest_a_job = heapq.pop(priorityq)
                if totrunningtime - highest_a_job[2] < job[-1]:
                    time_to_dec = totrunningtime - job[-1]
                    amount = (highest_a_job[-1] -
                              time_to_dec) / highest_a_job[1]
                    totalmoney += amount
                    heapq.push(priorityq, (highest_a_job[1], highest_a_job[0],
                                           highest_a_job[1] - time_to_dec))
                    print('totalmoney: ', totalmoney)
                    for i in range(t):
                        print(res_for_test_cases[i])

                    func()
Exemplo n.º 4
0
def traverse(figure, n, m):

    dis = [-1 for i in xrange(n * m)]

    q = []

    G = n * m - 1

    push(q, (0, 0))

    dis[0] = 0

    while (len(q) > 0):

        d, v = pop(q)

        for u in figure[v]:

            if (dis[u] > d + 1) or -1 == dis[u]:

                dis[u] = d + 1

                if u == G:
                    return d + 1

                push(q, (dis[u], u))

    return dis[-1]
Exemplo n.º 5
0
def searchEM(board, nodesToExpand=10000):
    startNode = createFunction(board)

    heap = []
    push(heap, (-startNode.score, startNode))
    i = 0
    while i < nodesToExpand:
        start = time.time()
        if len(heap) == 0:
            break
        thisNode = pop(heap)[1]
        if not thisNode.expanded:
            thisNode.expand()
            for a in ["L", "R", "D", "U"]:
                for childNode, _ in thisNode.children[a]:
                    push(heap, (-childNode.score, childNode))
            i += 1

            # print "time to expand:" , time.time()-start

    Node.allNodes = {}

    # print (startNode.bestDir)
    # pdb.set_trace()
    startNode.downdate()
    return board.move(startNode.bestDir)
Exemplo n.º 6
0
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:

        #   custom less than comparator using a lambda function taking ListNode objects x and y as parameters
        lessThan = lambda x, y: x.val < y.val
        ListNode.__lt__ = lessThan

        #   initialize an empty list which acts as min heap
        minHeapK = []

        #   create a dummy head and start the cursor from that node
        dummyHead = ListNode(-1)
        cursorNode = dummyHead

        #   push into minHeap all head nodes (take care of null checks)
        for i in range(len(lists)):
            currHead = lists[i]
            if (currHead != None):
                push(minHeapK, currHead)

        #   pop min node and add it to the main linked list, and also push its next node into the min heap (if not null)
        while (len(minHeapK) > 0):

            minNode = pop(minHeapK)  #   pop the min node in the heap

            cursorNode.next = minNode
            if (minNode.next !=
                    None):  #   push to the heap only if next node exists
                push(minHeapK, minNode.next)

            cursorNode = minNode  #   update cursor node

        #   return dummy head's next node
        return dummyHead.next
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        if not lists or len(lists) is 0:
            return None

        # custom comparator for list node
        ListNode.__lt__ = lambda x, y: x.val < y.val

        # min heap is required to sort in ascending order
        min_heap = []

        # push only head nodes of the lists
        for head in lists:
            if head:
                push(min_heap, head)

        dummy_head = current = ListNode(-1)

        while len(min_heap) > 0:
            min_node = pop(min_heap)
            current.next = min_node

            if min_node.next:
                push(min_heap, min_node.next)

            current = current.next

        return dummy_head.next
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:

        #   edge case check
        if (matrix == None or len(matrix) == 0):
            return -1

        #   initializations
        nRows = len(matrix)
        nCols = len(matrix[0])
        kSmall = []

        #   first column elements inside the minHeap
        for r in range(nRows):
            currObj = ValueCell(matrix[r][0], r, 0)
            push(kSmall, currObj)

        currObj = None

        #   remove min from heap k times and insert next min element k times
        for i in range(k):
            currObj = pop(kSmall)  #   remove min element
            row = currObj.nRows  #   extract row and col of extracted min element
            col = currObj.col

            if (col + 1 <
                    nCols):  #   if next col is valid => push next col's value
                nextObj = ValueCell(matrix[row][col + 1], row, col + 1)
                push(kSmall, nextObj)

            else:  #   else min heap's size is reduced by one
                pass

        return currObj.val  #   return kth min object's value
def MST(graph):
    cost = 0
    sub_graph = {list(graph.keys())[0]: graph[list(graph.keys())[0]]}
    added = {list(graph.keys())[0]}
    heap = []
    counter = {}
    for vertex in graph.keys():
        push(heap, (inf, vertex))
        counter[vertex] = inf
    while (len(sub_graph) != len(graph)):
        for edges in sub_graph.values():
            for edge in edges:
                if edge[0] not in added:
                    heap.remove((counter[edge[0]], edge[0]))
                    new_val = min(edge[1], counter[edge[0]])
                    push(heap, (new_val, edge[0]))
                    counter[edge[0]] = new_val
        vertex = pop(heap)
        cost += vertex[0]
        try:
            sub_graph[vertex[1]] = graph[vertex[1]]
        except KeyError:
            pass
        added.add(vertex[1])

    return cost
 def add(self, value):
     value = pushpop(self.upper, value)
     value = -pushpop(self.lower, -value)
     if len(self.upper) <= len(self.lower):
         push(self.upper, value)
     else:
         push(self.lower, -value)
Exemplo n.º 11
0
def main():
  n = int(sys.stdin.readline().strip())
  coming_up = defaultdict(int)
  vs = []
  leaves = set(range(1,n+2))
  for line in sys.stdin:
    v = int(line.strip())
    coming_up[v] += 1
    vs.append(v)
    if v in leaves:
      leaves.remove(v)

  if vs[-1] != n+1:
    print "Error"
    return

  avail = []
  for i in leaves:
    push(avail, i)

  soln = []
  for i in vs:
    if len(avail) == 0:
      print "Error"
      return
    soln.append(str(pop(avail)))
    coming_up[i] -= 1
    if coming_up[i] == 0:
      push(avail, i)

  print "\n".join(soln)
Exemplo n.º 12
0
def add(value):
    value = pushpop(upperHalf, value)
    value = -pushpop(lowerHalf, -value)
    if (len(upperHalf) <= len(lowerHalf)):
        push(upperHalf, value)
    else:
        push(lowerHalf, -value)
Exemplo n.º 13
0
    def solve(self, A, B, C):
        dist = [inf] * A
        dist[C] = 0
        heapq.push((0, C))
        visited = [False] * A
        minHeap = [(0, C)]
        heapq.heapify(minHeap)
        adj = [[] for i in range(len(B))]

        for i in range(len(B)):
            src = B[i][0]
            dest = B[i][1]
            wt = B[i][2]
            adj[src].append((dest, wt))
            adj[dest].append((src, wt))

        while len(minHeap) > 0:
            curr_dist, curr_node = heapq.heappop(minHeap)
            if visited[curr_node] == True:
                continue
            visited[curr_node] = True
            for neighbor in adj[curr_node]:
                neighbor_node = neighbor[0]
                neighbor_dist = neighbor[1]
                if dist[neighbor_node] > curr_dist + neighbor_dist:
                    dist[neighbor_node] = curr_dist + neighbor_dist
                    heapq.heappush(minHeap,
                                   (dist[neighbor_node], neighbor_node))

        for index, dist_obj in enumerate(dist):
            if dist_obj == inf:
                dist[index] = -1

        return dist
Exemplo n.º 14
0
    def traverser(self, graph, source, dest):
        queue = []
        start_heuristic = float(graph.node[source]['heuristic'])
        # Queue item (heuristic, (node, cost, parent))
        push(queue, (start_heuristic, (source, 0, None)))

        while queue:
            print(queue)
            currentnode, cost, parent = pop(queue)[1]
            print(currentnode)
            # goal check
            if (currentnode == dest):
                self.visited[currentnode] = parent
                self.path = [currentnode]
                origin = parent
                while origin is not None:
                    self.path.append(origin)
                    origin = self.visited[origin]
                self.path.reverse()
                break

            if (currentnode not in list(self.visited.keys())):
                self.visited[currentnode] = parent

            # check neighbours
            # Items returns (neighbornode, {dict of edge attributes})
            for neighbor, data in graph[currentnode].items():
                nheuristic = float(graph.node[neighbor]['heuristic'])
                ncost = cost + float(data['weight'])
                #  creating list with nodes in the queue
                queuenodes = [tuple[1][0] for tuple in queue]
                if (neighbor not in list(self.visited.keys())
                        and neighbor not in queuenodes):
                    # push to the queue with heuristic, cost ,parent
                    push(queue, (nheuristic, (neighbor, ncost, currentnode)))
Exemplo n.º 15
0
 def gen():
     # Highest element of low is at its root and
     # Lowest element of high is at its root
     low, high = [], [(yield)]
     while True:
         # Yield high if high > low
         push(low, -pushpop(high, (yield high[0] * 1.0)))
         # Else yield average of roots
         push(high, -pushpop(low, -(yield ((high[0] - low[0]) / 2.0))))
Exemplo n.º 16
0
def AEstrella(origen, funcionParo, g, h):
    '''Función que implementa el algoritmo A*

  Parámetros
  origen: estado desde el que se inicia
  fucionParo: función que nos indica si llegamos a un estado meta
  g: función de costo acumulado
  h: función heuristica

  return solución o nada
  '''
    historia = [(0, 0)]
    # solución trivial
    if funcionParo(origen):
        return trayectoria(origen), historia

    # inicializamos al agenda
    agenda = []

    # inicializamos el conjunto de expandidos
    expandidos = set()

    # generamos la función f(s) = g(s) + h(s)
    # f(s) representa el costo total a un nodo
    # costo acumulado más el costo de la heuristica a ese nodo
    # esto nos representará la prioridad para cada nodo
    f = lambda s: g(s) + h(s)

    # agregamos el primer nodo a la agenda
    push(agenda, (f(origen), origen))

    # mientras existan elementos en la agenda
    while agenda:
        historia.append((len(agenda), len(expandidos)))
        # sacamos un nodo de la agenda
        nodo = pop(agenda)[1]  # pos 1 para obtener el estado de la tupla

        # lo agregamos al conjunto de expandidos
        expandidos.add(nodo)

        # comparamos si este nodo cumple con la función de paro
        if funcionParo(nodo):
            return trayectoria(nodo), historia
        '''Comparamos si el nodo cumple con la función de paro aquí dado que el 
    algoritmo termina cuando el nodo objetivo o estado meta se encuentra en 
    la cabeza de la cola de prioridad, esto ocurrirá cuando el nodo sea el 
    siguiente en salir, por eso se compará afuera del ciclo'''
        # expandimos el nodo y comparamos a sus hijos
        for hijo in nodo.expandir():

            # agregamos el hijo a la cola de prioridad
            # siempre y cuando no esté en el conjunto de expandidos
            if hijo not in expandidos:
                push(agenda, (f(hijo), hijo))

    # si no hay ruta, regresamos un vacio
    return
Exemplo n.º 17
0
def kNearestPoints(points, k):
    heap = []
    result = []
    for i in range(len(points)):
        x, y = points[i]
        dist = x * x + y * y
        push(heap, (dist, points[i]))
    for j in range(k):
        result.append(pop(heap)[1])
    return result
Exemplo n.º 18
0
 def predict(self, new_X, k, dist_measure):
     assert k > 0
     assert dist_measure in dist_measures
     heap = []
     new_X = (new_X - self.means) / self.stds
     for i in range(len(self.X)):
         dist = dist_measures[dist_measure](self.X[i], new_X)
         push(heap, (dist, i))
     labels = list(map(lambda i: self.Y[pop(heap)[1]], range(k)))
     return np.bincount(labels).argmax()
Exemplo n.º 19
0
def huffman(freq):
    global tree
    if len(freq) == 2:
        expand(freq[0], freq[1])
    else:
        a = pop(freq)
        b = pop(freq)
        push(freq, (a[0] + b[0], a[1] + " " + b[1]))
        huffman(freq)
        expand(a, b)
Exemplo n.º 20
0
 def predict(self, new_X, k, dist_measure):
     assert k > 0
     assert dist_measure in dist_measures
     heap = []
     new_X = (new_X - self.means) / self.stds
     for i in range(len(self.X)):
         dist = dist_measures[dist_measure](self.X[i], new_X)
         push(heap, (dist, i))
     labels = list(map(lambda i: self.Y[pop(heap)[1]], range(k)))
     return np.bincount(labels).argmax()
Exemplo n.º 21
0
 def findKthLargest(self, nums: List[int], k: int) -> int:
     # list to act as min heap
     hq = []
     # adding k elements to the heap
     for i in range(len(nums)):
         push(hq, nums[i])
         # if elements in heap exceed k remove the smallest
         if len(hq) > k:
             pop(hq)
     # returning the smallest element
     return hq[0]
Exemplo n.º 22
0
def Astar(start,goal):
  q = [start]
  visited = {}
  while q:
    node = pop(q)
    if node == goal:
      return node
    for step in node.steps():
      if step not in visited or visited[step] > step:
        push(q,step)
        visited[step] = step
Exemplo n.º 23
0
def k_largest(stream, k):
    q = []

    for elem in stream:
        if len(q) < k:
            push(q, elem)
        elif q[0] < elem:
            pop(q)
            push(q, elem)

    return q
Exemplo n.º 24
0
def solution(o, i=0, sh=set(), nh=[], xh=[]):
    for e in o:
        if (v := e.split(" "))[0] == "I":
            push(nh, (int(v[1]), i))
            push(xh, (-int(v[1]), i))
            sh.add(i)
            i += 1
        elif sh:
            m = xh if v[1] == "1" else nh
            while (k := pop(m))[1] not in sh:
                pass
Exemplo n.º 25
0
def dijkstra(g,raizes):
    '''
    Parametro: g, representando um grafo
    Retorno: arvore, arvore de caminhos minimos
    Função: Retorna a SPT (Shortest Path Tree) ou arvore de caminhos minimos
    do grafo recebido como parametro
    '''
    lista_prioridades = [(Inf,i) for i in range(int(nx.number_of_nodes(g)))]
    # lista_prioridades transformar em heap
    heapify(lista_prioridades) 
    # inserir nó 0 (assumindo que o nó 0 é a raiz) com prioridade 0
    for i in raizes:    
        push(lista_prioridades, (0, i))
    #representação: posterior é o indice e anterior é o valor armazenado
    anteriores = [None for i in range(int(nx.number_of_nodes(g)))]
    # cria um dicionário que indica se um vertice v está no heap (se está no heap, ainda não foi processado)
    inheap = { v: True for v in g.nodes() }
    # cria um dicionário de pesos
    pesos = { v: float('inf') for v in g.nodes() }
    for i in raizes:    
        pesos[i] = 0;
 
    while lista_prioridades:
        no = pop(lista_prioridades)
        # testa se no ainda não foi removido do heap anteriormente (como não podemos atualizar os pesos, um nó pode repetir)
        if (inheap[no[1]]):
            inheap[no[1]] = False # foi retirado do heap pela primeira vez
        else:
            continue # já foi retirado do heap antes (repetido)

        #Seleciona cada vizinho
        for i in nx.neighbors(g,no[1]):
            #verifica se vizinho esta na lista de prioridades            
            if (inheap[i]): 
                #verifica se um no é anterior do outro   
                peso = g.get_edge_data(no[1],i)['weight'] + no[0]
                if peso < pesos[i]:
                    push(lista_prioridades, (peso, i))
                    anteriores[i] = no[1]
                    pesos[i] = peso
                
    arestas = [(i,anteriores[i]) for i in range(len(anteriores))]
    nos = []    
    for i in raizes:
        if (i,None) in arestas:
            nos.append(i)
			#Remove arestas que ligam um grafo a None
            arestas.remove((i,None))
    #arestas.pop(0) #Descarta o primeiro pois é a aresta da raiz, que não se liga com ninguém (0,None)
    g.clear()
    g.add_edges_from(arestas)
	#Coloca os nós que ficaram sozinhos
    g.add_nodes_from(nos)
    return g
Exemplo n.º 26
0
def solve(seq):
    visited = []
    queue = [seq]
    guess = seq.h
    return guess
    while True:
        seq = pop(queue)
        if seq == '+':
            return seq.g, guess
        for i in xrange(len(seq)):
            push(queue, seq.flip(i + 1))
Exemplo n.º 27
0
 def getNewsFeed(self, userId: int) -> List[int]:
     """
     Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
     """
     li = []
     fids = set(self.followed[userId])
     if fids is not None:
         for fid in fids:
             ftweets = self.tweets[fid]
             if ftweets is not None:
                 for ftweet in ftweets:
                     heapq.push(li, ftweet)
Exemplo n.º 28
0
def main():
    n, m = map(int, stdin.readline().split())

    # Read in all the edge data
    edges = {}
    for _ in range(m):
        u, v, w = map(int, stdin.readline().split())
        if u not in edges: edges[u] = set()
        if v not in edges: edges[v] = set()
        edges[u].add((v, w))
        edges[v].add((u, w))

    # Use Dijkstra's algorithm to find the best edges from each node to the end
    # Start from the last intersection (1) and essentially travel backwards
    q = []
    push(q, (0, 1, -2))
    path_to = [-1 for _ in range(n)]  # -1 will represent an unvisited point
    while q:
        d, c, f = pop(q)  # Distance, Current, Following
        if path_to[c] != -1: continue  # Node has been found more cheaply
        path_to[c] = f

        for e, w in edges[c]:
            if path_to[e] != -1: continue

            push(q, (d + w, e, c))

    # Use a DFS to find any path that does not take the edges found earlier
    path_from = [-1 for _ in range(n)]
    path_from[0] = -2
    s = deque()
    s.append((0, 0))

    while s:
        d, c = s.pop()  # Distance, Current position
        if c == 1: break

        for e, w in edges[c]:
            if (path_from[e] != -1 or path_to[c] == e):
                continue  # This represents an edge found earlier, so ignore it
            path_from[e] = c
            s.append((d + w, e))

    if path_from[1] == -1:  # No useable edges led to 1
        print('impossible')
    else:
        # Iterate backwards through the path, finding all edges used
        curr = 1
        outs = []
        while curr != -2:
            outs.append(str(curr))
            curr = path_from[curr]
        print(len(outs), ' '.join(outs[::-1]))
Exemplo n.º 29
0
 def heapq():
     global indegree
     q=[]
     push(q,(1e5-T[W], W))
     accum[W]=T[W]
     while q:
         reversed_time, building = pop(q)
         time = 1e5-reversed_time
         for v in edge[building]:
             indegree[v]-=1
             if indegree[v]==0:
                 
Exemplo n.º 30
0
def solution(stock, d, s, k):
    h = []
    d += [k]
    s += [0]
    cnt = i = 0
    for now in d:
        push(h, -s[i])
        i += 1
        while stock <= now:
            stock -= pop(h)
            cnt += 1
            if stock >= k: return cnt
Exemplo n.º 31
0
def solution(stock, d, s, k):
    h = []
    last = len(d)
    cnt = start = 0
    while stock < k:
        for i in range(start, last):
            if d[i] > stock: break
            push(h, -s[i])
            start = i + 1
        stock -= pop(h)
        cnt += 1
    return cnt
Exemplo n.º 32
0
def dijikstra(arrv):
    q = []
    push(q,(0,arrv))
    accum[arrv]=0
    while q:
        tmp, v = pop(q)
        if accum[v]<tmp:
            continue
        for dur, v2 in edge[v]:
            if dur+tmp<accum[v2]:
                accum[v2]=dur+tmp
                push(q,(accum[v2],v2))
Exemplo n.º 33
0
def connect_n_ropes(ropes):
    heap = []

    for r in ropes:
        push(heap, r)

    while len(heap) > 1:
        first = pop(heap)
        second = pop(heap)
        push(heap, first + second)

    return heap[0]
Exemplo n.º 34
0
def main():
	graph = [list(input()) for _ in range(8)]
	visited = [[False for _ in range(8)] for _ in range(8)]
	
	ti,tj = 7,0
	graph[ti][tj] = '.'

	q = []
	push(q,(0,ti,tj,0,[]))

	# 0,1,2,3 = R,D,L,U
	dirs = [(0,1,0),(1,0,1),(0,-1,2),(-1,0,3)] 

	while q:
		m,i,j,f,p = pop(q) # Moves,Y,X,Facing,Path
		if graph[i][j] == 'D':
			break

		if visited[i][j]: continue
		visited[i][j] = True

		for di,dj,df in dirs:
			if (0 <= i+di < 8 and 0 <= j+dj < 8
					and not visited[i+di][j+dj]):
				new_path = []
				
				if graph[i+di][j+dj] == 'C':
					continue

				if df != f:
					if abs(df-f) == 2:
						new_path.append('R')
						new_path.append('R')
					elif df == f+1:
						new_path.append('R')
					elif df == f-1:
						new_path.append('L')
					elif df == f+3:
						new_path.append('L')
					else:
						new_path.append('R')

				if graph[i+di][j+dj] == 'I':
					new_path.append('X')

				new_path.append('F')
				push(q,(m+len(new_path),i+di,j+dj,df,p+new_path))

	if graph[i][j] == 'D':
		print(''.join(p))
	else:
		print('no solution')
Exemplo n.º 35
0
    def __init__(self, file_name):
        stat = {}
        file = open(file_name, mode='r')
        for _ch in file.read():
            # print(_ch)
            ch = _ch
            if ch in stat.keys():
                stat[ch] += 1
            else:
                stat[ch] = 1
        file.close()

        from heapq import heappush as push, heappop as pop
        forest = []
        for ch in sorted(stat.keys()):
            push(forest, Tree(True, ch, stat[ch]))

        while len(forest) >= 2:
            left = pop(forest)
            right = pop(forest)
            push(forest, Tree(left=left, right=right, weight=(left.weight + right.weight)))

        self.t = pop(forest)
        base = {}

        def rec(t, tmp):
            if t.leaf == True:
                base[t.value] = tmp
            else:
                rec(t.left, tmp + ['0'])
                rec(t.right, tmp + ['1'])
        rec(self.t, [])

        self.a = []
        buffer = []
        file = open(file_name, mode='r')
        for _ch in file.read():
            ch = _ch
            for b in base[ch]:
                if len(buffer) == 8:
                    tmp = 0
                    for i in range(8):
                        tmp *= 2
                        if buffer[i] == '1':
                            tmp += 1
                    self.a += [tmp]
                    buffer = []
                buffer += [b]
        self.tail = buffer
        file.close()
Exemplo n.º 36
0
 def insert(self, num):
     if len(self.right) == len(self.left):
         push(self.left, -num)
         push(self.right, -pop(self.left))
     else:
         push(self.right, num)
         push(self.left, -pop(self.right))
Exemplo n.º 37
0
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        heap = []

        for n in nums:
            if len(heap) < k:
                push(heap, n)
            else:
                if heap[0] < n:
                    pop(heap)
                    push(heap, n)

        return heap[0]
Exemplo n.º 38
0
def Prim(g,dic_pesos):
    '''
    Parametro: g, representando um grafo
    Retorno: arvore, arvore geradora minima
    Função: Retorna a MST (Minimun Spend Tree) ou arvore geradora minima
    do grafo recebido como parametro
    '''
    lista_prioridades = [(Inf,i) for i in range(int(nx.number_of_nodes(g)))]
    # lista_prioridades transformar em heap
    heapify(lista_prioridades) 
    # inserir nó 0 (assumindo que o nó 0 é a raiz) com prioridade 0
    push(lista_prioridades, (0, 0))
    #representação: posterior é o indice e anterior é o valor armazenado
    anteriores = [None for i in range(int(nx.number_of_nodes(g)))]
    # cria um dicionário que indica se um vertice v está no heap (se está no heap, ainda não foi processado)
    inheap = { v: True for v in g.nodes() }
    # cria um dicionário de pesos (evita a necessidade de utilizar a função verifica_vizinho)
    pesos = { v: float('inf') for v in g.nodes() }
    pesos[0] = 0;
 
    while lista_prioridades:
        no = pop(lista_prioridades)
        # testa se no ainda não foi removido do heap anteriormente (como não podemos atualizar os pesos, um nó pode repetir)
        if (inheap[no[1]]):
            inheap[no[1]] = False # foi retirado do heap pela primeira vez
        else:
            continue # já foi retirado do heap antes (repetido)

        #Seleciona cada vizinho
        for i in nx.neighbors(g,no[1]):
            #verifica se vizinho esta na lista de prioridades            
            if (inheap[i]): 
                #verifica se um no é anterior do outro   
                peso = dic_pesos[(no[1],i)]
                if peso < pesos[i]:
                    push(lista_prioridades, (peso, i))
                    anteriores[i] = no[1]
                    pesos[i] = peso
                    
    arestas = [(i,anteriores[i]) for i in range(len(anteriores))]
    arestas.pop(0) #Descarta o primeiro pois é a aresta da raiz, que não se liga com ninguém (0,None)
    g.clear()
    g.add_edges_from(arestas)
    return g
Exemplo n.º 39
0
 def steps(self):
   result = []
   i,j,c,p = self.i,self.j,self._cost,self.path
   if i > 0:
     push(result,Node(i-1,j,c,p))
   if j > 0:
     push(result,Node(i,j-1,c,p))
   if i < self.m:
     push(result,Node(i+1,j,c,p))
   if j < self.n:
     push(result,Node(i,j+1,c,p))
   return result
Exemplo n.º 40
0
def medians(numbers):
    numbers = iter(numbers)
    left, right = [], []
    while True:
        #odd items
        push(left, -next(numbers))
        push(right, -pop(left))
        yield right[0]

        #even items
        push(right, next(numbers))
        push(left, -pop(right))
        yield (right[0] - left[0])/2.0
Exemplo n.º 41
0
def searchEM(board,nodesToExpand=1000):
	startNode = createFunction(board)

	heap = []
	push(heap,(-startNode.score,startNode))
	i = 0
	while i < nodesToExpand:
		start = time.time()
		thisNode = pop(heap)[1]
		if not thisNode.expanded:
			thisNode.expand()
			for a in ['L', 'R', 'D', 'U']:
				for childNode in thisNode.children2[a]:
					push(heap,(-childNode.score,childNode))
				for childNode in thisNode.children4[a]:
					push(heap,(-childNode.score,childNode))
			i += 1
		"""
		else:
			#print (i)
			for a in ['L', 'R', 'D', 'U']:
				for childNode in thisNode.children2[a]:
					push(heap,(-childNode.score,childNode))
				for childNode in thisNode.children4[a]:
					push(heap,(-childNode.score,childNode))
		"""

		#print "time to expand:" , time.time()-start

	Node.allNodes = {}

	#print (startNode.bestDir)
	#pdb.set_trace()
	return move(board,startNode.bestDir)
Exemplo n.º 42
0
Arquivo: wc.py Projeto: anchikam/wc
def run_medians(numlist):
    """ Given a list of integers, returns a generator of running medians.
    The method uses 2 heaps (max heap/min heap) and places streaming numbers in either of the 
    heap depending on their values. The way median is found depends if the heaps are balanced or not. """
    itnum = iter(numlist)
    less, more = [], []   
    first = next(itnum)
    yield first
    second = next(itnum)
    push(less, - min(first, second))
    push(more, max(first, second))
    while True:
        curr = ( more[0] if len(less) < len(more)
                    else - less[0] if len(less) > len(more)
                    else (more[0] - less[0]) / 2.0 )
        yield curr
        it = next(itnum)
        if it <= curr: push(less, - it)
        else: push(more, it)
        small, big = ( (less, more) if len(less) <= len(more)
                       else (more, less) )
        if len(big) - len(small) > 1: push(small, - pop(big))
Exemplo n.º 43
0
    for bound, bound_ws in witnesses_bounds:
        if n < bound:
            best_witnesses = bound_ws
            break
    # check compositeness with the witnesses
    for a in best_witnesses:
        a %= n
        if a and check_composite(n, s, d, a):
            return False
    return True
s = '123456789'
l = []
for i in xrange(1,11):
    a = P(s[:i])
    for j in a:
		c = int(''.join(j))
		if is_prime(c):
			push(l,c)
h(l)
for i in xrange(input()):
	a = True
	n = input()
	ln = len(l)
	for j in xrange(ln):
		if l[ln -j-1] < n:
			a = False
			print l[ln - j-1]
			break
	if a:
		print -1
# print l[-1]
Exemplo n.º 44
0
def push_next(p, b, e, h):
    p, e = p * b, e + 1
    while p < 1e28 and sum(int(i) for i in str(p)) != b:
        p, e = p * b, e + 1
    if p < 1e28: push(h, (p, b, e))
Exemplo n.º 45
0
	def __init__(heap, list = ()):
		for item in list:
			heapq.push(heap, item)