-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path24_game.py
More file actions
20 lines (17 loc) · 749 Bytes
/
Copy path24_game.py
File metadata and controls
20 lines (17 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Credits - LeetCode, complexity = O(1)
from operator import truediv, mul, add, sub
class Solution(object):
def judgePoint24(self, A):
if not A: return False
if len(A) == 1: return abs(A[0] - 24) < 1e-6
for i in xrange(len(A)):
for j in xrange(len(A)):
if i != j:
B = [A[k] for k in xrange(len(A)) if i != k != j]
for op in (truediv, mul, add, sub):
if (op is add or op is mul) and j > i: continue
if op is not truediv or A[j]:
B.append(op(A[i], A[j]))
if self.judgePoint24(B): return True
B.pop()
return False