還不行的話,就留個聯係方式,我發給你。
import itertools
import re
BAD_LIST = ['55', '5(', ')(', ')5', '(5)']
FIRST_CHAR_LIST = ['(', '-', '5']
LAST_CHAR_LIST = [')', '5']
def init_lst():
return ['5', '5', '5', '5', '5', '+', '-', '*', '/', '(', ')']
def generate ((first, last)):
lst.remove(first)
lst.remove(last)
for f in itertools.permutations(lst):
yield first + ''.join(f) + last
if __name__ == '__main__':
lst = init_lst()
count = 0
exp_list = []
my_list = [ r for r in itertools.product(FIRST_CHAR_LIST, LAST_CHAR_LIST)]
print '======= Possible combinations for the math question ============'
for first, last in my_list:
for exp in generate((first, last)):
if any([y in exp for y in BAD_LIST]): continue
elif re.findall(r'[-*/+][-*/+]+|^\(.*\)$', exp): continue
else:
try:
result = eval(exp)
if exp not in exp_list:
exp_list.append(exp)
print exp, '=', result
count += 1
except: pass
lst = init_lst()
print 'Total expressions found = ', count