Question DSA-STK-0061
HardWhat is the output of this code?
def calc(s):
st, num, op = [], 0, "+"
for i, c in enumerate(s + "+"):
if c.isdigit():
num = num * 10 + int(c)
elif c in "+-*/":
if op == "+": st.append(num)
elif op == "-": st.append(-num)
elif op == "*": st.append(st.pop() * num)
else: st.append(int(st.pop() / num))
op, num = c, 0
return sum(st)
print(calc("3+2*2-8/3"))
- A.
4
- B.
5
- C.
Sign up free to see the answer and a step-by-step explanation.