Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple OOP with properties for north and east solution in Clear category for Building Base by WittyWalrus
class Building:
def __init__(self, south, west, width_WE, width_NS, height=10):
self.south = south
self.west = west
self.width_WE = width_WE
self.width_NS = width_NS
self.height = height
@property
def north(self):
return self.south + self.width_NS
@property
def east(self):
return self.west + self.width_WE
def corners(self):
return {
"north-west": [self.north, self.west],
"north-east": [self.north, self.east],
"south-west": [self.south, self.west],
"south-east": [self.south, self.east],
}
def area(self):
return self.width_WE * self.width_NS
def volume(self):
return self.area() * self.height
def __repr__(self):
return (
"{0.__class__.__name__}({0.south}, {0.west}, "
"{0.width_WE}, {0.width_NS}, {0.height})".format(self)
)
if __name__ == "__main__":
# These "asserts" using only for self-checking and not necessary for auto-testing
def json_dict(d):
return dict((k, list(v)) for k, v in d.items())
b = Building(1, 2, 2, 3)
b2 = Building(1, 2, 2, 3, 5)
assert json_dict(b.corners()) == {
"north-east": [4, 4],
"south-east": [1, 4],
"south-west": [1, 2],
"north-west": [4, 2],
}, "Corners"
assert b.area() == 6, "Area"
assert b.volume() == 60, "Volume"
assert b2.volume() == 30, "Volume2"
assert str(b) == "Building(1, 2, 2, 3, 10)", "String"
July 17, 2024