24点

24点是一种益智游戏,任意给出4张扑克牌(不含大小王,J Q K A所代表的值分别为11, 12, 13, 1),可以有重复的数字。对这4张牌所代表的值进行加减乘除运算,不可改变数字的位置,但可使用括号改变运算顺序,最终使得表达式的值为24 。

在answer.py中实现下列函数:

def twenty_four_calc(filename="24_data.txt"):
    """
    :param filename: 文件名,当前文件夹下包含待计算的扑克牌符号的文件,
                     每行一组(用于评比的文件可能多达数百行),以空格分割,
                     具体格式如下:
                     A A 3 4
                     Q J K 2
                     10 9 5 A
                     ...
    :return : 此函数无返回结果,但需要将所有的解输出至当前文件夹下的
              "24_answer_<yourname>.txt" 中,将<yourname>替换为你的
              github用户名,文件格式如下:
              ===A A A A
              Impossible

              ===10 9 5 A
              ((10+9)+5)/1
              10+((9+5)/1)
              (10+(9+5))/1
              10+(9+(5/1))
              (10+9)+(5/1)
              ((10+9)+5)*1
              10+((9+5)*1)
              (10+(9+5))*1
              10+(9+(5*1))
              (10+9)+(5*1)

              ===A A 3 4
              ...(省略)
    """
    pass
题目地址
#windows
import string

def isTwentyFour(list):
    return 0

global resultCounter
resultCounter = 0

'''
((1 2) 3) 4
(1 2) (3 4)
1 (2 (3 4))
(1 (2 3)) 4
1 ((2 3) 4)
'''
def printStr(numList, opList, index):
    if index == 1:
        print "((", numList[0], opList[0], numList[1], ")", opList[1], numList[2], ")", opList[2], numList[3]
    elif index == 2:
        print "(", numList[0], opList[0], numList[1], ")", opList[1], "(", numList[2], opList[2], numList[3], ")"
    elif index == 3:
        print numList[0], opList[0], "(", numList[1], opList[1], "(", numList[2], opList[2], numList[3], "))"
    elif index == 4:
        print "(", numList[0], opList[0], "(", numList[1], opList[1], numList[2], "))", opList[2], numList[3]
    elif index == 5:
        print numList[0], opList[0], "((", numList[1], opList[1], numList[2], ")", opList[2], numList[3], ")"

def compute(a, b, op):
    if op == '+':
        return a+b
    elif op == '-':
        return a-b
    elif op == '*':
        return a*b
    elif op == '/' and b != 0:
        res = a/b
        if a == res * b:
            return res
        else:
            return 1000
    else:
        return 1000

'((1 2) 3) 4'
def compute1(numList, opList):
    global resultCounter
    res = compute(numList[0], numList[1], opList[0])
    res = compute(res, numList[2], opList[1])
    res = compute(res, numList[3], opList[2])
    if res == 24:
        resultCounter +=1
        printStr(numList, opList, 1)
    
'(1 2) (3 4)'
def compute2(numList, opList):
    global resultCounter
    res1 = compute(numList[0], numList[1], opList[0])
    res2 = compute(numList[2], numList[3], opList[2])
    res = compute(res1, res2, opList[1])
    if res == 24:
        resultCounter +=1
        printStr(numList, opList, 2)
'1 (2 (3 4))'
def compute3(numList, opList):
    global resultCounter
    res = compute(numList[2], numList[3], opList[2])
    res = compute(numList[1], res, opList[1])
    res = compute(numList[0], res, opList[0])
    if res == 24:
        resultCounter +=1
        printStr(numList, opList, 3)
'(1 (2 3)) 4'
def compute4(numList, opList):
    global resultCounter
    res = compute(numList[1], numList[2], opList[1])
    res = compute(numList[0], res, opList[0])
    res = compute(res, numList[3], opList[2])
    if res == 24:
        resultCounter +=1
        printStr(numList, opList, 4)
'1 ((2 3) 4)'
def compute5(numList, opList):
    global resultCounter
    res = compute(numList[1], numList[2], opList[1])
    res = compute(res, numList[3], opList[2])
    res = compute(numList[0], res, opList[0])
    if res == 24:
        resultCounter +=1
        printStr(numList, opList, 5)

def generatOperator(index):
    ops = ['+', '-', '*', '/']
    first = index/16
    second = (index%16)/4
    third = index%4
    opList = [ops[first], ops[second], ops[third]]
    return opList
        
def tryCal(list):
    for i in range(0, 64):
        compute1(list, generatOperator(i))
        compute2(list, generatOperator(i))
        compute3(list, generatOperator(i))
        compute4(list, generatOperator(i))
        compute5(list, generatOperator(i))
    

def swapList(first, end, list):
    list[first], list[end] = list[end], list[first]


def generatList(index, endflag, list):
    while 4 != index and 3 != endflag:
        swapList(endflag, index, list)
        if endflag == index - 1:
            index += 1
            endflag = 0
        else:
            endflag += 1
        tryCal(list)

def calculator(a, b, c, d):
    list = [a, b, c, d]
    generatList(1, 0, list)
    list = [b, c, d, a]
    generatList(1, 0, list)
    list = [c, d, a, b]
    generatList(1, 0, list)
    list = [d, a, b, c]
    generatList(1, 0, list)

def transfer(str):
    if str == 'J':
        return 11
    elif str == 'Q':
        return 12
    elif str == 'K':
        return 13
    elif str == 'A':
        return 1
    else:
        return  string.atoi(str, 10)


file_object = open('D:\\workspace\\twentyFour.txt')
try:
    while 1:
        line = file_object.readline()
        if not line:
            break
        list = line.strip().split(' ')
        a = transfer(list[0])
        b = transfer(list[1])
        c = transfer(list[2])
        d = transfer(list[3])
        resultCounter = 0
        calculator(a, b, c, d)
        if 0 == resultCounter:
            print a, b, c, d, "\r\nimpossible"
        
finally:
    file_object.close()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值