python24点计算器_24点计算器Python脚本

使用Python脚本枚举24点答案,去除冗余括号,效率没有Ruby版本的好,可能因精度的关系,和其它人些的答案数目不一致,记录之。

# -*- coding:gbk -*-

import itertools as itt

import re

nums = [7, 6, 4, 8]

ops = ['+', '-', '*', '/']

levels = [0, 1, 2]

def simplest(exprstr):

patterns = [re.compile(r"\((\(.+)\)(.+)")\

,re.compile(r"\((.+\))\)(.+)")\

,re.compile(r"(.+)\((\(.+)\)")\

,re.compile(r"(.+)\((.+\))\)")\

,re.compile(r"(.+)\(([^\)]+)\)(.+)")\

,re.compile(r"\((.+)\)(.+\(.+\))")\

,re.compile(r"(\(.+\).+)\((.+)\)")]

rl = [exprstr,exprstr.replace("(","").replace(")","")]

for p in patterns:

m = p.match(exprstr)

if m is not None:

rl.append("".join(m.groups()))

rl.sort(key = len)

for r in rl:

if abs(eval(r) - 24.0) < 1e-10:

return r

def fmtexpr(operand, operators, priority):

i = [i for i, x in enumerate(priority) if x == max(priority)].pop(0)

operand[i] = "({}{}{})".format(operand[i],operators.pop(i),operand.pop(i + 1))

priority.pop(i)

return operand.pop(0) if len(priority) == 0 else fmtexpr(operand,operators,priority)

def check24p(num, op, level):

numfloat = [float(n) for n in num]

return fmtexpr(numfloat, op, level)

answer = set([])

ai = 0

for num in itt.permutations(nums, r = 4):

for op in itt.product(ops, repeat = 3):

for level in itt.permutations(levels, r = len(levels)):

exprstr = check24p(list(num), list(op), list(level))

try:

if abs(eval(exprstr) - 24.0) < 1e-10:

ai = ai + 1

exprstr = simplest(exprstr[1:-1])

answer.add(exprstr.replace(".0",""))

except ZeroDivisionError:

continue

print("共{}种解法,化简后剩余{}种解法:".format(ai, len(answer)))

for a in answer:

print("我答24点 " + a)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值