我用的是Komodo IDE,也有不少人在用Pycharm。不知道源码能否显示得好

来源: 在城里 2016-01-06 05:54:05 [] [博客] [旧帖] [给我悄悄话] 本文已被阅读: 次 (2412 bytes)
本文内容已被 [ 在城里 ] 在 2016-01-06 05:56:50 编辑过。如有问题,请报告版主或论坛管理删除.

还不行的话,就留个联系方式,我发给你。

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

请您先登陆,再发跟帖!

发现Adblock插件

如要继续浏览
请支持本站 请务必在本站关闭/移除任何Adblock

关闭Adblock后 请点击

请参考如何关闭Adblock/Adblock plus

安装Adblock plus用户请点击浏览器图标
选择“Disable on www.wenxuecity.com”

安装Adblock用户请点击图标
选择“don't run on pages on this domain”