标签:index __new__ other def __name__ get com length app
# -*- coding: utf-8 -*-
import sys
class My24(int):
def __new__(cls, int_num, trace=‘‘, last_opt=‘‘):
obj = int.__new__(cls, int_num)
return obj
def __init__(self, int_num=0, trace=‘‘, last_opt=‘‘):
self.val = int_num
self.trace = trace if trace else str(int_num)
self.last_opt = last_opt
super(int, self).__init__(int_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 / 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 and b % a == 0:
vals.append(b / a)
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(a, b, c, d, target_val=24):
a, b, c, d = int(a), int(b), int(c), int(d)
result = comput24(*map(My24, [a, b, c, d]))
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(3, 3, 3, 8)
标签:index __new__ other def __name__ get com length app
原文地址:http://www.cnblogs.com/chens-smile/p/7667338.html