zoukankan      html  css  js  c++  java
  • python 算法4个数按照随机四则运算算出结果为24的值

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    """
    @author: 
    @version: 1.0.0
    @file: test_24.py
    @time: 2021/1/22 上午9:04
    @desc: 输入的4个数据,用加减乘除运算符来计算出能够凑出24
    """
    
    from pprint import pprint
    from itertools import permutations, combinations
    import operator
    from copy import deepcopy
    
    
    def func(*args):
        list_operate = ['+', '-', '*', '/']
        for op1 in list_operate:
            for op2 in list_operate:
                for op3 in list_operate:
                    # print(op3)
                    list_calc = [args[0], op1, args[1], op2, args[2], op3, args[3]]
                    res = recall_clac_list(deepcopy(list_calc))
                    # print(res)
                    if res == 24:
                        # print(list_calc)
                        # print(''.join([str(i) for i in list_calc]))
                        return ''.join([str(i) for i in list_calc])
    
    
    def recall_clac_list(list_calc):
        if '*' in list_calc or '/' in list_calc:
            for index, value in enumerate(list_calc):
                if value in ['*', '/']:
                    calc_value = calc(value, list_calc[index - 1], list_calc[index + 1])
                    list_calc2 = list_calc
                    [list_calc2.pop(index - 1) for i in range(3)]
                    list_calc2.insert(index - 1, calc_value)  # 指针会超出
                    if len(list_calc2) == 1:
                        return list_calc2[0]
                    return recall_clac_list(list_calc2)
        else:
            for index, value in enumerate(list_calc):
                if value in ['+', '-']:
                    calc_value = calc(value, list_calc[index - 1], list_calc[index + 1])
                    list_calc2 = list_calc
                    [list_calc2.pop(index - 1) for i in range(3)]
                    list_calc2.insert(index - 1, calc_value)  # 指针会超出
                    if len(list_calc2) == 1:
                        return list_calc2[0]
                    return recall_clac_list(list_calc2)
    
    
    def calc(operation, v1, v2):
        return get_operate_method(operation)(v1, v2)
    
    
    def get_operate_method(operation):
        dict_operator = {
            '+': operator.add,
            '-': operator.sub,
            '*': operator.mul,
            '/': operator.truediv
        }
        return dict_operator.get(operation)
    
    
    def main():
        """
        测试对列表操作后循环是否会受到影响
        :return:
        """
        # list1 = [i for i in range(10)]
        # for index, value in enumerate(list1):
        #     print(index, value)
        #     print(list1)
        #     list1.pop(0)
        #
        list_value = [1, 2, 6, 9]
        res = permutations(list_value, 4)
        for i in res:
            res = func(*i)
            print(res)
    
    
    if __name__ == '__main__':
        main()
    

    懂得,原来世界如此简单!

  • 相关阅读:
    【流量劫持】SSLStrip 终极版 —— location 瞒天过海
    【流量劫持】沉默中的狂怒 —— Cookie 大喷发
    【流量劫持】SSLStrip 的未来 —— HTTPS 前端劫持
    Web 前端攻防(2014版)
    流量劫持 —— 浮层登录框的隐患
    流量劫持能有多大危害?
    流量劫持是如何产生的?
    XSS 前端防火墙 —— 整装待发
    XSS 前端防火墙 —— 天衣无缝的防护
    XSS 前端防火墙 —— 无懈可击的钩子
  • 原文地址:https://www.cnblogs.com/qianxunman/p/14312504.html
Copyright © 2011-2022 走看看