def test_delete(minpq): minpq = pq.insert(minpq, 23) minpq = pq.insert(minpq, 7) minpq = pq.insert(minpq, 30) minpq = pq.insert(minpq, 5) minpq = pq.insert(minpq, 15) minpq = pq.insert(minpq, 1) key = pq.min(minpq) assert key == 1 key = pq.delMin(minpq) assert key == 1 key = pq.min(minpq) assert key == 5 key = pq.delMin(minpq) assert key == 5 key = pq.min(minpq) assert key == 7 minpq = pq.insert(minpq, 4) minpq = pq.insert(minpq, 3) minpq = pq.insert(minpq, 2) key = pq.min(minpq) assert key == 2 key = pq.delMin(minpq) assert key == 2 key = pq.delMin(minpq) assert key == 3 key = pq.delMin(minpq) assert key == 4
def getTopN(pq, n): taxis = {} services = {} for i in range(1, n+1): taxis[i]=pq.delMin(pq["T_taxis"]) services[i]=pq.delMin(pq["T_services"]) return (taxis, services)
def topN(theServices,N): valores=[] res={} i=0 n=0 keys= m.keySet(theServices) values=m.valueSet(theServices) itv=it.newIterator(values) itk=it.newIterator(keys) orden=mpq.newMinPQ(comparemaxpq) while it.hasNext(itv): value=it.next(itv) key= it.next(itk) if key!= "Independent Owner": mpq.insert(orden,value[1]) while i<N: valor=mpq.min(orden) valores.append(valor) res[valor]=[] mpq.delMin(orden) i+=1 itval=it.newIterator(values) itke=it.newIterator(keys) while it.hasNext(itval): val=it.next(itval) ke= it.next(itke) if ke!= "Independent Owner": for i in valores: if i==val[1]: if ke not in res[i] and n<N: res[i].append(ke) n+=1 return res
def getTopN(Pq, n): Taxis = {} Services = {} for i in range(1, n + 1): Taxis[i] = pq.delMin(Pq["T_taxis"]) Services[i] = pq.delMin(Pq["T_services"]) return (Taxis, Services)
def addTaxisServices (analyzer): iterator=it.newIterator(analyzer["companies"]["taxis"]) while it.hasNext(iterator): company=it.next(iterator) mp.insert(analyzer["companies"]["total_taxis"],(lt.size(company["taxis_id"])*-1),company["company"]) mp.insert(analyzer["companies"]["total_services"],(company["services"])*-1,company["company"]) analyzer["NumTaxis"]+=lt.size(company["taxis_id"]) for i in range(0,mp.size(analyzer["companies"]["total_taxis"])): analyzer["companies"]["diccio"]["top_taxis"].append(mp.delMin(analyzer["companies"]["total_taxis"])) for j in range (0,mp.size(analyzer["companies"]["total_services"])): analyzer["companies"]["diccio"]["top_services"].append(mp.delMin(analyzer["companies"]["total_services"]))
def organizeTop3(PQs): InTop = [] OutTop = [] UsageTop = [] for i in range(0, 3): #Se sacan los 3 primeros de cada Pq In = pq.delMin(PQs["In"]) Out = pq.delMin(PQs["Out"]) Usage = pq.delMin(PQs["Usage"]) InTop.append({"id": In["station"], "In": In["key"]}) OutTop.append({"id": Out["station"], "Out": Out["key"]}) UsageTop.append({"id": Usage["station"], "Usage": Usage["key"]}) return {"In": InTop, "Out": OutTop, "Usage": UsageTop}
def getMaxPointsinDateRange(tree, mindate, maxdate, num): taxiPoints = m.newMap(numelements=75000, comparefunction=compareTaxiId) taxiPointsPQ = pq.newMinPQ(comparePoints) dateRange = om.values(tree, mindate, maxdate) dateIterator = it.newIterator(dateRange) while it.hasNext(dateIterator): elem = it.next(dateIterator) taxiMap = elem['TaxiMap'] taxiIterator = it.newIterator(m.keySet(taxiMap)) while it.hasNext(taxiIterator): eleme = it.next(taxiIterator) if m.contains(taxiPoints, eleme): entry = me.getValue(m.get(taxiMap, eleme)) add_points = entry['points'] other_entry = me.getValue(m.get(taxiPoints, eleme)) other_entry['points'] += add_points else: entry = me.getValue(m.get(taxiMap, eleme)) add_points = entry['points'] other_entry = {'taxi_id': eleme, 'points': add_points} m.put(taxiPoints, eleme, other_entry) keyPoints = m.keySet(taxiPoints) keyIterator = it.newIterator(keyPoints) while it.hasNext(keyIterator): el = it.next(keyIterator) points = me.getValue(m.get(taxiPoints, el))['points'] pq.insert(taxiPointsPQ, (el, points)) cont = 0 taxiList = lt.newList() while cont < num: tax = pq.delMin(taxiPointsPQ) lt.addLast(taxiList, tax) cont += 1 return taxiList
def printBestTaxis(qeue, N): i = 0 while i < N and not pq.isEmpty(qeue): taxi = pq.delMin(qeue) print("\n------ " + str(i + 1) + " ------") print("⚛︎ Taxi ID: {0}".format(taxi["id"])) print("⚛︎ Puntaje: {0}".format(round(taxi["points"], 2))) i += 1 print( "\n\nACLARACIÓN: Si aparecen menos taxis de los esperados es porque no hay {0} taxis en esa fecha" .format(N))
def criticalStations(analyzer): vertexs = gr.vertices(analyzer["connections"]) indegree = mq.newMinPQ(compareinverted) outdegree = mq.newMinPQ(compareinverted) degree = mq.newMinPQ(comparenormal) iterator = it.newIterator(vertexs) res1 = lt.newList() res2 = lt.newList() res3 = lt.newList() while it.hasNext(iterator): element = it.next(iterator) ins = (element,int(gr.indegree(analyzer["connections"],element))) out = (element,int(gr.outdegree(analyzer["connections"],element))) deg = (element,int(gr.indegree(analyzer["connections"],element))+int(gr.outdegree(analyzer["connections"],element))) mq.insert(indegree,ins) mq.insert(outdegree,out) mq.insert(degree,deg) for a in range(1,4): lt.addLast(res1,mq.delMin(indegree)) lt.addLast(res2,mq.delMin(outdegree)) lt.addLast(res3,mq.delMin(degree)) return (res1,res2,res3)
def getMaxPointsinDate(tree, date, num): dateEntry = om.get(tree, date) if dateEntry is not None: minPQ = pq.newMinPQ(comparePoints) dateValue = me.getValue(dateEntry) taxiMap = dateValue['TaxiMap'] taxiList = m.keySet(taxiMap) taxiIterator = it.newIterator(taxiList) while it.hasNext(taxiIterator): elem = it.next(taxiIterator) taxiValue = me.getValue(m.get(taxiMap, elem)) taxipoints = taxiValue['points'] pq.insert(minPQ, (elem, taxipoints)) cont = 0 taxis = lt.newList() while cont < num: taxi = pq.delMin(minPQ) lt.addLast(taxis, taxi) cont +=1 return taxis else: return None
def M_servicios(lista, M): Min = [] for n in range(0, M): Min.append(mpq.delMin(lista["Servicios"])) return Min
def M_compañias(lista, M): Min = [] for n in range(0, M): Min.append(mpq.delMin(lista["Compañias"])) return Min
import config from DISClib.ADT import minpq as pq """ Encontrar los 5 elementos mas pequeños de un lista """ def greater(key1, key2): if key1 == key2: return 0 elif key1 < key2: return -1 else: return 1 minpq = pq.newMinPQ(greater) pq.insert(minpq, 5) pq.insert(minpq, 23) pq.insert(minpq, 31) pq.insert(minpq, 15) print(pq.delMin(minpq)) print(pq.delMin(minpq)) print(pq.delMin(minpq))
def greater(key1, key2): if key1['t'] == key2['t']: return 0 elif key1['t'] < key2['t']: return -1 else: return 1 cola_de_espera = queue.newQueue() minpq = pq.newMinPQ(greater) pq.insert(minpq, {'t': 0, 'evento': 'llegada'}) pq.insert(minpq, {'t': 5, 'evento': 'fin'}) iters = 0 while not pq.isEmpty(minpq) and iters < 100: p = pq.delMin(minpq) if p['evento'] == 'llegada': queue.enqueue(cola_de_espera, 1) pq.insert(minpq, {'t': p['t'] + 1, 'evento': 'llegada'}) print('llegada') if p['evento'] == 'fin': break iters = iters + 1 print(queue.size(cola_de_espera))