排列组合,取所有可能性

import random

def fun(str_inp):

# str_inp = input()
# str_inp = 'Q K A 2 3'

if 'joker' in str_inp or 'JOKER' in str_inp:
    return 'ERROR'

else:

    str_inp = str_inp.replace('J', '11')
    str_inp = str_inp.replace('Q', '12')
    str_inp = str_inp.replace('K', '13')
    str_inp = str_inp.replace('A', '1')

    flag_list = ['+', '-', '*', '/']
    data_list = [i for i in str_inp.split(' ')]
    print(data_list)

    from itertools import permutations

    res_list = list(permutations(data_list))
    # print(res_list)
    # print('元素不允许重复出现排列总数为:', len(res_list))

    for list_inp in res_list:
        i = list_inp[0]
        k = list_inp[1]
        m = list_inp[2]
        o = list_inp[-1]
        for j in flag_list:
            for l in flag_list:
                for n in flag_list:
                    test = i+j+k+l+m+n+o
                    # print(test)

                    temp = str(eval(i+j+k))

                    temp = str(eval(temp + l + m))
                    temp = eval(temp + n + o)
                    # print(temp)

                    if temp == 24:
                        return test
    else:
        return 'NONE'

list1 = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', 'joker', 'JOKER']
print(list1)
for i in range(10000):
list_temp = [random.choice(list1) for x in range(4)]
data_str = ' '.join(list_temp)
# print(data_str)
data_str = 'J J Q 4'

test = fun(data_str)
print(test)

https://blog.csdn.net/midion9/article/details/89000131
https://blog.csdn.net/qq_21578125/article/details/81111651?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2

你可能感兴趣的:(排列组合,取所有可能性)