代码在这里:

回答: 好厉害,没有考倒你啊!求代码!hot_powerz2016-01-07 19:27:13

import re, itertools

exp_list = []
OPS_LIST = ['+','*']    # operators allowed
ANSWER = 69

if __name__ == '__main__':
    print '======= Possible combinations for the math question ============'
    for op in itertools.product( OPS_LIST, repeat = 9):
        exp = ''.join( [ item for pair in zip([str(x) for x in range(0, 10)],  \
            list(op) + [0]) for item in pair][:-1])
        try:
            result = eval(exp)
            if result == ANSWER and (exp not in exp_list):
                exp_list.append(exp)
                print '%s = %d' % (exp, result)
        except: pass        
    print 'Total expressions found = ', len(exp_list)

所有跟帖: 

好精炼的代码,多少年的功力?再赞!以后代码问题就找你了! -hot_powerz- 给 hot_powerz 发送悄悄话 hot_powerz 的博客首页 (0 bytes) () 01/07/2016 postreply 21:37:03

没问题!我们一起共同进步。。。 -在城里- 给 在城里 发送悄悄话 在城里 的博客首页 (0 bytes) () 01/08/2016 postreply 05:39:43

请您先登陆,再发跟帖!