1 """Lotto play for fun by [yong27]. See [Lotto]
   2 Lotto results are in http://www.kbstar.com
   3 """
   4 import unittest, random
   5 
   6 class LottoGenerator:
   7     def __init__(self, aSeed=None):
   8         self.r=random.Random(aSeed)
   9         self.pool=range(1,46)
  10     def getOneGame(self):
  11         raise NotImplementedError
  12     def getNGames(self, n):
  13         self.nresult=[]
  14         for i in range(n):
  15             self.nresult.append(self.getOneGame())
  16         return self.nresult
  17     def verifyJackPot(self, aJackPot):
  18         hitList=[]
  19         for ns in self.nresult:
  20             hit=0
  21             for n in ns:
  22                 if n in aJackPot:
  23                     hit+=1
  24             hitList.append(hit)
  25         return hitList
  26 
  27 class ShuffleLottoGenerator(LottoGenerator):
  28     def __init__(self, aSeed=None):
  29         LottoGenerator.__init__(self, aSeed)
  30     def getOneGame(self):
  31         self.r.shuffle(self.pool)
  32         ns=self.pool[:6]
  33         ns.sort()
  34         return ns
  35 
  36 class ChoiceLottoGenerator(LottoGenerator):
  37     def __init__(self, aSeed=None):
  38         LottoGenerator.__init__(self, aSeed)
  39     def getOneGame(self):
  40         pool=range(1,46)
  41         hns=[]
  42         for i in range(6):
  43             self.r.shuffle(pool)
  44             hn = self.r.choice(pool)
  45             hns.append(hn)
  46             pool.remove(hn)
  47         hns.sort()
  48         return hns
  49 
  50 class LottoGeneratorTest(unittest.TestCase):
  51     def setUp(self):
  52         self.lg = ShuffleLottoGenerator(1)
  53     def testOneGame(self):
  54         self.assertEquals(6,len(self.lg.getOneGame()))
  55     def testNGames(self):
  56         expected = [[6, 12, 19, 20, 35, 40],
  57                     [5, 10, 27, 29, 32, 37],
  58                     [11, 12, 15, 16, 20, 21],
  59                     [11, 20, 24, 30, 31, 32],
  60                     [1, 2, 10, 17, 26, 28],
  61                    ]
  62         self.assertEquals(expected,self.lg.getNGames(5))
  63     def testVerifyJackPot(self):
  64         expected=[1,1,0,0,2]
  65         self.lg.getNGames(5)
  66         self.assertEquals(expected, self.lg.verifyJackPot([1,2,3,4,5,6]))
  67 
  68 def play(aSeed, aNumOfGames=5, aJackPot=None):
  69     #lg = ShuffleLottoGenerator(aSeed)
  70     lg = ChoiceLottoGenerator(aSeed)
  71     import pprint
  72     print "=== numbers ==="
  73     pprint.pprint(lg.getNGames(aNumOfGames))
  74     print
  75     if aJackPot:
  76         print "=== hit numbers ==="
  77         print lg.verifyJackPot(aJackPot)
  78         
  79 if __name__=='__main__':
  80     #unittest.main(argv=('','-v'))
  81     play(20030208)
web biohackers.net