Python with explanation - Generate All Possibilities
Use itertools.permutations to generate all the possible operands and operators to form an array of length 7, representing an equation of 4 operands and 3 operat
1. Use `itertools.permutations` to generate all the possible operands and operators to form an array of length 7, representing an equation of 4 operands and 3 operators.\n2. The `possible` function tries to evaluate the equation with different combinations of brackets, terminating as soon as an equation evaluates to 24. Each time `evaluate` is called, it reduces the length of the equation by 2, as it takes a triplet (operand, operator, operand) and evaluates into a value.\n3. Compare the final result of the equation with a small delta because of floating point inaccuracies.\n\n*- Yangshun*\n\n```\nclass Solution(object):\n    def judgePoint24(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: bool\n        """\n        import itertools\n        TARGET = 24\n        EQN_LEN = 3 # (Operand, Operator, Operand) triplet.\n        # Generate all possible number sequences. Convert to float string so that\n        # division does not result in truncation.\n        number_orders = set(tuple(itertools.permutations([str(num) + '.0' for num in nums])))\n        # Generate all possible operator sequences.\n        operator_orders = set(tuple(itertools.permutations('***///+++---', len(nums) - 1)))\n\n        # Evaluate an equation with different permutation of brackets\n        # and return True if any of them evaluate to the target.\n        def possible(equation):\n            found = [False]\n            def evaluate(eqn):\n                # Reduces an equation by length 2 each time:\n                # An equation of ['1.0', '*', '2.0', '+', '3.0', '/', '4.0'] becomes:\n                # - [2.0', '+', '3.0', '/', '4.0'] (1.0 * 2.0 reduced to 2.0)\n                # - [1.0', '*', '5.0', '/', '4.0'] (2.0 + 3.0 reduced to 5.0)\n                # - [1.0', '*', '2.0', '+', '0.75'] (3.0 / 4.0 reduced to 0.75)\n                if found[0]:\n                    return\n                if len(eqn) == EQN_LEN:\n                    val = eval(''.join(eqn))\n                    # Compare against a delta because of floating point inaccuracies.\n                    if abs(val - TARGET) < 0.0001:\n                        found[0] = True\n                    return\n                # Recursively try different permutations\n                # of operands + operators triplets, simulating brackets.\n                for i in range(0, len(eqn) - 1, 2):\n                    try:\n                        # Wrap in try/except as there can be a division by 0 error.\n                        evaluate(eqn[:i] + [str(eval(''.join(eqn[i:i + EQN_LEN])))] + eqn[i + EQN_LEN:])\n                    except:\n                        pass\n            evaluate(equation)\n            return found[0]\n\n        for number_order in number_orders:\n            for operator_order in operator_orders:\n                equation = [None] * (len(number_order) + len(operator_order))\n                for i, number in enumerate(number_order):\n                    equation[0 + i * 2] = number\n                for i, operator in enumerate(operator_order):\n                    equation[1 + i * 2] = operator\n                # Generate an equation to test whether it is possible to get 24 using it.\n                # Example equation: ['1.0', '*', '2.0', '+', '3.0', '/', '4.0']\n                if possible(equation):\n                    return True\n        return False\n```
[]