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 !!