Solved in 10 minutes

>solved in 10 minutes
And they call this medium

Attached: L150.png (518x1100, 95.83K)

reverse polish?
awruk

Dunno use a stack or something

fpbp
kek

Many CS grads will have encountered this exact question before, because evaluating postfix expressions is a very common example given when teaching stacks. So it's mainly testing whether you remember the general idea ("push operands to the stack, when you encounter an operator it's time to pop the last couple operands and do something to them"). If you've never encountered it before and still solved it in 10min, that's quite impressive.

If you want a good challenge (which will definitely take longer than 10mins), write an infix parser that can handle realistic human input. Think implicit multiplication, distinguishing negation from subtraction, tolerating slightly malformed input, etc.

>will not (can not) solve the problem

function rpn(input, stack) {
if(input.length == 0) {
return stack[0];
}
switch(input[0]){
case "+":
case "-":
case "*":
case "/": return rpn(input.slice(1), [eval(`Math.trunc(stack[1]${input[0]}stack[0])`), ...stack.slice(2)]);
default: return rpn(input.slice(1), [Number(input[0]),...stack]);
}
}

console.log( rpn( "10 6 9 3 + -11 * / * 17 + 5 +".split(" "), []));


>but you shouldn't use eval
Fuck you.

>10 minutes
NGMI

Forget eval mods ought to ban you for javascript

Solved it in 5 minutes, then debugged my truncate method for 15. Gave up and used operator.truediv

I only use the browser's console so fuck you too.

Is this just a stack?

>solved before even entering the thread
it's over for OP
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
for c in tokens:
if c in "+*/-":
right = stack.pop()
left = stack.pop()
if c == "+":
stack.append(left + right)
elif c == "-":
stack.append(left - right)
elif c == "*":
stack.append(left * right)
else:
stack.append(math.trunc(left/right))

else:
stack.append(int(c))
return stack.pop()

Attached: file.png (978x105, 6.26K)

>not using handlers for operands
Don't call us, we'll you

cope

Attached: file.png (739x140, 14.68K)

>iterate until find non-numeric value
>use current index to gather current value and previous two values
>use "if" statements to perform correct arithmetic (ex: if x == "*")
>assign result to current index
>pop previous two indices
>restart iteration from current index minus two (due to popped indices)
fairly simple solve

Attached: 1644071997347.jpg (573x720, 130.28K)

It was just a stack.
Had to debug the division a bit but very simple.

class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
def GetFromStack():
return(stack.pop(), stack.pop())

for e in tokens:
if e == "+":
b,a = GetFromStack()
stack.append(a+b)
elif e == "*":
b,a = GetFromStack()
stack.append(a*b)
elif e == "/":
b,a = GetFromStack()
stack.append(int(a/b))
elif e == "-":
b,a = GetFromStack()
stack.append(a-b)
else:
stack.append(int(e))

return stack.pop()

Python have types now?

Attached: index.jpg (1280x720, 65.25K)

ngl a lot of the medium difficulty questions will throw some weird math trivia into the mix (like the "product of all except self"), this one seems like it should be easy difficulty but maybe that's just because i've had to make an RPN calculator before.

it’s leetcode
it’s designed for normies who are shit at coding, to get them up to speed for interviews
if you are not able to solve every easy problem in 10 minutes, every medium problem in 15 minutes, and every advanced problem in 1 hour, you’re a shit programmer

>15 mins for you
I see