python计算24点
2010-12-22 18:50
239 查看
输入4个数字, 输出所有用加减乘除结果为24的表达式. 代码如下:
def isEqual(num1, num2):
return abs(num1 - num2) < 1e-5;
# End of isEqual().
def expressionsFromExpOpSeq(expSeq, opSeq):
assert(len(expSeq) == len(opSeq) + 1)
if 1 == len(expSeq):
yield expSeq[0]
return
# End of if.
for idx in range(len(opSeq)):
newExp = "(%s %s %s)" % (
expSeq[idx], opSeq[idx], expSeq[idx+1])
newExpSeq = expSeq[:idx] + [newExp] + expSeq[idx+2:]
newOpSeq = opSeq[:idx] + opSeq[idx+1:]
for exp in expressionsFromExpOpSeq(newExpSeq, newOpSeq):
yield exp
# End of for.
# End of expressionsFromExpOpSeq().
def expressionsFromExpSeq(expSeq):
for opSeq in selections("+-*/", len(expSeq) - 1):
for exp in expressionsFromExpOpSeq(expSeq, opSeq):
yield exp
# End of expressionsFromExpSeq().
def expressions(numList):
'''Generate expressions.
'''
if (0 == len(numList)): return;
for numPerm in permutations(numList):
expSeq = [str(n) for n in numPerm]
for exp in expressionsFromExpSeq(expSeq):
yield exp
# End of expressions().
numTuple=input("Please input 4 numbers to computer 24:(use ',' to divide them)")
numList=[float(i) for i in numTuple]
# numList = [1.0,4.0,5.0,6.0]
TARGET = 24.0
print("Your input is: %s" % numList)
for exp in expressions(numList):
try:
if isEqual(TARGET, eval(exp)):
print("%s = %s" % (exp, TARGET))
except ZeroDivisionError:
pass
# End of try.
# End of for.
其中计算排列组合的代码来自Cookbook:
# Recipe 19.15. Generating Permutations, Combinations, and Selections
def _combinators(_handle, items, n):
''' factored-out common structure of all following combinators '''
if n==0:
yield [ ]
return
for i, item in enumerate(items):
this_one = [ item ]
for cc in _combinators(_handle, _handle(items, i), n-1):
yield this_one + cc
def combinations(items, n):
''' take n distinct items, order matters '''
def skipIthItem(items, i):
return items[:i] + items[i+1:]
return _combinators(skipIthItem, items, n)
def selections(items, n):
''' take n (not necessarily distinct) items, order matters '''
def keepAllItems(items, i):
return items
return _combinators(keepAllItems, items, n)
def permutations(items):
''' take all items, order matters '''
return combinations(items, len(items))
示例:
Please input 4 numbers to computer 24:(use ',' to divide them)1,4,5,6
Your input is: [1.0, 4.0, 5.0, 6.0]
(4.0 / (1.0 - (5.0 / 6.0))) = 24.0
(6.0 / ((5.0 / 4.0) - 1.0)) = 24.0
也可以输入多个数字:
Please input 4 numbers to computer 24:(use ',' to divide them)2,3,4,5.5,6
Your input is: [2.0, 3.0, 4.0, 5.5, 6.0]
((2.0 - (3.0 / (4.0 - 5.5))) * 6.0) = 24.0
(2.0 * (3.0 - ((4.0 - 5.5) * 6.0))) = 24.0
((2.0 + (3.0 / (5.5 - 4.0))) * 6.0) = 24.0
......
参考:
* 一个 Lua 的凑24程序 ( http://blog.csdn.net/shell_picker/archive/2010/11/25/6034158.aspx )
* python 算24的题目 ( http://zhidao.baidu.com/question/207378450.html )
def isEqual(num1, num2):
return abs(num1 - num2) < 1e-5;
# End of isEqual().
def expressionsFromExpOpSeq(expSeq, opSeq):
assert(len(expSeq) == len(opSeq) + 1)
if 1 == len(expSeq):
yield expSeq[0]
return
# End of if.
for idx in range(len(opSeq)):
newExp = "(%s %s %s)" % (
expSeq[idx], opSeq[idx], expSeq[idx+1])
newExpSeq = expSeq[:idx] + [newExp] + expSeq[idx+2:]
newOpSeq = opSeq[:idx] + opSeq[idx+1:]
for exp in expressionsFromExpOpSeq(newExpSeq, newOpSeq):
yield exp
# End of for.
# End of expressionsFromExpOpSeq().
def expressionsFromExpSeq(expSeq):
for opSeq in selections("+-*/", len(expSeq) - 1):
for exp in expressionsFromExpOpSeq(expSeq, opSeq):
yield exp
# End of expressionsFromExpSeq().
def expressions(numList):
'''Generate expressions.
'''
if (0 == len(numList)): return;
for numPerm in permutations(numList):
expSeq = [str(n) for n in numPerm]
for exp in expressionsFromExpSeq(expSeq):
yield exp
# End of expressions().
numTuple=input("Please input 4 numbers to computer 24:(use ',' to divide them)")
numList=[float(i) for i in numTuple]
# numList = [1.0,4.0,5.0,6.0]
TARGET = 24.0
print("Your input is: %s" % numList)
for exp in expressions(numList):
try:
if isEqual(TARGET, eval(exp)):
print("%s = %s" % (exp, TARGET))
except ZeroDivisionError:
pass
# End of try.
# End of for.
其中计算排列组合的代码来自Cookbook:
# Recipe 19.15. Generating Permutations, Combinations, and Selections
def _combinators(_handle, items, n):
''' factored-out common structure of all following combinators '''
if n==0:
yield [ ]
return
for i, item in enumerate(items):
this_one = [ item ]
for cc in _combinators(_handle, _handle(items, i), n-1):
yield this_one + cc
def combinations(items, n):
''' take n distinct items, order matters '''
def skipIthItem(items, i):
return items[:i] + items[i+1:]
return _combinators(skipIthItem, items, n)
def selections(items, n):
''' take n (not necessarily distinct) items, order matters '''
def keepAllItems(items, i):
return items
return _combinators(keepAllItems, items, n)
def permutations(items):
''' take all items, order matters '''
return combinations(items, len(items))
示例:
Please input 4 numbers to computer 24:(use ',' to divide them)1,4,5,6
Your input is: [1.0, 4.0, 5.0, 6.0]
(4.0 / (1.0 - (5.0 / 6.0))) = 24.0
(6.0 / ((5.0 / 4.0) - 1.0)) = 24.0
也可以输入多个数字:
Please input 4 numbers to computer 24:(use ',' to divide them)2,3,4,5.5,6
Your input is: [2.0, 3.0, 4.0, 5.5, 6.0]
((2.0 - (3.0 / (4.0 - 5.5))) * 6.0) = 24.0
(2.0 * (3.0 - ((4.0 - 5.5) * 6.0))) = 24.0
((2.0 + (3.0 / (5.5 - 4.0))) * 6.0) = 24.0
......
参考:
* 一个 Lua 的凑24程序 ( http://blog.csdn.net/shell_picker/archive/2010/11/25/6034158.aspx )
* python 算24的题目 ( http://zhidao.baidu.com/question/207378450.html )
相关文章推荐
- python计算24点
- Python写的计算24点小游戏
- 十四、Python编程计算24点(之二)
- 十三、Python编程计算24点(之一)
- Python之计算24点
- python中numpy基础学习及进行数组和矢量计算
- Anaconda-科学计算的 Python 发行版
- 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)
- Python计算一个文件里字数的方法
- python分别用while和for于vcf格式提取复等位基因的snp(并计算分别条数)
- python 时间差的计算
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- python time 计算日期
- python实现带误差传递的数学计算
- python计算
- Python 计算某个目录下的文件md5并且以md5批量重命名
- 使用python计算夏普比率与最大回撤和最大回撤时间的程序
- 代码干货 | Python高性能计算库——Numba
- 坚持#第232天~零基础自学云计算基础语言应用之python第8节