Issues with Code and Test Failures in Lab06: Apartment Sorting and Analysis

the full details of my problem are here:
this is my assignment: lab06 - Sorting Apartments -

my errors:

[CODE]mergesort is O(NLogN) (20/20)

Running mergesort(custom array)…

Completed mergesort(custom array): O(nlog(n))

Time took: 0.56830s

ensureSortedAscending(apartmentList) (0/10)

Test Failed:

mergesort sorts Apartment objects in ascending order (0/20)

Warning! Make sure your >, <, and == operators work. If they all work, check your mergesort function

Test Failed:

== operator overloading (5/5)

getRent(self), getMetersFromUCSB(self), and getCondition(self) (5/5)

operator overloading (0/5)

Failing Test: comparisons on metersFromUCSB attribute

Failing Test: comparisons on condition attribute

Test Failed: True != False

< operator overloading (0/5)

Failing Test: comparisons on metersFromUCSB attribute

Failing Test: comparisons on condition attribute

Test Failed: False != True

init for Apartment class (5/5)

getAffordableApartments(apartmentList, budget) (0/15)

Test Failed: ‘(Apa[52 chars]ion: bad\n(Apartment) Rent: $100, Distance Fro[30 chars]lent’ != ‘(Apa[52 chars]ion: excellent\n(Apartment) Rent: $100, Distan[30 chars] bad’

  • (Apartment) Rent: $100, Distance From UCSB: 200m, Condition: bad

  • (Apartment) Rent: $100, Distance From UCSB: 200m, Condition: excellent+ (Apartment) Rent: $100, Distance From UCSB: 200m, Condition: excellent

? +

  • (Apartment) Rent: $100, Distance From UCSB: 200m, Condition: bad

getBestApartment(apartmentList) (0/5)

Test Failed: ‘(Apartment) Rent: $100, Distance From UCSB: 110m, Condition: bad’ != ‘(Apartment) Rent: $1200, Distance From UCSB: 200m, Condition: excellent’

  • (Apartment) Rent: $100, Distance From UCSB: 110m, Condition: bad

? ^^ ^^^

  • (Apartment) Rent: $1200, Distance From UCSB: 200m, Condition: excellent

? + ^^ ^^^^^^^^^

getWorstApartment(apartmentList) (0/5)

Test Failed: ‘(Apartment) Rent: $1200, Distance From UCSB: 200m, Condition: bad’ != ‘(Apartment) Rent: $100, Distance From UCSB: 110m, Condition: bad’

  • (Apartment) Rent: $1200, Distance From UCSB: 200m, Condition: bad

? - ^^

  • (Apartment) Rent: $100, Distance From UCSB: 110m, Condition: bad

? ^[1]

This is my current code:

Apartment.py

[CODE=python]class Apartment:

def __init__(self, rent, metersFromUCSB, condition):

    self.rent = rent

    self.metersFromUCSB = metersFromUCSB

    self.condition = condition

    self.conditionFactor = self.calculate_condition_factor()



def calculate_condition_factor(self):

    if self.condition == 'bad':

        return 1

    elif self.condition == 'average':

        return 2

    elif self.condition == 'excellent':

        return 3

    else:

        raise ValueError("Invalid condition")



def getRent(self):

    return self.rent



def getMetersFromUCSB(self):

    return self.metersFromUCSB



def getCondition(self):

    return self.condition



def getApartmentDetails(self):

    return f'(Apartment) Rent: ${self.rent}, Distance From UCSB: {self.metersFromUCSB}m, Condition: {self.condition}'



def __gt__(self, other):

    if self.rent > other.rent:

        return True

    elif self.rent == other.rent:

        if self.metersFromUCSB < other.metersFromUCSB:

            return True

        elif self.metersFromUCSB == other.metersFromUCSB:

            return self.conditionFactor > other.conditionFactor

    return False



def __lt__(self, other):

    if self.rent < other.rent:

        return True

    elif self.rent == other.rent:

        if self.metersFromUCSB > other.metersFromUCSB:

            return True

        elif self.metersFromUCSB == other.metersFromUCSB:

            return self.conditionFactor < other.conditionFactor

    return False



def __eq__(self, other):

    return (

        self.rent == other.rent

        and self.metersFromUCSB == other.metersFromUCSB

        and self.conditionFactor == other.conditionFactor

    )[/CODE]

lab06.py

[CODE=python]class Apartment:

def __init__(self, rent, metersFromUCSB, condition):

    self.rent = rent

    self.metersFromUCSB = metersFromUCSB

    self.condition = condition

    self.conditionFactor = self.calculate_condition_factor()



def calculate_condition_factor(self):

    if self.condition == 'bad':

        return 1

    elif self.condition == 'average':

        return 2

    elif self.condition == 'excellent':

        return 3

    else:

        raise ValueError("Invalid condition")



def getRent(self):

    return self.rent



def getMetersFromUCSB(self):

    return self.metersFromUCSB



def getCondition(self):

    return self.condition



def getApartmentDetails(self):

    return f'(Apartment) Rent: ${self.rent}, Distance From UCSB: {self.metersFromUCSB}m, Condition: {self.condition}'



def __gt__(self, other):

    if self.rent > other.rent:

        return True

    elif self.rent == other.rent:

        if self.metersFromUCSB < other.metersFromUCSB:

            return True

        elif self.metersFromUCSB == other.metersFromUCSB:

            return self.conditionFactor > other.conditionFactor

    return False



def __lt__(self, other):

    if self.rent < other.rent:

        return True

    elif self.rent == other.rent:

        if self.metersFromUCSB > other.metersFromUCSB:

            return True

        elif self.metersFromUCSB == other.metersFromUCSB:

            return self.conditionFactor < other.conditionFactor

    return False



def __eq__(self, other):

    return (

        self.rent == other.rent

        and self.metersFromUCSB == other.metersFromUCSB

        and self.conditionFactor == other.conditionFactor

    )[/CODE]

lab06.py


from Apartment import Apartment

def mergesort(apartmentList):

    if len(apartmentList) > 1:

        left_arr = apartmentList[:len(apartmentList) // 2]

        right_arr = apartmentList[len(apartmentList) // 2:]



        # recursion

        mergesort(left_arr)

        mergesort(right_arr)



        # merge

        i = 0 #left_arr index

        j = 0 #right_arr index

        k = 0 #merged array index

        while i < len(left_arr) and j < len(right_arr):

            if left_arr[i] < right_arr[j]:

                apartmentList[k] = left_arr[i]

                i += 1

            else:

                apartmentList[k] = right_arr[j]

                j += 1

            k += 1



        while i < len(left_arr):

            apartmentList[k] = left_arr[i]

            i += 1

            k += 1



        while j < len(right_arr):

            apartmentList[k] = right_arr[j]

            j += 1

            k += 1



def ensureSortedAscending(apartmentList):

    for i in range(len(apartmentList) - 1):

        if apartmentList[i] > apartmentList[i + 1]:

            return False

    return True



def getBestApartment(apartmentList):

    mergesort(apartmentList)

    return apartmentList[-1].getApartmentDetails()



def getWorstApartment(apartmentList):

    mergesort(apartmentList)

    return apartmentList[0].getApartmentDetails()



def getAffordableApartments(apartmentList, budget):

    mergesort(apartmentList)

    affordable_apartments = [apartment for apartment in apartmentList if apartment.getRent() <= budget]

    if not affordable_apartments:

        return ""

    return '\n'.join([apartment.getApartmentDetails() for apartment in affordable_apartments])

testFile.py

[CODE=python]from Apartment import Apartment

from lab06 import *

#good

def test_apartment_getters():

a1 = Apartment(1204, 200, "bad")

assert a1.getRent() == 1204

assert a1.getMetersFromUCSB() == 200

assert a1.getCondition() == "bad"

#good

def test_apartment_details():

a2 = Apartment(1500, 300, "excellent")

assert a2.getApartmentDetails() == "(Apartment) Rent: $1500, Distance From UCSB: 300m, Condition: excellent"

test_apartment_getters()

test_apartment_details()

good

def test_apartment_sorting():

a0 = Apartment(1115, 215, "bad")

a1 = Apartment(950, 215, "average")

a2 = Apartment(950, 215, "excellent")

a3 = Apartment(950, 190, "excellent")

a4 = Apartment(900, 190, "excellent")

a5 = Apartment(500, 250, "bad")

apartmentList = [a0, a1, a2, a3, a4, a5]



assert not ensureSortedAscending(apartmentList)



mergesort(apartmentList)

assert ensureSortedAscending(apartmentList)

def test_apartment_best_worst():

a0 = Apartment(1200, 200, "average")

a1 = Apartment(1200, 200, "excellent")

a2 = Apartment(1000, 100, "average")

a3 = Apartment(1000, 215, "excellent")

a4 = Apartment(700, 315, "bad")

a5 = Apartment(800, 250, "excellent")

apartmentList = [a0, a1, a2, a3, a4, a5]



assert not ensureSortedAscending(apartmentList)



best_apartment = getBestApartment(apartmentList)

worst_apartment = getWorstApartment(apartmentList)

#print(best_apartment)

#print(worst_apartment)

# undo comments in the future

#assert best_apartment == a1

#assert worst_apartment == a4

def test_affordable_apartments():

a0 = Apartment(1115, 215, "bad")

a1 = Apartment(970, 215, "average")

a2 = Apartment(950, 215, "average")

a3 = Apartment(950, 190, "excellent")

a4 = Apartment(900, 190, "excellent")

a5 = Apartment(500, 250, "bad")

apartmentList = [a0, a1, a2, a3, a4, a5]



affordable_apartments = getAffordableApartments(apartmentList, 950)

assert affordable_apartments == [a2, a3, a4]

test_apartment_sorting()

test_apartment_best_worst()

#test_affordable_apartments()[/CODE]


  1. /CODE ↩︎

The code and error are pretty illegible as you haven’t bothered to format them in your post. If you did that you would be more likely to get a sensible answer. I’ll just say:

assert best_apartment == a1

and a1 is the most expensive apartment which isn’t usually ‘best’ unless you’re the landlord.

Look at the example given for a sorted list:

(Apartment) Rent: $500, Distance From UCSB: 250m, Condition: bad
(Apartment) Rent: $900, Distance From UCSB: 190m, Condition: excellent
(Apartment) Rent: $950, Distance From UCSB: 190m, Condition: excellent
(Apartment) Rent: $950, Distance From UCSB: 215m, Condition: excellent
(Apartment) Rent: $950, Distance From UCSB: 215m, Condition: average
(Apartment) Rent: $1115, Distance From UCSB: 215m, Condition: bad

It’s ordered from lower to higher rent, from shorter to longer distance, from excellent to average.

So which is the ‘best’ and which is the ‘worst’?

Also, you have:

assert not ensureSortedAscending(apartmentList)

Are you sure that’s correct?

That part looks right to me. It’s given a particular list, and that list is not sorted, so the method should return False.

What’s missing, however, is any tests of comparisons between particular items (at least as far as I can tell—proper formatting would help here). It does look like the errors are related to the comparisons specifically, so writing those tests would probably help find the problem.

Yes, you’re right. That line precedes the sort and there’s another assertion afterwards.