- Published on
pycheckio Building Base题解
- Authors
- Name
- Lif
pycheckio的一道题,太长了,所以不复制过来了。 主要是构造类,然后在类中构造方法,难点是方法中返回一个字典,目前没有找到除了硬写一个字典以外更好的办法
#!/usr/bin/env checkio --domain=py run building-base
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
def corners(self):
north_west = [self.south + self.width_NS, self.west]
north_east = [self.south + self.width_NS, self.west + self.width_WE]
south_west = [self.south, self.west]
south_east = [self.south, self.west + self.width_WE]
corner = {"north-west": north_west, "north-east": north_east, "south-west": south_west,
"south-east": south_east}
return corner
def area(self):
return self.width_NS*self.width_WE
def volume(self):
return self.area()*self.height
def __repr__(self):
return "Building({0}, {1}, {2}, {3}, {4})".format(self.south, self.west, self.width_WE, self.width_NS, self.height)
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"