1 """ 
   2 Seminar:FourBoxes problem in [yong27/2003-01-10]
   3 """
   4 import unittest
   5 
   6 class Rect:
   7     def set(self,x1,y1,x2,y2):
   8         self.xr=range(x1,x2)
   9         self.yr=range(y1,y2)
  10         self.tiles=[]
  11         for x in self.xr:
  12             for y in self.yr:
  13                 self.tiles.append((x,y))
  14     def getTiles(self):
  15         return self.tiles
  16     def __add__(self, aRect):
  17         for tile in aRect.tiles:
  18             if tile not in self.tiles:
  19                 self.tiles.append(tile)
  20         self.tiles.sort()
  21         return self
  22     def size(self):
  23         return len(self.tiles)
  24     
  25 class RectTest(unittest.TestCase):
  26     def testGetTiles(self):
  27         a=Rect()
  28         a.set(1,2,4,4)
  29         expected=[(1,2),(1,3),(2,2),(2,3),(3,2),(3,3)]
  30         self.assertEquals(expected, a.getTiles())
  31     def testAdd(self):
  32         a=Rect(); a.set(1,2,4,4)
  33         b=Rect(); b.set(0,2,3,4)
  34         expected=[(0,2),(0,3),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3)]
  35         total=a+b
  36         self.assertEquals(expected, total.getTiles())
  37     def testAddThree(self):
  38         a=Rect(); a.set(1,2,4,4)
  39         b=Rect(); b.set(0,2,3,4)
  40         c=Rect(); c.set(3,2,5,4)
  41         expected=[(0,2),(0,3),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3),(4,2),(4,3)]
  42         total=a+b+c
  43         self.assertEquals(expected, total.getTiles())
  44     def testGetSize(self):
  45         a=Rect()
  46         a.set(1,2,4,4)
  47         self.assertEquals(6, a.size())
  48     def testGetSizeTwo(self):
  49         a=Rect(); a.set(1,2,4,4)
  50         b=Rect(); b.set(0,2,3,4)
  51         total=a+b
  52         self.assertEquals(8, total.size())
  53     def testProblem(self):
  54         a=Rect(); a.set(1,2,4,4)
  55         b=Rect(); b.set(2,3,5,7)
  56         c=Rect(); c.set(3,1,6,5)
  57         d=Rect(); d.set(7,3,8,6)
  58         total=a+b+c+d
  59         self.assertEquals(26, total.size())
  60 
  61 if __name__=='__main__':
  62     unittest.main(argv=('','-v'))
web biohackers.net