#format python """Lotto play for fun by [yong27]. See [Lotto] Lotto results are in http://www.kbstar.com """ import unittest, random class LottoGenerator: def __init__(self, aSeed=None): self.r=random.Random(aSeed) self.pool=range(1,46) def getOneGame(self): raise NotImplementedError def getNGames(self, n): self.nresult=[] for i in range(n): self.nresult.append(self.getOneGame()) return self.nresult def verifyJackPot(self, aJackPot): hitList=[] for ns in self.nresult: hit=0 for n in ns: if n in aJackPot: hit+=1 hitList.append(hit) return hitList class ShuffleLottoGenerator(LottoGenerator): def __init__(self, aSeed=None): LottoGenerator.__init__(self, aSeed) def getOneGame(self): self.r.shuffle(self.pool) ns=self.pool[:6] ns.sort() return ns class ChoiceLottoGenerator(LottoGenerator): def __init__(self, aSeed=None): LottoGenerator.__init__(self, aSeed) def getOneGame(self): pool=range(1,46) hns=[] for i in range(6): self.r.shuffle(pool) hn = self.r.choice(pool) hns.append(hn) pool.remove(hn) hns.sort() return hns class LottoGeneratorTest(unittest.TestCase): def setUp(self): self.lg = ShuffleLottoGenerator(1) def testOneGame(self): self.assertEquals(6,len(self.lg.getOneGame())) def testNGames(self): expected = [[6, 12, 19, 20, 35, 40], [5, 10, 27, 29, 32, 37], [11, 12, 15, 16, 20, 21], [11, 20, 24, 30, 31, 32], [1, 2, 10, 17, 26, 28], ] self.assertEquals(expected,self.lg.getNGames(5)) def testVerifyJackPot(self): expected=[1,1,0,0,2] self.lg.getNGames(5) self.assertEquals(expected, self.lg.verifyJackPot([1,2,3,4,5,6])) def play(aSeed, aNumOfGames=5, aJackPot=None): #lg = ShuffleLottoGenerator(aSeed) lg = ChoiceLottoGenerator(aSeed) import pprint print "=== numbers ===" pprint.pprint(lg.getNGames(aNumOfGames)) print if aJackPot: print "=== hit numbers ===" print lg.verifyJackPot(aJackPot) if __name__=='__main__': #unittest.main(argv=('','-v')) play(20030208)