python-checkio/Github/building-base.py

48 lines
1.4 KiB
Python
Raw Normal View History

2020-01-03 14:31:18 +01:00
class Building:
def __init__(self, south, west, width_WE, width_NS, height=10):
self.x = west
self.y = south
self.width = width_WE
self.height = width_NS
self.depth = height
def corners(self):
return {
"south-west": [self.y, self.x],
"south-east": [self.y, self.x + self.width],
"north-west": [self.y + self.height, self.x],
"north-east": [self.y + self.height, self.x + self.width],
}
def area(self):
return self.width * self.height
def volume(self):
return self.area() * self.depth
def __repr__(self):
return "Building({}, {}, {}, {}, {})".format(
self.y,
self.x,
self.width,
self.height,
self.depth
)
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 b.area() == 6, "Area"
assert b.volume() == 60, "Volume"
assert b2.volume() == 30, "Volume2"
assert json_dict(b.corners()) == {'north-east': [4, 4], 'south-east': [1, 4],
'south-west': [1, 2], 'north-west': [4, 2]}, "Corners"
assert str(b) == "Building(1, 2, 2, 3, 10)", "String"