# Bruteforce python solution

• Finished this one because I was revisiting the stack data structure with postfix expressions.
Good thing: it shows the use of various itertools and some string format stuff, and there is no need to consider brackets
Bad thing: it really won't differentiate between different operators so ZeroDivisionError has to be checked every time, and it's incredibly slow

``````class Solution:
import itertools
def evalPostExp(self,postlist):
arr=[]
for thing in postlist:
if thing not in "+-*/^":
arr.append(int(thing))
else:
arr.append({'+':lambda x,y:y+x,'-':lambda x,y:y-x,'*':lambda x,y:y*x,'/':lambda x,y:y/x}[thing](arr.pop(),arr.pop()))
return abs(arr[0]-24)<=0.0001

def judgePoint24(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
lst=["{0}{1}{2}{3}{4}{5}{6}","{0}{1}{2}{4}{3}{5}{6}","{0}{1}{2}{4}{5}{3}{6}","{0}{1}{4}{2}{5}{3}{6}","{0}{1}{4}{2}{3}{5}{6}"]
for p in itertools.permutations(nums):
for x,y,z in itertools.product('+-*/', repeat=3):
for exp in lst:
try:
if self.evalPostExp(exp.format(p[0],p[1],p[2],p[3],x,y,z)):
return True
except ZeroDivisionError:
pass
return False
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.