Need help with Lab06


Joined
Dec 30, 2022
Messages
5
Reaction score
1
this is my assignment: https://ucsb-cs9.github.io/s23/lab/lab06/
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
?                                             ^^

This is my current code:
Apartment.py
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
        )
lab06.py
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
        )
lab06.py
Python:
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
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()
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top