#format python """ Seminar:FourBoxes problem in [yong27/2003-01-10] """ import unittest class Rect: def set(self,x1,y1,x2,y2): self.xr=range(x1,x2) self.yr=range(y1,y2) self.tiles=[] for x in self.xr: for y in self.yr: self.tiles.append((x,y)) def getTiles(self): return self.tiles def __add__(self, aRect): for tile in aRect.tiles: if tile not in self.tiles: self.tiles.append(tile) self.tiles.sort() return self def size(self): return len(self.tiles) class RectTest(unittest.TestCase): def testGetTiles(self): a=Rect() a.set(1,2,4,4) expected=[(1,2),(1,3),(2,2),(2,3),(3,2),(3,3)] self.assertEquals(expected, a.getTiles()) def testAdd(self): a=Rect(); a.set(1,2,4,4) b=Rect(); b.set(0,2,3,4) expected=[(0,2),(0,3),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3)] total=a+b self.assertEquals(expected, total.getTiles()) def testAddThree(self): a=Rect(); a.set(1,2,4,4) b=Rect(); b.set(0,2,3,4) c=Rect(); c.set(3,2,5,4) expected=[(0,2),(0,3),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3),(4,2),(4,3)] total=a+b+c self.assertEquals(expected, total.getTiles()) def testGetSize(self): a=Rect() a.set(1,2,4,4) self.assertEquals(6, a.size()) def testGetSizeTwo(self): a=Rect(); a.set(1,2,4,4) b=Rect(); b.set(0,2,3,4) total=a+b self.assertEquals(8, total.size()) def testProblem(self): a=Rect(); a.set(1,2,4,4) b=Rect(); b.set(2,3,5,7) c=Rect(); c.set(3,1,6,5) d=Rect(); d.set(7,3,8,6) total=a+b+c+d self.assertEquals(26, total.size()) if __name__=='__main__': unittest.main(argv=('','-v'))