Evaluate Reverse Polish Notation (RPN) - Leetcode 150 - Stacks (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

Using eval() for shorter code
stack = [] # stack contains strings
for c in tokens:
if c in '+-*/':
b, a = stack.pop(), stack.pop()
stack.append(str(int(eval(a + c + b))))
else:
stack.append(c)
return int(stack[0])

JoeTan-nqfq
Автор

Best channel I’ve discovered this year! Great content, thank you

masemark
Автор

Pay attention, in Python 2, the / operator performs integer division, and rounds towards negative infinity. So 6/-132 gives -1, and of course int(6/-132) also gives -1. In Python 3, the / operator performs float division, so 6/-132 is -0.045. Then the int function truncates decimals, so int(6/-132) is 0.

badrlakhal
Автор

I feel that this statement in the question is the most important. "The division between two integers always truncates toward zero."

mohitsonwane
Автор

Could you not use stk.append( int(a / b) ) for division instead of the if/else and use of ceil and floor?

ThePersopolis
Автор

Ceil and floor giving error in leetcode although in my local compiler the output is correct.
Try this instead:
int(float(a)/b)

saleheen
Автор

For js/ ts ceil doesn't work i think i had to use trunc ..

anirbandas
Автор

Noticed that in Java implementation you also used ceil and floor. But it is redundant for integer division. It always drops the part after dot.

VitaliyBorisok