-
Notifications
You must be signed in to change notification settings - Fork 0
/
basiccalculator2.py
345 lines (300 loc) · 13.1 KB
/
basiccalculator2.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#Given a string s which represents an expression, evaluate this expression and return its value.
#The integer division should truncate toward zero.
#You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1].
#Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
#Input: s = "3+2*2"
#Output: 7
#the reason the output to "14-3/2" is 13 and not 12! remember, we want int(-3 / 2) aka -1 and not (-3 // 2) aka -2, so in the end, stack = [14, -1], so sum(stack) yields 13 as result
#python3 solution:
class Solution:
def calculate(self, s: str) -> int:
number, sign, stack = 0, '+', []
for index, value in enumerate(s):
if value.isnumeric():
number = number * 10 + int(value) #in "14-3/2", the 1st iteration means that value = 1, not 14, so number becomes 1, and then if value in statement becomes false, so we keep looping to next iteration of for loop, and now, in the 2nd iteration, value becomes 4, so num = 1 * 10 + 4, which means nums becomes 14
if value in '+-/*' or index == len(s) - 1:
if sign == "+":
stack.append(number)
elif sign == "-":
stack.append(-number)
elif sign == "*":
j = stack.pop() * number
stack.append(j)
elif sign == "/":
#without this, int(-3 / 2) > -1.5 > -1 vs. (-3 // 2) > -2, so 12 vs 13 as output for the s = "14-3/2" test case
j = int(stack.pop() / number)
stack.append(j)
sign = value #if this was indented to outer if block, we would get an error that says pop from empty list because sign would incorrectly become "3" vs. sign would remain "+" when indented correctly, which is wrong, and it would cause the stack to remain empty because sign wouldn't be equal to '+', so num would never get appended to the stack for test case s = "3+2*2"
number = 0 #for s = "14-3/2", if we didn't have this, number would become 143 instead of 3
return sum(stack)
#5/6/24 refresher:
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", []
for index, value in enumerate(s):
if value.isnumeric():
currentn = currentn * 10 + int(value)
if value in "+-/*" or index == len(s) - 1:
if sign == "+":
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
popandmultiply = stack.pop() * currentn
stack.append(popandmultiply)
elif sign == "/":
divide = int(stack.pop() / currentn)
stack.append(divide)
sign = value
currentn = 0
return sum(stack)
#5/7/24 refresher:
class Solution:
def calculate(self, s: str) -> int:
number, sign, stack = 0, "+", []
for index, char in enumerate(s):
if char.isdigit():
number = number * 10 + int(char)
if char in "+-*/" or index >= len(s) - 1:
if sign == "+":
stack.append(number)
elif sign == "-":
stack.append(-number)
elif sign == "*":
j = stack.pop() * number
stack.append(j)
elif sign == "/":
j = int(stack.pop() / number)
stack.append(j)
sign = char
number = 0
return sum(stack)
#5/9/24 refresher:
class Solution:
def calculate(self, s: str) -> int:
number, sign, stack = 0, "+", []
for index, char in enumerate(s):
if char.isdigit():
number = number * 10 + int(char)
if char in "+-/*" or index >= len(s) - 1:
if sign == "+":
stack.append(number) #we add NUMBER to the stack, not char because char is the sign if this block gets executed!
elif sign == "-":
stack.append(-number)
elif sign == "*":
j = stack.pop() * number #we multiply by number, because, again, char is the sign!
stack.append(j)
elif sign == "/":
j = int(stack.pop() / number)
stack.append(j)
sign = char
number = 0
return sum(stack)
#5/11/24 refresher:
class Solution:
def calculate(self, s: str) -> int:
number, sign, stack = 0, "+", []
for index, char in enumerate(s):
if char.isdigit():
number = number * 10 + int(char)
if char in "+-/*" or index >= len(s) - 1:
if sign == "+":
stack.append(number)
elif sign == "-":
stack.append(-number)
elif sign == "*":
j = stack.pop() * number
stack.append(j)
elif sign == "/":
j = int(stack.pop() / number)
stack.append(j)
sign = char
number = 0
return sum(stack)
#5/15/24 refresher:
class Solution:
def calculate(self, s: str) -> int:
# sign goes from + to * to 2 in "3+2*2"
number, sign, stack = 0, "+", []
for i, j in enumerate(s):
if j.isdigit():
number = number * 10 + int(j)
if j in "+-/*" or i >= len(s) - 1:
if sign == "+":
stack.append(number)
elif sign == "-":
stack.append(-number)
elif sign == "*":
val = stack.pop() * number
stack.append(val)
elif sign == "/":
val = int(stack.pop() / number)
stack.append(val)
sign = j #we need this line because, when the for loop reaches * in "3+2*2", sign is still "+", so we append number, and stack = [3, 2], and then we set sign = "*". when for loop reaches final 2, stack = [3, 2], so stack.pop() = 2, and number = 2, so val = 2 * 2 = 4, so stack goes from [3, 2] to [3, 4] - NOTE THAT IN THE FINAL ITERATION, THE BLOCK ONLY EXECUTES BECAUSE I == LEN(S) - 1 IS TRUE!, SO SIGN IS ACTUALLY FINALLY SET TO "2"
number = 0
#in the end, since stack = [3, 4], sum(stack) = 7 for s = "3+2*2"
return sum(stack)
#6/1/24 (missed):
class Solution:
def calculate(self, s: str) -> int:
#"3+2*2"
currentn, sign, stack = 0, "+", []
for i, char in enumerate(s):
if char.isdigit():
currentn = currentn * 10 + int(char)
if char in "+-/*" or i >= len(s) - 1: #i >= len(s) - 1 True will trigger this when we reach the last 2 in "3+2*2", so DON'T FORGET - 1 BECAUSE I IS AN INDEX
if sign == "+":
stack.append(currentn) #[3] becomes [3, 2]
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
val = stack.pop() * currentn #[3, 2]
stack.append(val) #[3, 4]
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
sign = char #goes from + to * to 2 (last one triggered by i >= len(s) - 1)
currentn = 0
return sum(stack) #sum([3, 4]) = 7
#6/2/24 review:
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", []
for i, char in enumerate(s):
if char.isdigit():
currentn = currentn * 10 + int(char)
if char in "+-/*" or i == len(s) - 1:
if sign == "+":
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
val = stack.pop() * currentn
stack.append(val)
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
sign = char
currentn = 0
return sum(stack)
#6/5/24 review:
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", []
for i, char in enumerate(s):
if char.isdigit():
currentn = currentn * 10 + int(char)
if char in "+-/*" or i >= len(s) - 1:
if sign == "+":
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
val = stack.pop() * currentn
stack.append(val)
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
sign = char
currentn = 0
return sum(stack)
#print(int(-3 / 2)) vs. print(-3 // 2)
#print(-3 // 2) > uses floor division. floor division rounds down to the nearest whole number towards negative infinity
#int(-3 / 2) > int discards the fractional part, so -1.5 becomes -1
#6/17/24 review (missed 2 days ago):
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", []
for i, char in enumerate(s):
if char.isdigit():
currentn = currentn * 10 + int(char)
if char in "+-/*" or i >= len(s) - 1: #DO NOT FORGET -1 BECAUSE INDEXES ARE ONE LESS THAN LENGTH! YOU WOULD GET 5 INSTEAD OF 7 AS OUTPUT BECUASE WOULDN'T CONSIDER THE LAST 2 IN 3+2*2
if sign == "+":
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
elif sign == "*":
val = stack.pop() * currentn
stack.append(val)
sign = char
currentn = 0
return sum(stack)
#6/24/24 review:
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", []
for i, char in enumerate(s):
if char.isdigit():
currentn = currentn * 10 + int(char)
if char in "+-/*" or i >= len(s) - 1:
if sign == "+":
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
val = stack.pop() * currentn
stack.append(val)
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
sign = char
currentn = 0
return sum(stack)
#7/10/24 review:
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", [] #remember that sign starts at "+" instead of 1 because of the 2nd inner if block
for i, char in enumerate(s):
if char.isdigit():
currentn = currentn * 10 + int(char)
if char in "+-/*" or i >= len(s) - 1:
if sign == "+":
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
val = stack.pop() * currentn
stack.append(val)
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
sign = char
currentn = 0
return sum(stack)
#7/25/24 refresher:
class Solution:
def calculate(self, s: str) -> int:
currentn, sign, stack = 0, "+", []
for i, char in enumerate(s):
if char.isdigit():
#12 > 1 * 10 + int("2") = 12
currentn = currentn * 10 + int(char)
if char in "+/*-" or i >= len(s) - 1:
if sign == "+":
#char here is a symbol, so we append currentn, not char, to our stack!
stack.append(currentn)
elif sign == "-":
stack.append(-currentn)
elif sign == "*":
val = int(stack.pop() * currentn)
stack.append(val)
elif sign == "/":
val = int(stack.pop() / currentn)
stack.append(val)
sign = char #sign becomes the operand that char was, not currentnumber
currentn = 0
return sum(stack)
#my own solution using python3 on 11/18/24:
class Solution:
def calculate(self, s: str) -> int:
stack = []
for c in s:
if c == "/":
stack.append("//")
else:
stack.append(c)
print(stack)
return eval("".join(stack))