def sortHash (catalog): #Iterar tabla de hash, extraer lista de llaves, ordenar cada lista de tuplas con mergeSort citiesList = map.keySet(catalog['capacityMap']) citiesIter = it.newIterator(citiesList) while it.hasNext(citiesIter): i_city = it.next(citiesIter) stationCapacityList = map.get(catalog['capacityMap'], i_city)['value'] mergesort.mergesort(stationCapacityList, compareByTuple)
def addToHash (catalog, row): capacity_station = (row['dock_count'],row['id']) if map.contains(catalog['capacityMap'],row['city']): statcapList = map.get(catalog['capacityMap'],row['city'])['value'] lt.addLast(statcapList,capacity_station) else: stationCapacityList = lt.newList('ARRAY_LIST') lt.addLast(stationCapacityList,capacity_station) map.put(catalog['capacityMap'],row['city'], stationCapacityList)
def addTempHash (catalog, row): hashTemp = catalog['temperatureHash'] date = strToDate(row['date'], '%Y-%m-%d') #Convertir fecha a str if map.contains(catalog['temperatureHash'], row['mean_temperature_f']): dateIList = map.get(catalog['temperatureHash'], row['mean_temperature_f'])['value'] lt.addLast(dateIList, date) map.put(hashTemp, row['mean_temperature_f'], dateIList) else: dateList = lt.newList('ARRAY') lt.addLast(dateList, date) map.put(hashTemp, row['mean_temperature_f'], dateList)
def addToTree (catalog, row): tree=catalog['dateTree'] #Árbol RBT ordenado por fecha date_raw= row['start_date'].split(" ")[0] date=strToDate(date_raw, '%m/%d/%Y') #Convertir fecha a str StationInf = map.get(catalog['stationMap'], row['start_station_id'])['value'][1] #Ciudad de la estación if oms.contains(tree, date, greater): dateValue = oms.get(tree, date, greater) if map.contains(dateValue, StationInf): value = map.get(dateValue, StationInf)['value'] map.put(dateValue, StationInf, value+1) else: map.put(dateValue, StationInf, 1) totalValue = map.get(dateValue, 'total')['value'] totalValue += 1 map.put(dateValue, 'total', totalValue) tree = oms.put(tree, date, dateValue, greater) else: DateValueMap = map.newMap(maptype='PROBING', comparefunction= compareByKey) map.put(DateValueMap, StationInf, 1) map.put(DateValueMap, 'total', 1) tree = oms.put(tree, date, DateValueMap, greater)
def leastCapacity(catalog, city, ncapacities): rawMap = catalog['capacityMap'] statcapList = map.get(rawMap, city)['value'] response = "Least capacity stations: " LessN = lt.subList(statcapList, 1, ncapacities) if not lt.isEmpty(LessN): iteraless=it.newIterator(LessN) while it.hasNext(iteraless): station = it.next(iteraless) response += str(station) + ':' + " " return response return None
def test_get(self): table = ht.newMap(capacity=17, maptype='PROBING') ht.put(table, '1', 'title1', self.comparekeys) ht.put(table, '2', 'title2', self.comparekeys) ht.put(table, '3', 'title3', self.comparekeys) ht.put(table, '4', 'title4', self.comparekeys) ht.put(table, '5', 'title5', self.comparekeys) ht.put(table, '6', 'title6', self.comparekeys) self.assertEqual(ht.size(table), 6) entry = ht.get(table, '5', self.comparekeys) print(entry)
def mostCapacity(catalog, city, ncapacities): rawMap = catalog['capacityMap'] statcapList = map.get(rawMap, city)['value'] response= "Top capacity stations: " size = lt.size(statcapList) TopN = lt.subList(statcapList, size-ncapacities+1, ncapacities) if not lt.isEmpty(TopN): iteratop=it.newIterator(TopN) while it.hasNext(iteratop): station = it.next(iteratop) response += str(station) + ':' + " " return response return None
def tripCityforDates (catalog, start_date, end_date): start_date=strToDate(start_date, '%m/%d/%Y') #Convertir fecha end_date=strToDate(end_date, '%m/%d/%Y') #Convertir fecha dateList = oms.valueRange(catalog['dateTree'], start_date, end_date, greater) #Lista de llaves entre las fechas dadas response='' tripsCityDays = map.newMap(capacity=11, maptype='CHAINING') #Se almacenan if dateList: print(dateList) iteraDate=it.newIterator(dateList) while it.hasNext(iteraDate): dateElement = it.next(iteraDate) #print(dateElement) if oms.get(catalog['dateTree'],dateElement, greater):#Si el nodo tiene un valor asociado if map.isEmpty(tripsCityDays):#Si cities está vacío, se le asigna el map de accidentes por ciudad del primer nodo tripsCityDays = oms.get(catalog['dateTree'], dateElement, greater) else: #De lo contrario, se compara cada ciudad del map de cada nodo con el map ciudadesNodo = map.keySet(dateElement)#Lista de las ciudades que tuvieron accidentes en esa fecha(nodo) ciudadesCities = map.keySet(tripsCityDays) iteraCiudades = it.newIterator(ciudadesNodo) while it.hasNext(iteraCiudades): ciudadElement=it.next(iteraCiudades)# que está en el map de cada nodo if ciudadElement: if lt.isPresent(ciudadesCities, ciudadElement, compareByKey): #Se verifica si la ciudad está en los valores del map num=map.get(tripsCityDays, ciudadElement)['value'] num+=map.get(dateElement, ciudadElement)['value'] map.put(tripsCityDays, ciudadElement, num) else: num= map.get(dateElement, ciudadElement)['value'] map.put(dateElement, ciudadElement, num) if not map.isEmpty(tripsCityDays): cityList= map.keySet(tripsCityDays) iteracity=it.newIterator(cityList) while it.hasNext(iteracity): cityKey = it.next(iteracity) response += str(cityKey) + ':' + str(map.get(tripsCityDays,cityKey)['value']) + " " return response return None
def test_get (self): """ """ table = ht.newMap (capacity= 7, maptype='CHAINING') ht.put (table, 'book1', 'title1', self.compareentryfunction) ht.put (table, 'book2', 'title2', self.compareentryfunction) ht.put (table, 'book3', 'title3', self.compareentryfunction) ht.put (table, 'book4', 'title4', self.compareentryfunction) ht.put (table, 'book5', 'title5', self.compareentryfunction) ht.put (table, 'book6', 'title6', self.compareentryfunction) book = ht.get (table, 'book2', self.compareentryfunction) print (book) self.assertEqual (ht.size(table), 6)
def test_get(self): capacity = 10 table = ht.newMap(capacity, maptype='PROBING', comparefunction=self.comparekeys) ht.put(table, '1', 'title1') ht.put(table, '2', 'title2') ht.put(table, '11', 'title3') ht.put(table, '3', 'title4') ht.put(table, '12', 'title5') ht.put(table, '5', 'title6') entry = ht.get(table, '5') print(entry)
def test_delete(self): table = ht.newMap(capacity=17, maptype='PROBING') ht.put(table, '1', 'title1', self.comparekeys) ht.put(table, '2', 'title2', self.comparekeys) ht.put(table, '3', 'title3', self.comparekeys) ht.put(table, '4', 'title4', self.comparekeys) ht.put(table, '5', 'title5', self.comparekeys) ht.put(table, '6', 'title6', self.comparekeys) self.assertEqual(ht.size(table), 6) self.printTable(table) entry = ht.remove(table, '3', self.comparekeys) self.assertEqual(ht.size(table), 5) entry = ht.get(table, '3', self.comparekeys) self.assertIsNone(entry) self.printTable(table)
def test_get(self): entry = ht.get (self.table, '5', self.comparekeys) print (entry)
def get(map, key): """ Retorna la pareja llave, valor, cuya llave sea igual a key. Es necesario proveer una función de comparación para las llaves. """ return ht.get(map, key)
def tripsPerTemperature(catalog, number): longList = lt.size(catalog['tempList']) leastTemp = lt.subList(catalog['tempList'],longList-number,number) mostTemp = lt.subList(catalog['tempList'], 1, number) #O(1) por ser array counter1 = 0 counter2 = 0 response1='' response2='' tripsMostTempDays = map.newMap(30, comparefunction=compareByKey) tripsLeastTempDays = map.newMap(30, comparefunction=compareByKey) while counter1<number: leastTempIterator = it.newIterator(leastTemp) #Iterar la lista con n menores temperaturas while it.hasNext(leastTempIterator): tempElement = it.next(leastTempIterator) #Temperatura dateListElement = map.get(catalog['temperatureHash'],tempElement)['value']#Lista de todas las fechas para esa temperatura if number - lt.size(dateListElement) < counter1: #si no se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = lt.subList(dateListElement, 1, number-counter1) #lista de las que si se pueden agragar n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) #fecha a agregar trips = oms.get(catalog['dateTree'], n_dateElement, greater) #hash de viajes de esa fecha #print(type(trips)) totaltrip = map.get(trips, 'total')['value'] #número de viajes totales en esa fecha value = (tempElement, totaltrip) #tupla que se asigna como valor de cada fecha: temperatura, viajes totales map.put(tripsLeastTempDays, n_dateElement, value) counter1 += lt.size(n_dates)#el número de fechas que se agregó se suma al counter else: #si se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = dateListElement #se recorre la lista completa n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) #fecha a agregar trips = oms.get(catalog['dateTree'], n_dateElement, greater) #print(type(trips)) totaltrip = map.get(trips, 'total')['value'] value = (tempElement, totaltrip) map.put(tripsLeastTempDays, n_dateElement, value) counter1 += lt.size(dateListElement) while counter2<number: mostTempIterator = it.newIterator(mostTemp) #Iterar la lista con n temperaturas más altas while it.hasNext(mostTempIterator): tempElement = it.next(mostTempIterator) dateListElement = map.get(catalog['temperatureHash'],tempElement)['value']#Lista de todas las fechas para esa temperatura if number - lt.size(dateListElement) < counter2: #si no se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = lt.subList(dateListElement, 1, number-counter2) n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) value = oms.get(catalog['dateTree'], n_dateElement, greater) value = map.get(value, 'total')['value'] value = (tempElement, value) map.put(tripsMostTempDays, n_dateElement, value) counter2 += lt.size(n_dates) else: #si se pueden agregar todas las fechas de esa temperatura sin alcanzar el n deseado n_dates = dateListElement n_iterator = it.newIterator(n_dates) while it.hasNext(n_iterator): n_dateElement = it.next(n_iterator) value = oms.get(catalog['dateTree'], n_dateElement, greater) value = map.get(value, 'total')['value'] value = (tempElement, value) map.put(tripsMostTempDays, n_dateElement, value) counter2 += lt.size(dateListElement) if not map.isEmpty(tripsMostTempDays): tempList= map.keySet(tripsMostTempDays) iteratemp=it.newIterator(tempList) while it.hasNext(iteratemp): tempKey = it.next(iteratemp) response1 += str(tempKey) + ':' + str(map.get(tripsMostTempDays,tempKey)['value']) + " " if not map.isEmpty(tripsLeastTempDays): tempList= map.keySet(tripsLeastTempDays) iteratemp=it.newIterator(tempList) while it.hasNext(iteratemp): tempKey = it.next(iteratemp) response2 += str(tempKey) + ':' + str(map.get(tripsLeastTempDays,tempKey)['value']) + " " return response1, response2