def pointsInRange(analyzer, n, in_date, fi_date): arbol=analyzer["arbol_dates"] taxis = lt.newList("ARRAY_LIST",compare) lista=om.values(arbol, in_date, fi_date) if lt.isEmpty(lista): return None iterator=it.newIterator(lista) mapTaxis = m.newMap(100,maptype='CHAINING',loadfactor=5,comparefunction=compareMap) while it.hasNext(iterator): date=it.next(iterator) for i in range(1,lt.size(date)+1): element = lt.getElement(date,i) num,taxiId = element entry = m.get(mapTaxis,taxiId) if entry == None: lt.addLast(taxis, taxiId) m.put(mapTaxis,taxiId,num) else: num2 = entry["value"] numNew = num2 + num entry["value"] = numNew max_pq=hp.newHeap(compare) for j in range (1, lt.size(taxis)+1): taxiId = lt.getElement(taxis,j) points = me.getValue(m.get(mapTaxis,taxiId)) hp.insertMax(max_pq,points, taxiId) lst = max_pq['elements'] size = lt.size(lst) while size>1: lt.exchange(lst,1,size) size -=1 sinksort(lst,1,size) return (lst,lt.size(lst))
def requerimiento1(catalog,country,category): start_time = time.process_time() country=country.lower() countries=catalog["countries"] couple=mp.get(countries,country) lista=me.getValue(couple) size=lt.size(lista) number=getCategoryNumber(category,catalog) contador=1 iterador=li.newIterator(lista) i=1 while li.hasNext(iterador): video=li.next(iterador) if cmpVideosByCategory(number,video): lt.exchange(lista,i,contador) contador+=1 i+=1 lista_total=lt.subList(lista,1,contador) size=lt.size(lista_total) ordenada=mergeSort(lista_total,1) stop_time = time.process_time() elapsed_time_mseg = (stop_time - start_time)*1000 return ordenada,elapsed_time_mseg
def exchange(heap, posa, posb): """ Intercambia los elementos en las posiciones posa y posb del heap """ try: lt.exchange(heap['elements'], posa, posb) except Exception as exp: error.reraise(exp, 'heap:exchange')
def insertionSort(lst, lessfunction): size = lt.size(lst) pos1 = 1 while pos1 <= size: pos2 = pos1 while (pos2 > 1) and (lessfunction( (lt.getElement(lst, pos2), lt.getElement(lst, pos2 - 1)))): lt.exchange(lst, pos2, pos2 - 1) pos2 -= 1 pos1 += 1
def sort(lst, cmpfunction): size = lt.size(lst) pos1 = 1 while pos1 <= size: pos2 = pos1 while (pos2 > 1) and (cmpfunction(lt.getElement(lst, pos2), lt.getElement(lst, pos2 - 1))): lt.exchange(lst, pos2, pos2 - 1) pos2 -= 1 pos1 += 1 return lst
def selectionSort(lst, lessfunction): size = lt.size(lst) pos1 = 1 while pos1 < size: minimum = pos1 # minimun tiene el menor elemento pos2 = pos1 + 1 while (pos2 <= size): if (lessfunction(lt.getElement(lst, pos2), (lt.getElement(lst, minimum)))): minimum = pos2 # minimum = posición elemento más pequeño pos2 += 1 lt.exchange(lst, pos1, minimum) # elemento más pequeño -> elem pos1 pos1 += 1
def shellSort(lst, lessfunction): n = lt.size(lst) h = 1 while h < n / 3: # Se calcula el tamaño del primer gap. La lista se h-ordena con este tamaño h = 3 * h + 1 # por ejemplo para n = 100, h toma un valor inical de 13 , 4, 1 while (h >= 1): for i in range(h, n): j = i while (j >= h) and lessfunction(lt.getElement(lst, j + 1), lt.getElement(lst, j - h + 1)): lt.exchange(lst, j + 1, j - h + 1) j -= h h //= 3 # h se decrementa en un tercio. cuando h es igual a 1, se comporta como insertionsort
def shellSort(lst, lessfunction): n = lt.size(lst) h = 1 while h < n / 3: # primer gap. La lista se h-ordena con este tamaño h = 3 * h + 1 while (h >= 1): for i in range(h, n): j = i ant = lt.getElement(lst, j + 1) post = lt.getElement(lst, j - h + 1) while (j >= h) and lessfunction(ant, post): lt.exchange(lst, j + 1, j - h + 1) j -= h h //= 3 # h se decrementa en un tercio
def partition(lst, lo, hi, cmpfunction): """ Función que va dejando el pivot en su lugar, mientras mueve elementos menores a la izquierda del pivot y elementos mayores a la derecha del pivot """ follower = leader = lo while leader < hi: if cmpfunction(lt.getElement(lst, leader), lt.getElement(lst, hi)): lt.exchange(lst, follower, leader) follower += 1 leader += 1 lt.exchange(lst, follower, hi) return follower
def sort(lst, cmpfunction): n = lt.size(lst) h = 1 while h < n / 3: # primer gap. La lista se h-ordena con este tamaño h = 3 * h + 1 while (h >= 1): for i in range(h, n): j = i while (j >= h) and cmpfunction(lt.getElement(lst, j + 1), lt.getElement(lst, j - h + 1)): lt.exchange(lst, j + 1, j - h + 1) j -= h h //= 3 # h se decrementa en un tercio return lst
def shellSort(lst, lessfunction): n = lt.size(lst) h = 1 while ( h < n / 3 ): # Se calcula el tamaño del primer gap. La lista se h-ordena con este tamaño h = 3 * h + 1 # por ejemplo para n = 100, h toma un valor inical de 13 , 4, 1 while h >= 1: for i in range(h, n): j = i while (j >= h) and lessfunction(lt.getElement(lst, j + 1), lt.getElement(lst, j - h + 1)): lt.exchange(lst, j + 1, j - h + 1) j -= h h //= 3
def sinksort (lst, pos, size): if (2*pos>size): return lst dad = lt.getElement(lst,pos) left = lt.getElement(lst,2*pos) right = lt.getElement(lst,(2*pos)+1) if ((2*pos)+1 > size): num,id = left right = ((num-1),id) if ((dad > left) and (dad > right)): return lst else: if left >= right: lt.exchange(lst,2*pos,pos) sinksort(lst,2*pos,size) else: lt.exchange(lst,(2*pos)+1,pos) sinksort(lst,(2*pos)+1,size) return lst
def orderPoints(analyzer): lista=analyzer["list_dates"] mapa= analyzer["date"] arbol= analyzer["arbol_dates"] for i in range(1, lt.size(lista)+1): date=lt.getElement(lista, i) taxis=me.getValue(m.get(mapa, date)) taxisLst = taxis["taxis"] taxisMap = taxis["map"] max_pq=hp.newHeap(compare) for j in range (1, lt.size(taxisLst)+1): taxiId = lt.getElement(taxisLst,j) taxiDict = me.getValue(m.get(taxisMap,taxiId)) point=(taxiDict["millas"]/taxiDict["money"])*taxiDict["services"] hp.insertMax(max_pq,point, taxiId) lst = max_pq['elements'] size = lt.size(lst) while size>1: lt.exchange(lst,1,size) size -=1 sinksort(lst,1,size) om.put(arbol,date, lst)
def PrimMST(graph, origin=None): """ Implementa el algoritmo de Prim Args: graph: El grafo de busqueda Returns: La estructura search con los MST Raises: Exception """ try: search = initSearch(graph) vertices = g.vertices(graph) if origin is not None: pos = lt.isPresent(vertices, origin) if pos != 0: lt.exchange(vertices, 1, pos) for vert in lt.iterator(vertices): if not map.get(search['marked'], vert)['value']: prim(graph, search, vert) return search except Exception as exp: error.reraise(exp, 'prim:PrimMST')
def test_exchange_array(altbooks, books): book1 = lt.getElement(altbooks, 1) book5 = lt.getElement(altbooks, 5) lt.exchange(altbooks, 1, 5) assert lt.getElement(altbooks, 1) == book5 assert lt.getElement(altbooks, 5) == book1