以下是自己在最近闲来无事计算彩票中奖概率时用到的一些函数:
-
import operator
-
import itertools
-
import sys
-
-
-
# 组合函数
-
def C(n, k):
-
return reduce(operator.mul, range(n - k + 1, n + 1)) / reduce(operator.mul, range(1, k + 1))
-
-
-
# 购买组合复式的花费
-
def spendMoney(x, x1, y, y1, additional):
-
if additional:
-
return C(x, x1) * C(y, y1) * 3
-
else:
-
return C(x, x1) * C(y, y1) * 2
-
-
-
# 输出组合元组的列表
-
def combination(listData, num):
-
return list(itertools.combinations(listData, num))
-
-
-
# 重定向输出
-
def changeOut():
-
oldStdout = None
-
logfile = None
-
# try:
-
logfile = open('lotteryResult.txt', 'w+')
-
oldStdout = sys.stdout
-
sys.stdout = logfile
-
# finally:
-
# if logfile:
-
# logfile.close()
-
# if oldStdout:
-
# sys.stdout = oldStdout
阅读(541) | 评论(0) | 转发(0) |