代碼在這裏:

回答: 好厲害,沒有考倒你啊!求代碼!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

請您先登陸,再發跟帖!