Differences between revisions 2 and 11 (spanning 9 versions)
Revision 2 as of 2004-08-12 17:35:34
Size: 2985
Editor: Zoom.Quiet
Comment: Alex Dong
Revision 11 as of 2009-12-25 07:18:56
Size: 5914
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from PyPorgramGames/24point
Line 6: Line 7:
-- Zoom.Quiet [[[DateTime(2004-08-12T16:39:28Z)]]]
[[TableOfContents]]
-- Zoom.Quiet [<<DateTime(2004-08-12T16:39:28Z)>>]
<<TableOfContents>>
Line 54: Line 55:

= Chaox =
 <[email protected]>
 
刚刚开始学python,试着写了一个24点的程序,遍历所有的解法,最多是24×64×5=7680种可能。有点丑陋而且重解很多,不过还是发上来让大家扔一下。。。python真的是很强大啊~~

{{{
#!python
from __future__ import division
from operator import add, sub, mul
from time import *



div = lambda x, y : x/y
ops = [add, sub, mul, div]
opsc = ['+','-','*','/']
equation = []

data = input('Pls input the four numbers:(separate them by comma)')

xtime = time()

#--------------------------------------------------------
# list all the sort according to the four numbers, it is
# copied from AllStartFromGame at http://python.cn
#--------------------------------------------------------
def permute(seq):
  l = len(seq)
  if l == 1:
    return [seq]
  else:
    res=[]
    for i in range(len(seq)):
      rest = seq[:i] + seq[i+1:]
      for x in permute(rest):
        res.append(seq[i:i+1] + x)
    return res

listofall = permute(data)
newlist = list(set(listofall))


#--------------------------------------------------------
# consider the five different structures of combinations
#--------------------------------------------------------
for a in newlist:
    for i in range(4):
        for j in range(4):
            for k in range(4):
                express = []
                value = (a[0],opsc[i],a[1],opsc[j],a[2],opsc[k],a[3])
                express.append("((%d %s %d) %s %d) %s %d" % value)
                express.append("(%d %s (%d %s %d)) %s %d" % value)
                express.append("%d %s ((%d %s %d) %s %d)" % value)
                express.append("%d %s (%d %s (%d %s %d))" % value)
                express.append("(%d %s %d) %s (%d %s %d)" % value)
                for s in express:
                    try:
                        x = eval(s)
                    except:
                        continue
                    if x < 24.00001 and x > 23.99999:
                        equation.append(s)

ytime = time()
cacltime = ytime - xtime

equation = list(set(equation))
for e in equation:
    print e
    
print "there are %d" % len(equation), "items at all"
print "all the time of calculation is %s s" % cacltime
}}}

== 实例 ==

在[[http://wiki.woodpecker.org.cn/moin/Chaox|Chaox]]底部有个用pygame实现的24点游戏的实例
Line 55: Line 136:
 * ["24pointHowto"] -- 关于二十四点游戏的编程思路与基本算法  * [[24pointHowto]] -- 关于二十四点游戏的编程思路与基本算法
 * 网络社团--技术文档--[[http://www.588188.com/netbook/tech/java/html/java.ohchina.186.htm|算24点程序]]:原理,面向过程的C实现,面向对象的__
Line 57: Line 139:

= Alex Dong <[email protected]> =
 * 来个[[24pointAandQ]]吧.
= Alex Dong =
<[email protected]>
Line 77: Line 159:
= monkeyXite =
程序员 2002年的时候编程擂台里边出过的题和Alex Dong兄的扩展以后的问题一样的,基本算法一样的
最好在递归中作些优化吧…………写出来看看吧

[python-chinese] 一个计算24点(或n点)的游戏.

-- Zoom.Quiet [2004-08-12 16:39:28]

hoxide

自己曾经写过一个,但代码找不到了,偶知道24点的程序很多的说, 于是到网上搜了一下. 是有不少, 但是一个用c++的(其实根本就不能叫用c++,全是c的语法), 试了n多次,borlandc3.1和gcc都不能编译. 还找到了vb,和web版的,看来都没用. 在偶找东西搞得焦头烂额的时候,偶同学自己算出来了.(到底怎么算用这个程序试试吧)

为了以后不被这种问题困扰,花一个小时用python自己写了一个,还是python好~~~~~~~

精解

   1 funs = [ lambda x, item: (x+item[0],
   2                                str(x)+'+('+item[1]+')'
   3                               ),
   4       lambda x, item: (x-item[0],
   5                                str(x)+'-('+item[1]+')'
   6                               ),
   7       lambda x, item: (item[0]-x,
   8                                '('+item[1]+')-'+str(x)
   9                               ),
  10       lambda x, item: (x*item[0],
  11                                str(x)+'*('+item[1]+')'
  12                               ),
  13       lambda x, item:   (item[0]==0 and (0,'ZZZ')) or \
  14                         (x/item[0],
  15                                str(x)+'/('+item[1]+')'
  16                               ),
  17       lambda x, item:   (x==0 and (0,'ZZZ')) or \
  18                         (item[0]/x,
  19                                '('+item[1]+')/'+str(x)
  20                               )
  21 ]
  22 
  23 def con(num):
  24     l = len(num)
  25     p = list()
  26     if l==1: return {num[0]:str(num[0])}
  27     for i in range(l):
  28         for f in funs:
  29             p += map(lambda item: f(num[i],item),
  30                        con(num[:i]+num[i+1:]).items()
  31                     )
  32     return dict(p)
  33 
  34 print con(map(float,[1,5,6,7])).get(21.0,0)

Chaox

刚刚开始学python,试着写了一个24点的程序,遍历所有的解法,最多是24×64×5=7680种可能。有点丑陋而且重解很多,不过还是发上来让大家扔一下。。。python真的是很强大啊~~

   1 from __future__ import division
   2 from operator import add, sub, mul
   3 from time import *
   4 
   5 
   6 
   7 div = lambda x, y : x/y
   8 ops = [add, sub, mul, div]
   9 opsc = ['+','-','*','/']
  10 equation = []
  11 
  12 data = input('Pls input the four numbers:(separate them by comma)')
  13 
  14 xtime = time()
  15 
  16 #--------------------------------------------------------
  17 # list all the sort according to the four numbers, it is
  18 # copied from AllStartFromGame at http://python.cn
  19 #--------------------------------------------------------
  20 def permute(seq):
  21   l = len(seq)
  22   if l == 1:
  23     return [seq]
  24   else:
  25     res=[]
  26     for i in range(len(seq)):
  27       rest = seq[:i] + seq[i+1:]
  28       for x in permute(rest):
  29         res.append(seq[i:i+1] + x)
  30     return res
  31 
  32 listofall = permute(data)
  33 newlist = list(set(listofall))
  34 
  35 
  36 #--------------------------------------------------------
  37 # consider the five different structures of combinations
  38 #--------------------------------------------------------
  39 for a in newlist:
  40     for i in range(4):
  41         for j in range(4):
  42             for k in range(4):
  43                 express = []
  44                 value = (a[0],opsc[i],a[1],opsc[j],a[2],opsc[k],a[3])
  45                 express.append("((%d %s %d) %s %d) %s %d" % value)
  46                 express.append("(%d %s (%d %s %d)) %s %d" % value)
  47                 express.append("%d %s ((%d %s %d) %s %d)" % value)
  48                 express.append("%d %s (%d %s (%d %s %d))" % value)
  49                 express.append("(%d %s %d) %s (%d %s %d)" % value)
  50                 for s in express:
  51                     try:
  52                         x = eval(s)
  53                     except:
  54                         continue
  55                     if x < 24.00001 and x > 23.99999:
  56                         equation.append(s)                        
  57 
  58 ytime = time()
  59 cacltime = ytime - xtime
  60 
  61 equation = list(set(equation))
  62 for e in equation:
  63     print e
  64     
  65 print "there are %d" % len(equation), "items at all"
  66 print "all the time of calculation is %s s" % cacltime

实例

Chaox底部有个用pygame实现的24点游戏的实例

讨论

  • 24pointHowto -- 关于二十四点游戏的编程思路与基本算法

  • 网络社团--技术文档--算24点程序:原理,面向过程的C实现,面向对象的

  • 但是 Python 中的 map lambda filter 等等函式编程的支持,非常不习惯,怎么没有办法看明白!可以比较通俗的讲解一下子? 加些"猪屎吧!" -- Zoomq
  • 来个24pointAandQ吧.

Alex Dong

思路

上周在火车上无聊,就掏出纸和笔也在考虑这个算法,最后的结果是把这个问题变成:

给定任何n个的数,如何使用四则运算获得m这个结果。

我的大概思想是一个递归加不断规约的过程:

  • 1.对于n个数,从中间任意取出2个数。
  • 2.对这两个数进行计算,把结果存放回原来的数中,现在就有n-1个数。
    • 反复循环1,2两步操作,直到最终只剩下2个数。
    • 在递归的过程中需要记录两个东西:
    • 现有的计算和组合顺序。
    • 未进行的计算顺序,以便于下次穷举。

最近很忙,有时间的时候会尝试把这个算法实现出来。

monkeyXite

程序员 2002年的时候编程擂台里边出过的题和Alex Dong兄的扩展以后的问题一样的,基本算法一样的 最好在递归中作些优化吧…………写出来看看吧

PyProgramGames/24point (last edited 2009-12-25 07:18:56 by localhost)