24点牌 递归算法

# -*- coding: utf-8 -*-
import sys

class My24(float):

def __new__(cls, num, trace='', last_opt=''):
obj = float.__new__(cls, num)
return obj

def __init__(self, num=0, trace='', last_opt=''):
self.val = num
self.trace = trace if trace else str(num)
self.last_opt = last_opt
super(float, self).__init__(num)

def __hash__(self):
return hash(self.trace)

def adjust_trace(self, opt):
if opt in ['*', '/'] and self.last_opt in ['+', '-', '/']:
return '(' + self.trace + ')'
return self.trace

def mk_trace(self, other, opt):
return "{} {} {}".format(self.adjust_trace(opt), opt, other.adjust_trace(opt))

def show_trace(self):
print(self.trace)

def __add__(self, other):
trace = self.mk_trace(other, '+')
last_opt = '+'
num = self.val + other.val
return My24(num, trace, last_opt)

def __sub__(self, other):
trace = self.mk_trace(other, '-')
last_opt = '-'
num = self.val - other.val
return My24(num, trace, last_opt)

def __div__(self, other):
trace = self.mk_trace(other, '/')
last_opt = '/'
num = self.val * 1.0 / other.val
return My24(num, trace, last_opt)

def __mul__(self, other):
trace = self.mk_trace(other, '*')
last_opt = '*'
num = self.val * other.val
return My24(num, trace, last_opt)


def possibel_val(a, b):
""" 不要负的和小数"""
if a > b:
a, b = b, a
vals = [a + b, a * b]
vals.append(b - a)
if a != 0:
vals.append(b / a)
if b != 0:
vals.append(a / b)
return vals

def possibel_val_by_list(list1, list2):
return_list = []
[return_list.extend(possibel_val(a, b)) for a in set(list1) for b in set(list2) ]
return return_list

def comput24(*numbs):
length = len(numbs)
if length == 1:
return [numbs[0]]
if length == 2:
return possibel_val(*numbs)
elif length == 3:
possibel_1 = possibel_val_by_list(numbs[:1], comput24(*numbs[1:]))
possibel_2 = possibel_val_by_list(comput24(*numbs[:2]), numbs[2:])
possibel_3 = possibel_val_by_list([numbs[1]], comput24(numbs[0], numbs[2]))
return possibel_1 + possibel_2 + possibel_3
elif length == 4:
possibel_1 = possibel_val_by_list([numbs[0]], comput24(numbs[1], numbs[2], numbs[3]))
possibel_2 = possibel_val_by_list([numbs[1]], comput24(numbs[0], numbs[2], numbs[3]))
possibel_3 = possibel_val_by_list([numbs[2]], comput24(numbs[0], numbs[1], numbs[3]))
possibel_4 = possibel_val_by_list([numbs[3]], comput24(numbs[1], numbs[2], numbs[0]))
possibel_5 = possibel_val_by_list(comput24(*numbs[:2]), comput24(*numbs[2:]))
possibel_6 = possibel_val_by_list(comput24(numbs[0], numbs[2]), comput24(numbs[1], numbs[3]))
possibel_7 = possibel_val_by_list(comput24(numbs[0], numbs[3]), comput24(numbs[1], numbs[2]))
return possibel_1 + possibel_2 + possibel_3 + possibel_4 + possibel_5 + possibel_6 + possibel_7

def print_val_trace(possibels):
if not isinstance(possibels, (list, tuple, set)):
print possibels.val, '<=', possibels.trace
return
for item in possibels:
print item.val, '<=', item.trace

def run(target_val=24, *vals):
result = comput24(*map(My24, vals))
result = sorted(list(set(result)))
print(result)
if target_val not in result:
print "result not find"
else:
index = result.index(target_val)
while result[index] == target_val:
print_val_trace(result[index])
index += 1

if __name__ == '__main__':
if len(sys.argv) > 1:
print sys.argv
run(*sys.argv[1:])
else:
run(24, 3, 3, 7, 7)

原文地址:https://www.cnblogs.com/chens-smile/p/7667338.html