示例#1
0
文件: 24.py 项目: chigby/dotfiles
def find_solution(nums, show=True, ops=list('+-*/')):
    solving_combinations = {}
    found = False
    for n in permutations(nums):
        for p in product(ops, repeat=3):
            if solving_combinations.get(n) == p:
                continue
            for c in paren_combations(n):
                expr = ''.join(str(i) for i in list(flatten(izip_longest(c, p, fillvalue=''))))
                try:
                    result = parse(expr)
                except ZeroDivisionError:
                    continue

                if result == 24:
                    found = True
                    if show:
                        print expr, '---->', result
                        solving_combinations[n] = p
                        break
                    else:
                        print 'solution exists'
                        return
    if not found:
        print 'no solution found'
示例#2
0
def main(args):
    if len(args) < 2:
        print(f"Usage: {args[0]} <EXPRESSION> [<KEY>=<VALUE> ...]")
        print(f"")
        print(f"This program evaluates a arithmetic expression.")
        print(
            f"Any number of key-value-pairs can be given to use for variables:"
        )
        print(f"    {args[0]} x^2+y x=2 y=1")
        print(f"    5")
        exit(1)
    elif len(args) == 2:
        print(evaluate(args[1]))
    else:
        namespace = dict(
            ((key, evaluate(value))
             for key, value in map(lambda x: x.split("="), args[2:])))
        print(parse(args[1]).eval(**namespace))
    exit(0)
示例#3
0
    def parse_generated(self):

        if self.platform.name == "unit_testing":
            return {}

        cmake_cache_path = os.path.join(self.build_dir, "CMakeCache.txt")
        defconfig_path = os.path.join(self.build_dir, "zephyr", ".config")

        with open(defconfig_path, "r") as fp:
            defconfig = {}
            for line in fp.readlines():
                m = self.config_re.match(line)
                if not m:
                    if line.strip() and not line.startswith("#"):
                        sys.stderr.write("Unrecognized line %s\n" % line)
                    continue
                defconfig[m.group(1)] = m.group(2).strip()

        self.defconfig = defconfig

        cmake_conf = {}
        try:
            cache = CMakeCache.from_file(cmake_cache_path)
        except FileNotFoundError:
            cache = {}

        for k in iter(cache):
            cmake_conf[k.name] = k.value

        self.cmake_cache = cmake_conf

        filter_data = {
            "ARCH": self.platform.arch,
            "PLATFORM": self.platform.name
        }
        filter_data.update(os.environ)
        filter_data.update(self.defconfig)
        filter_data.update(self.cmake_cache)

        edt_pickle = os.path.join(self.build_dir, "zephyr", "edt.pickle")
        if self.testsuite and self.testsuite.filter:
            try:
                if os.path.exists(edt_pickle):
                    with open(edt_pickle, 'rb') as f:
                        edt = pickle.load(f)
                else:
                    edt = None
                res = expr_parser.parse(self.testsuite.filter, filter_data,
                                        edt)

            except (ValueError, SyntaxError) as se:
                sys.stderr.write("Failed processing %s\n" %
                                 self.testsuite.yamlfile)
                raise se

            if not res:
                return {
                    os.path.join(self.platform.name, self.testsuite.name): True
                }
            else:
                return {
                    os.path.join(self.platform.name, self.testsuite.name):
                    False
                }
        else:
            self.platform.filter_data = filter_data
            return filter_data
示例#4
0
def parse(expression_string, **names):
    expression = expr_parser.parse(expression_string)
    expression = substitute(expression, **names)
    return expression