-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0282_expression_add_operators.py
More file actions
152 lines (120 loc) · 3.66 KB
/
0282_expression_add_operators.py
File metadata and controls
152 lines (120 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#------------------------------------------------------------------------------
# Question: 0282_expression_add_operators.py
#------------------------------------------------------------------------------
# tags: #pemdas
'''
Given a string that contains only digits 0-9 and a target value, return all
possibilities to add binary operators (not unary) +, -, or * between the
digits so they evaluate to the target value.
Example 1:
Input: num = "123", target = 6
Output: ["1+2+3", "1*2*3"]
Example 2:
Input: num = "232", target = 8
Output: ["2*3+2", "2+3*2"]
Example 3:
Input: num = "105", target = 5
Output: ["1*0+5","10-5"]
Example 4:
Input: num = "00", target = 0
Output: ["0+0", "0-0", "0*0"]
Example 5:
Input: num = "3456237490", target = 9191
Output: []
'''
#------------------------------------------------------------------------------
# Solutions
#------------------------------------------------------------------------------
from typing import *
class Solution:
def addOperators(self, num: str, target: int) -> List[str]:
'''
operators = set([+,-,*])
1,2,3,4,5
1,2,3,45
1,2,345
1,2345
12,3,4,5
12,3,45
12,345
123,4,5
123,45
1234,5
12345
'''
def default(x, y):
return None
def dispatch(opp, x, y):
return {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"*": lambda x, y: x * y,
}.get(opp, default)(x, y)
'''
1+2*3+4*5+6
V V
1+ 6 + 20 - 6
'''
def evaluate(exp):
def isbad(e):
'''
105
[1,+,05] -> 05 is bad path
'''
if len(e) > 1 and e[0] == '0':
return True
return False
if len(exp) == 0:
return float("-inf")
stack = []
n1 = None
for e in exp:
if isbad(e):
return float('inf')
if e == "*":
n1 = stack.pop()
elif n1:
stack.append(str(int(n1) * int(e)))
n1 = None
else:
stack.append(e)
result = int(stack[0])
for elem in stack[1:]:
if elem in operations:
opp = elem
else:
result = dispatch(opp, result, int(elem))
return(result)
def dfs(path=[], index=0):
e = evaluate(path)
if e == target:
result.append(path)
return
for i in range(index, len(num)):
x = num[index:i + 1]
if len(path) == 0:
dfs(path+[x], i+1)
else:
for opp in operations:
dfs(path + [opp] + [x], i + 1)
operations = "+-*"
result = []
dfs()
result = ["".join(x) for x in result]
return (result)
#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------
import unittest
class TestSolution(unittest.TestCase):
# def test_simple1(self):
# s = Solution()
# num = "123"
# target = 6
# self.assertEqual(s.addOperators(num, target), ["1+2+3", "1*2*3"])
def test_simple2(self):
s = Solution()
num = "105"
target = 5
self.assertEqual(s.addOperators(num, target), ["1*0+5", "10-5"])
unittest.main(verbosity=2)