from itertools import permutations
def fun353(x):
for cards in permutations(x):
for i in ['+','-','*','/']:
for j in ['+','-','*','/']:
for n in ['+','-','*','/']:
if eval('%s%s%s%s%s%s%s'%(cards[0],i,cards[1],j,cards[2],n,cards[3])) == 24:
return True
cards_1 = cards[0]
cards_2 = cards[1:]
for i in ['+','-']:
for j in ['+','-']:
sum_cards_2 = eval('%s%s%s%s%s'%(cards_2[0],i,cards_2[1],j,cards_2[2]))
try:
if cards_1*sum_cards_2==24 or cards_1/sum_cards_2==24:
return True
except ZeroDivisionError:
continue
cards_2 = cards[3]
cards_1 = cards[:3]
for i in ['+','-']:
for j in ['+','-']:
sum_cards_1 = eval('%s%s%s%s%s'%(cards_1[0],i,cards_1[1],j,cards_1[2]))
try:
if sum_cards_1*cards_2==24 or sum_cards_1/cards_2==24:
return True
except ZeroDivisionError:
continue
cards_1 = cards[:2]
cards_2 = cards[2:]
for i in ['+','-']:
for j in ['+','-']:
sum_cards_1 = eval('%s%s%s'%(cards_1[0],i,cards_1[1]))
sum_cards_2 = eval('%s%s%s'%(cards_2[0],i,cards_2[1]))
try:
if sum_cards_1*sum_cards_2==24 or sum_cards_1*sum_cards_2==24:
return True
except ZeroDivisionError:
continue
return False
|