IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python之24点牌游戏 -> 正文阅读

[Python知识库]python之24点牌游戏

# -*- coding: UTF-8 -*-
from itertools import combinations
from itertools import permutations

str1 = "1 2 J A"
card = str1.split(" ")
re_ref = ['J', 'Q', 'K', 'A']
i_ref = [11, 12, 13, 1]

# 运算符
def getYS():
    result = []
    list1 = ['+', '-', '*', '/'] * 3
    for p in combinations(list1, 3):
        result.append(p)
    return list(set(result))
# print(len(getYS()))

# 获取牌顺序
def getCard(card):
    result = []
    for p in permutations(card, 4):
        list1 = []
        list1.append(p[0])
        list1.append(p[1])
        list1.append(p[2])
        list1.append(p[3])
        result.append(list1)
    return result

# print(len(getCard(card)))

# 获取表达式
def getExpress1(card):
    result = []
    for i in getYS():
        tmp = card[:]
        tmp.insert(1, i[0])
        tmp.insert(3, i[1])
        tmp.insert(5, i[2])
        result.append(tmp)
    return result

# print(getExpress(card))

#添加括号
def getExpress2(list1):
    #只有一个*或者/
    if list1.count("*") + list1.count("/") == 1:
        if list1[3] == "*" or list1[3] == "/":
            list1.insert(0, "(")
            list1.insert(4, ")")
        elif list1[5] == "*" or list1[5] == "/":
            list1.insert(0, "(")
            list1.insert(6, ")")
    #有两个*或者/
    elif list1.count("*") + list1.count("/") == 2:
        if (list1[1] == "*" or list1[1] == "/") and (list1[5] == "*" or list1[5] == "/"):
            list1.insert(0, "(")
            list1.insert(6, ")")
        elif (list1[1] == "*" or list1[1] == "/") and (list1[5] == "*" or list1[5] == "/"):
            list1.insert(0, "(")
            list1.insert(4, ")")
    return list1

#生成字符串表达式
def getExpress3(card, res_result):
    result = getExpress1(card)
    for i in result:
        list1 = getExpress2(i)
        str1 = ''
        for j in list1:
            str1 = str1 + str(j)
        if eval(str1) == 24:
            str2 = str1.replace('.', '').replace('0', '').replace('(', '').replace(')', '')
            res_result.append(str2.replace('11', 'J').replace('12', 'Q').replace('13', 'K').replace('1', 'A'))

res_result = []
if "joker" in card or "JOKER" in card:
    print('ERROR')
else:
    for i in card:
        if i in re_ref:
            card[card.index(i)] = float(i_ref[re_ref.index(i)])
        else:
            card[card.index(i)] = float(i)
    print(card)
    for j in getCard(card):
        getExpress3(j, res_result)
    if len(res_result) == 0:
        print('NONE')
    else:
        #print(list(set(res_result)))
        print(list(set(res_result))[0])
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-06-25 18:04:45  更:2022-06-25 18:06:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 12:25:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码