ordering __cmp__, order according to two parameters

3

Holaa

the code is as follows,

def es_numero(valor):
    return isinstance(valor, (int, float, long, complex) )

def es_cadena_no_vacia (valor):
    return isinstance (valor, (str))


class Hotel(object):

    def __init__ (self, nombre = '*', ubicacion = '*',
                 puntaje = 0, precio = float("inf")):


        if es_cadena_no_vacia (nombre):
            self.nombre = nombre
        else:
            raise TypeError ("El nombre debe ser una cadena no vacía")

        if es_cadena_no_vacia (ubicacion):
            self.ubicacion = ubicacion
        else:
            raise TypeError ("La ubicación debe ser una cadena no vacía")

        if es_numero(puntaje):
            self.puntaje = puntaje
        else:
            raise TypeError ("El puntaje debe ser un número")

        if es_numero(precio):
            if precio != 0:
                self.precio = precio
            else:
                self.precio = float("inf")
        else:
            raise TypeError("El precio debe ser un número")

    def __str__(self):
        """ Muestra el hotel según lo requerido. """
        return self.nombre + " de "+ self.ubicacion+ \
                " - Puntaje: "+ str(self.puntaje) + " - Precio: "+ \
                str(self.precio)+ " pesos."

    def ratio (self):
        """ Calcula la relación calidad-precio de un hotel de acuerdo a la fórmula que nos dio el cliente. """
        return ((self.puntaje**2)*10.)/self.precio

h1 = Hotel("Hotel Guadalajara", "Pinamar", 1, 35)

h2 = Hotel("Hostería París", "Aosario", 1, 35)

h3 = Hotel("Apart-Hotel Estocolmo", "Esquel", 3, 105)

h4 = Hotel("Posada El Cairo", "Salta", 2.5, 15)

lista = [ h1, h2, h3, h4 ]

lista.sort()

for Hotel in lista:

    print Hotel

The exercise that I am not able to solve is the following:

  

Modify the cmp Hotel method so that the hotel lists can be sorted according to criteria: first by location, in alphabetical order and within each location by the price-quality ratio.

That is, if I understand correctly, if two hotels are called equal, order according to price (ratio).

How would I achieve this? I am infinitely grateful for any guidance response at least since I lack some knowledge, and try some ways, and I can not get it out .. may not be understanding well / or is not quite expressed the statement of the exercise? (from here: link )

Thank you very much !!

    
asked by equipo_de_gnc 13.08.2016 в 04:52
source

2 answers

0

A possible solution to your problem would be to order the columns as follows, first order by quality and then by the name of the hotel.

This solution only works if the sorting keeps the order of the initial instances.

    
answered by 17.08.2016 в 09:47
0

This is my solution:

# coding=utf-8


def es_numero(valor):
    return isinstance(valor, (int, float, long, complex))


def es_cadena_no_vacia(valor):
    return isinstance(valor, (str))


class Hotel(object):
    def __init__(self, nombre='*', ubicacion='*',
                 puntaje=0, precio=float("inf")):

        if es_cadena_no_vacia(nombre):
            self.nombre = nombre
        else:
            raise TypeError("El nombre debe ser una cadena no vacía")

        if es_cadena_no_vacia(ubicacion):
            self.ubicacion = ubicacion
        else:
            raise TypeError("La ubicación debe ser una cadena no vacía")

        if es_numero(puntaje):
            self.puntaje = puntaje
        else:
            raise TypeError("El puntaje debe ser un número")

        if es_numero(precio):
            if precio != 0:
                self.precio = precio
            else:
                self.precio = float("inf")
        else:
            raise TypeError("El precio debe ser un número")

    def __str__(self):
        """ Muestra el hotel según lo requerido. """
        return self.nombre + " de " + self.ubicacion + \
               " - Puntaje: " + str(self.puntaje) + " - Precio: " + \
               str(self.precio) + " pesos."

    def __cmp__(self, other):
        if self.ubicacion == other.ubicacion:
            if self.ratio() > other.ratio():
                return 1
            elif self.ratio() < other.ratio():
                return -1
            else:
                return 0
        else:
            return self.ubicacion > other.ubicacion

    def ratio(self):
        """ Calcula la relación calidad-precio de un hotel de acuerdo a la fórmula que nos dio el cliente. """
        return ((self.puntaje ** 2) * 10.) / self.precio


h1 = Hotel("Hotel Guadalajara", "Pinamar", 1, 35)

h2 = Hotel("Hostería París", "Aosario", 1, 35)

h3 = Hotel("Apart-Hotel Estocolmo", "Esquel", 3, 105)

h4 = Hotel("Posada El Cairo", "Salta", 2.5, 15)

print h1 > h2
print h2 > h3
    
answered by 27.11.2016 в 21:17