Python beats 99% - hopefully clear code and explanation
\nclass Solution:\n    def judgePoint24(self, cards: List[int]) -> bool:\n        \n        self.allcombos = []\n        \n        # need all combinations of ca
```\nclass Solution:\n    def judgePoint24(self, cards: List[int]) -> bool:\n        \n        self.allcombos = []\n        \n        # need all combinations of cards\n        def permute(arr,remcards):\n            if len(arr)==4:\n                self.allcombos.append(arr[:])\n                return\n                \n            for i,card in enumerate(remcards):\n                arr.append(card)\n                combos(arr,remcards[:i]+remcards[i+1:])\n                arr.pop()        \n        \n        # then all combinations of operations for all parens placements\n        def calculate(cards):\n            if len(cards)<=1:\n                return cards\n            else:\n                values = set()\n\t\t\t\tleft = calculate(cards[:splitidx])\n\t\t\t\tright = calculate(cards[splitidx:])\n                for splitidx in range(1,len(cards)):\n                    for lval in left:\n                        for rval in right:\n                            values.add(lval+rval)\n                            values.add(lval-rval)\n                            values.add(lval*rval)\n                            if rval!=0:\n                                values.add(lval/rval)\n                return values\n        \n        permute([],cards)\n        for combo in self.allcombos:\n            for c in calculate(combo):\n                if c>23.99 and c<24.01: return True\n        return False\n```\n\nThe permute function just comes up will all permuations of the cards list.  There are other leetcode quesitons that handle this portion or you can use itertools.permutations as well (I implemented a backtracking solution for practice).\n\nThe tricker part of this is coming up with how to have every combination of parens and every combination of operator. I implemented this recursively.  The trick is to have a function that calculates all possible values for the cards on the left and cards on the right of a \'split\'. \n\nAs an example, you might have the cards [4,1,2,5]. You can split this into ([4] and [1,2,5]) or ([4,1] and [2,5]) or ([4] and [1,2,5]).  What we want is to calculate all possible values for each of these splits and then apply our operators to both sides.  When we have the split on idx 2 ([4,1] and [2,5]) we come up with left values like 4+1 = 5, 4-1 = 3, 4*1=4, 4/1=4 and do the same for the right side. What gets returned is [5,3,4] (removing duplicates) for the left side and some other list of values for the right side. Then we apply all of our operators to each pairing in both lists.\n\nBesides that we just go through  the outputs for each permutation of cards and see if 24 shows up.\n\nThats it!\n\nThe above code beats 95% -- if you use itertools.permutations it beats 99%\n\n```        \ndef calculate(cards):\n\tif len(cards)<=1:\n\t\treturn cards\n\telse:\n\t\tvalues = set()\n\t\tfor splitidx in range(1,len(cards)):\n\t\t\tfor lval in calculate(cards[:splitidx]):\n\t\t\t\tfor rval in calculate(cards[splitidx:]):\n\t\t\t\t\tvalues.add(lval+rval)\n\t\t\t\t\tvalues.add(lval-rval)\n\t\t\t\t\tvalues.add(lval*rval)\n\t\t\t\t\tif rval!=0:\n\t\t\t\t\t\tvalues.add(lval/rval)\n\t\treturn values\n\nfor combo in itertools.permutations(cards):\n\tfor c in calculate(combo):\n\t\tif c>23.99 and c<24.01: return True\nreturn False\n\t\t```
['Python']