python 穷举法 算24点(史上最简短代码)

简介: 本来想用回溯法实现 算24点。题目都拟好了,就是《python 回溯法 子集树模板 系列 —— 7、24点》。无奈想了一天,没有头绪。只好改用暴力穷举法。思路说明根据四个数,三个运算符,构造三种中缀表达式,遍历,计算每一种可能显然可能的形式不止三种。

本来想用回溯法实现 算24点。题目都拟好了,就是《python 回溯法 子集树模板 系列 —— 7、24点》。无奈想了一天,没有头绪。只好改用暴力穷举法。

思路说明

根据四个数,三个运算符,构造三种中缀表达式,遍历,计算每一种可能

显然可能的形式不止三种。但是,其它的形式要么得不到24点,要么在加、乘意义下可以转化为这三种形式的表达式!

使用内置的eval函数计算中缀表达式,使得代码变得非常简洁!

完整代码

# 作者:hhh5460
# 时间:2017年6月3日

import itertools

def twentyfour(cards):
    '''史上最短计算24点代码'''
    for nums in itertools.permutations(cards): # 四个数
        for ops in itertools.product('+-*/', repeat=3): # 三个运算符(可重复!)
            # 构造三种中缀表达式 (bsd)
            bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops)  # (a+b)*(c-d)
            bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops)  # (a+b)*c-d
            bds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops)  #  a/(b-(c/d))
            
            for bds in [bds1, bds2, bds3]: # 遍历
                try:
                    if abs(eval(bds) - 24.0) < 1e-10:   # eval函数
                        return bds
                except ZeroDivisionError: # 零除错误!
                    continue
    
    return 'Not found!'


# 测试
# 数据来源:http://www.cnblogs.com/grenet/archive/2013/02/28/2936965.html
cards =[[1,1,1,8],
        [1,1,2,6],
        [1,1,2,7],
        [1,1,2,8],
        [1,1,2,9],
        [1,1,2,10],
        [1,1,3,4],
        [1,1,3,5],
        [1,1,3,6],
        [1,1,3,7],
        [1,1,3,8],
        [1,1,3,9],
        [1,1,3,10],
        [1,1,4,4],
        [1,1,4,5],
        [1,1,4,6],
        [1,1,4,7],
        [1,1,4,8],
        [1,1,4,9],
        [1,1,4,10],
        [1,1,5,5],
        [1,1,5,6],
        [1,1,5,7],
        [1,1,5,8],
        [1,1,6,6],
        [1,1,6,8],
        [1,1,6,9],
        [1,1,7,10],
        [1,1,8,8],
        [1,2,2,4],
        [1,2,2,5],
        [1,2,2,6],
        [1,2,2,7],
        [1,2,2,8],
        [1,2,2,9],
        [1,2,2,10],
        [1,2,3,3],
        [1,2,3,4],
        [1,2,3,5],
        [1,2,3,6],
        [1,2,3,7],
        [1,2,3,8],
        [1,2,3,9],
        [1,2,3,10],
        [1,2,4,4],
        [1,2,4,5],
        [1,2,4,6],
        [1,2,4,7],
        [1,2,4,8],
        [1,2,4,9],
        [1,2,4,10],
        [1,2,5,5],
        [1,2,5,6],
        [1,2,5,7],
        [1,2,5,8],
        [1,2,5,9],
        [1,2,5,10],
        [1,2,6,6],
        [1,2,6,7],
        [1,2,6,8],
        [1,2,6,9],
        [1,2,6,10],
        [1,2,7,7],
        [1,2,7,8],
        [1,2,7,9],
        [1,2,7,10],
        [1,2,8,8],
        [1,2,8,9],
        [1,2,8,10],
        [1,3,3,3],
        [1,3,3,4],
        [1,3,3,5],
        [1,3,3,6],
        [1,3,3,7],
        [1,3,3,8],
        [1,3,3,9],
        [1,3,3,10],
        [1,3,4,4],
        [1,3,4,5],
        [1,3,4,6],
        [1,3,4,7],
        [1,3,4,8],
        [1,3,4,9],
        [1,3,4,10],
        [1,3,5,6],
        [1,3,5,7],
        [1,3,5,8],
        [1,3,5,9],
        [1,3,5,10],
        [1,3,6,6],
        [1,3,6,7],
        [1,3,6,8],
        [1,3,6,9],
        [1,3,6,10],
        [1,3,7,7],
        [1,3,7,8],
        [1,3,7,9],
        [1,3,7,10],
        [1,3,8,8],
        [1,3,8,9],
        [1,3,8,10],
        [1,3,9,9],
        [1,3,9,10],
        [1,3,10,10],
        [1,4,4,4],
        [1,4,4,5],
        [1,4,4,6],
        [1,4,4,7],
        [1,4,4,8],
        [1,4,4,9],
        [1,4,4,10],
        [1,4,5,5],
        [1,4,5,6],
        [1,4,5,7],
        [1,4,5,8],
        [1,4,5,9],
        [1,4,5,10],
        [1,4,6,6],
        [1,4,6,7],
        [1,4,6,8],
        [1,4,6,9],
        [1,4,6,10],
        [1,4,7,7],
        [1,4,7,8],
        [1,4,7,9],
        [1,4,8,8],
        [1,4,8,9],
        [1,4,9,10],
        [1,4,10,10],
        [1,5,5,5],
        [1,5,5,6],
        [1,5,5,9],
        [1,5,5,10],
        [1,5,6,6],
        [1,5,6,7],
        [1,5,6,8],
        [1,5,6,9],
        [1,5,6,10],
        [1,5,7,8],
        [1,5,7,9],
        [1,5,7,10],
        [1,5,8,8],
        [1,5,8,9],
        [1,5,8,10],
        [1,5,9,9],
        [1,5,9,10],
        [1,5,10,10],
        [1,6,6,6],
        [1,6,6,8],
        [1,6,6,9],
        [1,6,6,10],
        [1,6,7,9],
        [1,6,7,10],
        [1,6,8,8],
        [1,6,8,9],
        [1,6,8,10],
        [1,6,9,9],
        [1,6,9,10],
        [1,7,7,9],
        [1,7,7,10],
        [1,7,8,8],
        [1,7,8,9],
        [1,7,8,10],
        [1,7,9,9],
        [1,7,9,10],
        [1,8,8,8],
        [1,8,8,9],
        [1,8,8,10],
        [2,2,2,3],
        [2,2,2,4],
        [2,2,2,5],
        [2,2,2,7],
        [2,2,2,8],
        [2,2,2,9],
        [2,2,2,10],
        [2,2,3,3],
        [2,2,3,4],
        [2,2,3,5],
        [2,2,3,6],
        [2,2,3,7],
        [2,2,3,8],
        [2,2,3,9],
        [2,2,3,10],
        [2,2,4,4],
        [2,2,4,5],
        [2,2,4,6],
        [2,2,4,7],
        [2,2,4,8],
        [2,2,4,9],
        [2,2,4,10],
        [2,2,5,5],
        [2,2,5,6],
        [2,2,5,7],
        [2,2,5,8],
        [2,2,5,9],
        [2,2,5,10],
        [2,2,6,6],
        [2,2,6,7],
        [2,2,6,8],
        [2,2,6,9],
        [2,2,6,10],
        [2,2,7,7],
        [2,2,7,8],
        [2,2,7,10],
        [2,2,8,8],
        [2,2,8,9],
        [2,2,8,10],
        [2,2,9,10],
        [2,2,10,10],
        [2,3,3,3],
        [2,3,3,5],
        [2,3,3,6],
        [2,3,3,7],
        [2,3,3,8],
        [2,3,3,9],
        [2,3,3,10],
        [2,3,4,4],
        [2,3,4,5],
        [2,3,4,6],
        [2,3,4,7],
        [2,3,4,8],
        [2,3,4,9],
        [2,3,4,10],
        [2,3,5,5],
        [2,3,5,6],
        [2,3,5,7],
        [2,3,5,8],
        [2,3,5,9],
        [2,3,5,10],
        [2,3,6,6],
        [2,3,6,7],
        [2,3,6,8],
        [2,3,6,9],
        [2,3,6,10],
        [2,3,7,7],
        [2,3,7,8],
        [2,3,7,9],
        [2,3,7,10],
        [2,3,8,8],
        [2,3,8,9],
        [2,3,8,10],
        [2,3,9,9],
        [2,3,9,10],
        [2,3,10,10],
        [2,4,4,4],
        [2,4,4,5],
        [2,4,4,6],
        [2,4,4,7],
        [2,4,4,8],
        [2,4,4,9],
        [2,4,4,10],
        [2,4,5,5],
        [2,4,5,6],
        [2,4,5,7],
        [2,4,5,8],
        [2,4,5,9],
        [2,4,5,10],
        [2,4,6,6],
        [2,4,6,7],
        [2,4,6,8],
        [2,4,6,9],
        [2,4,6,10],
        [2,4,7,7],
        [2,4,7,8],
        [2,4,7,9],
        [2,4,7,10],
        [2,4,8,8],
        [2,4,8,9],
        [2,4,8,10],
        [2,4,9,9],
        [2,4,9,10],
        [2,4,10,10],
        [2,5,5,7],
        [2,5,5,8],
        [2,5,5,9],
        [2,5,5,10],
        [2,5,6,6],
        [2,5,6,7],
        [2,5,6,8],
        [2,5,6,9],
        [2,5,6,10],
        [2,5,7,7],
        [2,5,7,8],
        [2,5,7,9],
        [2,5,7,10],
        [2,5,8,8],
        [2,5,8,9],
        [2,5,8,10],
        [2,5,9,10],
        [2,5,10,10],
        [2,6,6,6],
        [2,6,6,7],
        [2,6,6,8],
        [2,6,6,9],
        [2,6,6,10],
        [2,6,7,8],
        [2,6,7,9],
        [2,6,7,10],
        [2,6,8,8],
        [2,6,8,9],
        [2,6,8,10],
        [2,6,9,9],
        [2,6,9,10],
        [2,6,10,10],
        [2,7,7,8],
        [2,7,7,10],
        [2,7,8,8],
        [2,7,8,9],
        [2,7,9,10],
        [2,7,10,10],
        [2,8,8,8],
        [2,8,8,9],
        [2,8,8,10],
        [2,8,9,9],
        [2,8,9,10],
        [2,8,10,10],
        [2,9,10,10],
        [3,3,3,3],
        [3,3,3,4],
        [3,3,3,5],
        [3,3,3,6],
        [3,3,3,7],
        [3,3,3,8],
        [3,3,3,9],
        [3,3,3,10],
        [3,3,4,4],
        [3,3,4,5],
        [3,3,4,6],
        [3,3,4,7],
        [3,3,4,8],
        [3,3,4,9],
        [3,3,5,5],
        [3,3,5,6],
        [3,3,5,7],
        [3,3,5,9],
        [3,3,5,10],
        [3,3,6,6],
        [3,3,6,7],
        [3,3,6,8],
        [3,3,6,9],
        [3,3,6,10],
        [3,3,7,7],
        [3,3,7,8],
        [3,3,7,9],
        [3,3,8,8],
        [3,3,8,9],
        [3,3,8,10],
        [3,3,9,9],
        [3,3,9,10],
        [3,4,4,4],
        [3,4,4,5],
        [3,4,4,6],
        [3,4,4,7],
        [3,4,4,8],
        [3,4,4,9],
        [3,4,4,10],
        [3,4,5,5],
        [3,4,5,6],
        [3,4,5,7],
        [3,4,5,8],
        [3,4,5,9],
        [3,4,5,10],
        [3,4,6,6],
        [3,4,6,8],
        [3,4,6,9],
        [3,4,6,10],
        [3,4,7,7],
        [3,4,7,8],
        [3,4,7,9],
        [3,4,7,10],
        [3,4,8,9],
        [3,4,8,10],
        [3,4,9,9],
        [3,4,10,10],
        [3,5,5,6],
        [3,5,5,7],
        [3,5,5,8],
        [3,5,5,9],
        [3,5,6,6],
        [3,5,6,7],
        [3,5,6,8],
        [3,5,6,9],
        [3,5,6,10],
        [3,5,7,8],
        [3,5,7,9],
        [3,5,7,10],
        [3,5,8,8],
        [3,5,8,9],
        [3,5,9,9],
        [3,5,9,10],
        [3,5,10,10],
        [3,6,6,6],
        [3,6,6,7],
        [3,6,6,8],
        [3,6,6,9],
        [3,6,6,10],
        [3,6,7,7],
        [3,6,7,8],
        [3,6,7,9],
        [3,6,7,10],
        [3,6,8,8],
        [3,6,8,9],
        [3,6,8,10],
        [3,6,9,9],
        [3,6,9,10],
        [3,6,10,10],
        [3,7,7,7],
        [3,7,7,8],
        [3,7,7,9],
        [3,7,7,10],
        [3,7,8,8],
        [3,7,8,9],
        [3,7,9,9],
        [3,7,9,10],
        [3,7,10,10],
        [3,8,8,8],
        [3,8,8,9],
        [3,8,8,10],
        [3,8,9,9],
        [3,8,9,10],
        [3,8,10,10],
        [3,9,9,9],
        [3,9,9,10],
        [3,9,10,10],
        [4,4,4,4],
        [4,4,4,5],
        [4,4,4,6],
        [4,4,4,7],
        [4,4,4,8],
        [4,4,4,9],
        [4,4,4,10],
        [4,4,5,5],
        [4,4,5,6],
        [4,4,5,7],
        [4,4,5,8],
        [4,4,5,10],
        [4,4,6,8],
        [4,4,6,9],
        [4,4,6,10],
        [4,4,7,7],
        [4,4,7,8],
        [4,4,7,9],
        [4,4,7,10],
        [4,4,8,8],
        [4,4,8,9],
        [4,4,8,10],
        [4,4,10,10],
        [4,5,5,5],
        [4,5,5,6],
        [4,5,5,7],
        [4,5,5,8],
        [4,5,5,9],
        [4,5,5,10],
        [4,5,6,6],
        [4,5,6,7],
        [4,5,6,8],
        [4,5,6,9],
        [4,5,6,10],
        [4,5,7,7],
        [4,5,7,8],
        [4,5,7,9],
        [4,5,7,10],
        [4,5,8,8],
        [4,5,8,9],
        [4,5,8,10],
        [4,5,9,9],
        [4,5,9,10],
        [4,5,10,10],
        [4,6,6,6],
        [4,6,6,7],
        [4,6,6,8],
        [4,6,6,9],
        [4,6,6,10],
        [4,6,7,7],
        [4,6,7,8],
        [4,6,7,9],
        [4,6,7,10],
        [4,6,8,8],
        [4,6,8,9],
        [4,6,8,10],
        [4,6,9,9],
        [4,6,9,10],
        [4,6,10,10],
        [4,7,7,7],
        [4,7,7,8],
        [4,7,8,8],
        [4,7,8,9],
        [4,7,8,10],
        [4,7,9,9],
        [4,7,9,10],
        [4,7,10,10],
        [4,8,8,8],
        [4,8,8,9],
        [4,8,8,10],
        [4,8,9,9],
        [4,8,9,10],
        [4,8,10,10],
        [4,9,9,10],
        [5,5,5,5],
        [5,5,5,6],
        [5,5,5,9],
        [5,5,6,6],
        [5,5,6,7],
        [5,5,6,8],
        [5,5,7,7],
        [5,5,7,8],
        [5,5,7,10],
        [5,5,8,8],
        [5,5,8,9],
        [5,5,8,10],
        [5,5,9,9],
        [5,5,9,10],
        [5,5,10,10],
        [5,6,6,6],
        [5,6,6,7],
        [5,6,6,8],
        [5,6,6,9],
        [5,6,6,10],
        [5,6,7,7],
        [5,6,7,8],
        [5,6,7,9],
        [5,6,8,8],
        [5,6,8,9],
        [5,6,8,10],
        [5,6,9,9],
        [5,6,9,10],
        [5,6,10,10],
        [5,7,7,9],
        [5,7,7,10],
        [5,7,8,8],
        [5,7,8,9],
        [5,7,8,10],
        [5,7,9,10],
        [5,7,10,10],
        [5,8,8,8],
        [5,8,8,9],
        [5,8,8,10],
        [5,9,10,10],
        [6,6,6,6],
        [6,6,6,8],
        [6,6,6,9],
        [6,6,6,10],
        [6,6,7,9],
        [6,6,7,10],
        [6,6,8,8],
        [6,6,8,9],
        [6,6,8,10],
        [6,6,9,10],
        [6,7,7,10],
        [6,7,8,9],
        [6,7,8,10],
        [6,7,9,9],
        [6,7,10,10],
        [6,8,8,8],
        [6,8,8,9],
        [6,8,8,10],
        [6,8,9,9],
        [6,8,9,10],
        [6,9,9,10],
        [6,10,10,10],
        [7,7,9,10],
        [7,8,8,9],
        [7,8,8,10],
        [7,8,9,10],
        [7,8,10,10],
        [8,8,8,10]]

for card in cards:
    print(twentyfour(card))

以上数据全都pass,图我就不截了

目录
相关文章
|
11天前
|
存储 数据挖掘 开发者
Python编程入门:从零到英雄
在这篇文章中,我们将一起踏上Python编程的奇幻之旅。无论你是编程新手,还是希望拓展技能的开发者,本教程都将为你提供一条清晰的道路,引导你从基础语法走向实际应用。通过精心设计的代码示例和练习,你将学会如何用Python解决实际问题,并准备好迎接更复杂的编程挑战。让我们一起探索这个强大的语言,开启你的编程生涯吧!
|
17天前
|
机器学习/深度学习 人工智能 TensorFlow
人工智能浪潮下的自我修养:从Python编程入门到深度学习实践
【10月更文挑战第39天】本文旨在为初学者提供一条清晰的道路,从Python基础语法的掌握到深度学习领域的探索。我们将通过简明扼要的语言和实际代码示例,引导读者逐步构建起对人工智能技术的理解和应用能力。文章不仅涵盖Python编程的基础,还将深入探讨深度学习的核心概念、工具和实战技巧,帮助读者在AI的浪潮中找到自己的位置。
|
17天前
|
机器学习/深度学习 数据挖掘 Python
Python编程入门——从零开始构建你的第一个程序
【10月更文挑战第39天】本文将带你走进Python的世界,通过简单易懂的语言和实际的代码示例,让你快速掌握Python的基础语法。无论你是编程新手还是想学习新语言的老手,这篇文章都能为你提供有价值的信息。我们将从变量、数据类型、控制结构等基本概念入手,逐步过渡到函数、模块等高级特性,最后通过一个综合示例来巩固所学知识。让我们一起开启Python编程之旅吧!
|
17天前
|
存储 Python
Python编程入门:打造你的第一个程序
【10月更文挑战第39天】在数字时代的浪潮中,掌握编程技能如同掌握了一门新时代的语言。本文将引导你步入Python编程的奇妙世界,从零基础出发,一步步构建你的第一个程序。我们将探索编程的基本概念,通过简单示例理解变量、数据类型和控制结构,最终实现一个简单的猜数字游戏。这不仅是一段代码的旅程,更是逻辑思维和问题解决能力的锻炼之旅。准备好了吗?让我们开始吧!
|
4天前
|
Python
Python编程入门:从零开始的代码旅程
本文是一篇针对Python编程初学者的入门指南,将介绍Python的基本语法、数据类型、控制结构以及函数等概念。文章旨在帮助读者快速掌握Python编程的基础知识,并能够编写简单的Python程序。通过本文的学习,读者将能够理解Python代码的基本结构和逻辑,为进一步深入学习打下坚实的基础。
|
8天前
|
数据采集 存储 数据处理
Python中的多线程编程及其在数据处理中的应用
本文深入探讨了Python中多线程编程的概念、原理和实现方法,并详细介绍了其在数据处理领域的应用。通过对比单线程与多线程的性能差异,展示了多线程编程在提升程序运行效率方面的显著优势。文章还提供了实际案例,帮助读者更好地理解和掌握多线程编程技术。
|
11天前
|
存储 人工智能 数据挖掘
Python编程入门:打造你的第一个程序
本文旨在为初学者提供Python编程的初步指导,通过介绍Python语言的基础概念、开发环境的搭建以及一个简单的代码示例,帮助读者快速入门。文章将引导你理解编程思维,学会如何编写、运行和调试Python代码,从而开启编程之旅。
34 2
|
12天前
|
存储 Python
Python编程入门:理解基础语法与编写简单程序
本文旨在为初学者提供一个关于如何开始使用Python编程语言的指南。我们将从安装Python环境开始,逐步介绍变量、数据类型、控制结构、函数和模块等基本概念。通过实例演示和练习,读者将学会如何编写简单的Python程序,并了解如何解决常见的编程问题。文章最后将提供一些资源,以供进一步学习和实践。
25 1
|
19天前
|
设计模式 算法 搜索推荐
Python编程中的设计模式:优雅解决复杂问题的钥匙####
本文将探讨Python编程中几种核心设计模式的应用实例与优势,不涉及具体代码示例,而是聚焦于每种模式背后的设计理念、适用场景及其如何促进代码的可维护性和扩展性。通过理解这些设计模式,开发者可以更加高效地构建软件系统,实现代码复用,提升项目质量。 ####
|
14天前
|
存储 网络协议 IDE
从零起步学习Python编程
从零起步学习Python编程