You have 4 cards each containing a number from 1 to 9.  You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
Example 1:
Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2]
Output: False
Note:
The division operator / represents real division, not integer division.  For example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers.  In particular, we cannot use - as a unary operator.  For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
You cannot concatenate numbers together.  For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.
class Solution(object):
     def judgePoint24(self, nums):
         """
         :type nums: List[int]
         :rtype: bool
         """
         
         def cal(operands, operaters):
             for opt in operaters:
                 op2, op1 = operands.pop(-1), operands.pop(-1)
                 if opt=='+':
                     ret = op1+op2
                 elif opt=='-':
                     ret = op1-op2
                 elif opt=='*':
                     ret = op1*op2
                 elif opt=='/' and op2!=0:
                     ret = op1/op2
                 else:
                     return float('-inf')
                 operands.append(ret)
             
             return operands[0] if abs(operands[0]-24)>0.0000001 else 24
         
         def comb(arr):
             if len(arr)==1:
                 return [arr]
             ret = []
             for i in range(len(arr)):
                 if i-1>=0 and arr[i]==arr[i-1]:
                     continue
                 for remain in comb(arr[:i]+arr[i+1:]):
                     ret.append([arr[i]]+remain)
             return ret
         
         def genopt(n):
             if n==0:
                 return ['']
             ret = []
             for i in '+-*/':
                 for res in genopt(n-1):
                     ret.append(i+res)
             return ret
         
         nums.sort()
         ops = comb(nums)
         opts = genopt(3)
         for op in ops:
             for opt in opts:
                 if cal(op[:], opt)==24:
                     return True
                 
         def base(a, b):
             return set([a+b,a-b,b-a,a*b]+([a/b] if b!=0 else[])+([b/a] if a!=0 else[]))
             
         for i in range(4):
             for j in range(i+1, 4):
                 a, b = nums[i], nums[j]
                 remain = nums[:i]+nums[i+1:j]+nums[j+1:4]
                 for x in base(a, b):
                     for y in base(remain[0], remain[1]):
                         if 24 in base(x, y):
                             return True
                 
         
         return False
class Solution(object):
     def judgePoint24(self, nums):
         """
         :type nums: List[int]
         :rtype: bool
         """
         
         def cal(operands, operaters):
             for opt in operaters:
                 op2, op1 = operands.pop(-1), operands.pop(-1)
                 if opt=='+':
                     ret = op1+op2
                 elif opt=='-':
                     ret = op1-op2
                 elif opt=='*':
                     ret = op1*op2
                 elif opt=='/' and op2!=0:
                     ret = op1/op2
                 else:
                     return float('-inf')
                 operands.append(ret)
             
             return operands[0] if abs(operands[0]-24)>0.0000001 else 24
         
         def comb(arr):
             if len(arr)==1:
                 return [arr]
             ret = []
             for i in range(len(arr)):
                 if i-1>=0 and arr[i]==arr[i-1]:
                     continue
                 for remain in comb(arr[:i]+arr[i+1:]):
                     ret.append([arr[i]]+remain)
             return ret
         
         def genopt(n):
             if n==0:
                 return ['']
             ret = []
             for i in '+-*/':
                 for res in genopt(n-1):
                     ret.append(i+res)
             return ret
         
         nums.sort()
         ops = comb(nums)
         opts = genopt(3)
         for op in ops:
             for opt in opts:
                 if cal(op[:], opt)==24:
                     return True
                 
         def base(a, b):
             return set([a+b,a-b,b-a,a*b]+([a/b] if b!=0 else[])+([b/a] if a!=0 else[]))
             
         for i in range(4):
             for j in range(i+1, 4):
                 a, b = nums[i], nums[j]
                 remain = nums[:i]+nums[i+1:j]+nums[j+1:4]
                 for x in base(a, b):
                     for y in base(remain[0], remain[1]):
                         if 24 in base(x, y):
                             return True
                 
         
         return False