Python实现24点,包含对于A,1与14的处理
Python实现24点,简易版本,数字[1,14],不含真实场景
'''
@author:ccc-ju
@software:PyCharm
@time: 19:11
'''
import random
from itertools import product
from itertools import permutations
cards = [random.randint(1, 14) for _ in range(4)]
def op_with_number(cards):
operators = ['+', '-', '*', '/']
car1, car2, car3, car4 = cards
result = []
for p in product(operators, repeat=len(cards) - 1):
op1, op2 ,op3 = p
result.append('{} {} {} {} {} {} {}'.format(car1, op1, car2, op2, car3, op3, car4))
return result
def op_with_number_position_exchange(cards):
result = []
for p in permutations(cards):
result += op_with_number(p)
return result
def add_brace(cards):
if len(cards) < 2:
return [cards]
if len(cards) == 2:
return [['(' + str(cards[0])] + [str(cards[1]) + ')']]
result = []
for i in range(1, len(cards)):
prefixs = add_brace(cards[:i])
tails = add_brace(cards[i:])
for p, t in product(prefixs, tails):
with_brace_around = ['(' + p[0]] + p[1:] + t[:-1] + [t[-1] + ')']
result.append(with_brace_around)
return result
def join_op(operators, with_brace):
result = with_brace[0]
for i, op in enumerate(operators):
result += op + ' ' + with_brace[i + 1]
return result
def join_op_with_numbers(expression):
numbers = expression.split()[::2]
operators = expression.split()[1::2]
with_brace = add_brace(numbers)
result = []
for brace in with_brace:
result.append(join_op(operators, brace))
return result
def solve_problem_24_points(cards):
TARGET = 24
for exp in op_with_number_position_exchange(cards):
for i in join_op_with_numbers(exp):
try:
if eval(i) == TARGET:
print(i)
except ZeroDivisionError:
continue
print('我抽到的牌是:\n', cards, '\n有以下的组合形式:')
solve_problem_24_points(cards)
Python实现24点,包含对于A,1与14的处理
'''
@author:ccc-ju
@software:PyCharm
@time: 20:30
编程三大概念
PKU:
1.迭代模拟
2.递归
3.动态规划
'''
import random
from itertools import product
from itertools import permutations
def random_real_card():
cards = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
color = '红桃 黑桃 方片 梅花'.split()
return random.choice(color) + random.choice(cards)
cards = [random_real_card() for _ in range(4)]
def op_collection_cards(cards):
list_operators = ['+', '-', '*', '/']
card1, card2, card3, card4 = cards
expression = []
for p in product(list_operators, repeat=len(cards) - 1):
op1, op2, op3 = p
expression.append('{} {} {} {} {} {} {}'.format(card1, op1, card2, op2, card3, op3, card4))
return expression
def op_collection_cards_xchange(cards):
result = []
for p in permutations(cards):
result += op_collection_cards(p)
return result
def add_brace(cards):
if len(cards) == 1:
return [cards]
if len(cards) == 2:
return [['(' + str(cards[0])] + [str(cards[1]) + ')']]
results = []
for i in range(1, len(cards)):
prefixs = add_brace(cards[:i])
tails = add_brace(cards[i:])
for p, t in product(prefixs, tails):
with_brace_around = ['(' + p[0]] + p[1:] + t[:-1] + [t[-1] + ')']
results.append(with_brace_around)
return results
def brace_with_operators(with_brace,operators):
finally_exp = with_brace[0]
for i, op in enumerate(operators):
finally_exp += (op + ' ' + with_brace[i+1])
return finally_exp
def join_brace_to_expression(expression):
numbers = expression.split()[::2]
operators = expression.split()[1::2]
with_brace = add_brace(numbers)
op_and_numbers = []
for brace in with_brace:
op_and_numbers.append(brace_with_operators(brace, operators))
return op_and_numbers
def card_change_to_number_card(cards, num):
str = ''
for i in range(len(cards)):
str += ' ' + cards[i][2:]
cards_two = str.split(' ')[1:]
size = len(cards_two)
card_result1 = list()
if num == 0:
print('进入的是第一种')
for i in range(size):
if cards_two[i] == 'A':
card_result1.append(1)
elif cards_two[i] == 'J':
card_result1.append(11)
elif cards_two[i] == 'Q':
card_result1.append(12)
elif cards_two[i] == 'K':
card_result1.append(13)
else:
card_result1.append(int(cards_two[i]))
elif num == 1:
print('进入的是第二种')
for i in range(size):
if cards_two[i] == 'A':
card_result1.append(14)
elif cards_two[i] == 'J':
card_result1.append(11)
elif cards_two[i] == 'Q':
card_result1.append(12)
elif cards_two[i] == 'K':
card_result1.append(13)
else:
card_result1.append(int(cards_two[i]))
else:
print('进入的是第三种')
for i in range(size):
if cards_two[i] == 'J':
card_result1.append(11)
elif cards_two[i] == 'Q':
card_result1.append(12)
elif cards_two[i] == 'K':
card_result1.append(13)
else:
card_result1.append(int(cards_two[i]))
return card_result1
def finally_result(cards):
str = ''
for i in range(len(cards)):
str += ' ' + cards[i][2:]
cards_two = str.split(' ')[1:]
TARGET = 24
count = 0
for i in range(len(cards_two)):
if cards_two[i] == 'A':
print('走的第一种含A的情况且A为1')
cards2 = card_change_to_number_card(cards, 0)
for exp in op_collection_cards_xchange(cards2):
for b in join_brace_to_expression(exp):
try:
if eval(b) == TARGET:
count += 1
print(b)
except ZeroDivisionError:
continue
for i in range(len(cards_two)):
if cards_two[i] == 'A' and count == 0:
print('走的第二种含A的情况且A为14')
cards2 = card_change_to_number_card(cards, 1)
for exp in op_collection_cards_xchange(cards2):
for b in join_brace_to_expression(exp):
try:
if eval(b) == TARGET:
count += 1
print(b)
except ZeroDivisionError:
continue
for i in range(len(cards_two)):
if cards_two[i] != 'A' and count == 0:
print('走的第三种不含A的情况')
cards2 = card_change_to_number_card(cards, -1)
for exp in op_collection_cards_xchange(cards2):
for b in join_brace_to_expression(exp):
try:
if eval(b) == TARGET:
count += 1
print(b)
except ZeroDivisionError:
continue
print('我抽到的牌是:\n', cards)
finally_result(cards)