Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
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
Пример #5
0
 def setUp (self):
     ht.put (self.table, '1', 'title1', self.comparekeys)
     ht.put (self.table, '2', 'title2', self.comparekeys)
     ht.put (self.table, '11', 'title3', self.comparekeys)
     ht.put (self.table, '3', 'title4', self.comparekeys)
     ht.put (self.table, '12', 'title5', self.comparekeys)
     ht.put (self.table, '5', 'title6', self.comparekeys)
Пример #6
0
def put(map, key, value):
    """
    Ingresa una pareja llave,valor a la tabla de hash.  Si la llave ya existe en la tabla, se reemplaza el valor.
    Es necesario proveer una función de comparación para las llaves.
    """
    ht.put(map, key, value)
    def test_delete (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)
        self.printTable (table)
        ht.remove (table, 'book1', self.comparekeyfunction)
        self.printTable (table)
    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_getvalues (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)

        ltset = lt.newList ('SINGLE_LINKED_LIST')
        ltset = ht.valueSet (table)
        iterator = it.newIterator (ltset)
        while it.hasNext (iterator):
            info = it.next (iterator)
            print (info)
Пример #10
0
    def test_getkeys(self):
        """
        """
        table = ht.newMap(capacity=7,
                          maptype='CHAINING',
                          comparefunction=self.comparefunction)

        ht.put(table, 'book1', 'title1')
        ht.put(table, 'book2', 'title2')
        ht.put(table, 'book3', 'title3')
        ht.put(table, 'book4', 'title4')
        ht.put(table, 'book5', 'title5')
        ht.put(table, 'book6', 'title6')

        ltset = ht.keySet(table)
        iterator = it.newIterator(ltset)
        while it.hasNext(iterator):
            info = it.next(iterator)
            print(info)
    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)
Пример #12
0
    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)
Пример #13
0
    def test_contains(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.assertTrue(ht.contains(table, '1', self.comparekeys))
        self.assertFalse(ht.contains(table, '15', self.comparekeys))
        self.assertTrue(ht.contains(table, '6', self.comparekeys))
        self.assertEqual(ht.size(table), 6)
Пример #14
0
def addToStationHash (catalog, row):
    value=(row['name'], row['city'])
    map.put(catalog['stationMap'], row['id'], value)
    def test_delete(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')
        self.printTable(table)
        entry = ht.remove(table, '3')
        self.printTable(table)
Пример #16
0
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
    def test_getkeys(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')

        ltset = lt.newList('SINGLE_LINKED_LIST')
        ltset = ht.keySet(table)
        iterator = it.newIterator(ltset)
        while it.hasNext(iterator):
            info = it.next(iterator)
            print(info)
Пример #18
0
    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)
Пример #19
0
    def test_put(self):
        """
        """
        table = ht.newMap(capacity=7,
                          maptype='CHAINING',
                          comparefunction=self.comparefunction)

        ht.put(table, 'book1', 'title1')
        ht.put(table, 'book2', 'title2')
        ht.put(table, 'book3', 'title3')
        ht.put(table, 'book4', 'title4')
        ht.put(table, 'book5', 'title5')
        ht.put(table, 'book6', 'title6')
        ht.put(table, 'book2', 'new-title 2')
        self.printTable(table)
        self.assertEqual(ht.size(table), 6)
Пример #20
0
    def test_getkeys(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)

        ltset = lt.newList('SINGLE_LINKED')
        ltset = ht.keySet(table)
        iterator = it.newIterator(ltset)
        while it.hasNext(iterator):
            info = it.next(iterator)
            print(info)
    def test_contains(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')

        self.assertTrue(ht.contains(table, '1'))
        self.assertFalse(ht.contains(table, '15'))
        self.assertTrue(ht.contains(table, '11'))