Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Metaclass Implementation(Faster than normal) solution in Speedy category for Building Base by Arandomusername
def c_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
def c_corners(self):
north, west, east, south = self.south + self.width_NS, self.west, self.west + self.width_WE, self.south
return {
"north-west": [north, west], "north-east": [north, east],
"south-west": [south, west], "south-east": [south, east]}
def c_area(self):
return self.width_WE * self.width_NS
def c_volume(self):
return self.width_WE * self.width_NS * self.height
def c_repr(self):
return "Building(%r, %r, %r, %r, %r)" % (self.south, self.west, self.width_WE, self.width_NS, self.height)
Building = type('Building', (), {'__init__': c_init, 'corners': c_corners,
'area': c_area, 'volume': c_volume,'__repr__':c_repr})
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"
Feb. 26, 2015
Comments: