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

Поделиться
HTML-код
  • Опубликовано: 4 янв 2025

Комментарии • 16

  • @GregHogg
    @GregHogg  5 месяцев назад +2

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

  • @masemark87
    @masemark87 10 месяцев назад +1

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

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 2 месяца назад +1

    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])

  • @ThePersopolis
    @ThePersopolis 10 месяцев назад +4

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

  • @badrlakhal5440
    @badrlakhal5440 4 месяца назад

    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.

  • @Sanu-k5c
    @Sanu-k5c 29 дней назад

    Hello there, when putting the condition for the token being a number in the if and it being an operator in the else part, why was there a problem with the pop() statement, error said as "pop from an empty list", ? Has it got something to do with the interpreter ?
    Although it works fine in vs code

  • @mohitsonwane7031
    @mohitsonwane7031 5 месяцев назад

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

  • @AliShahid-cp6bz
    @AliShahid-cp6bz 6 месяцев назад

    Great content and dedication.

  • @saleheen12
    @saleheen12 3 месяца назад

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

    • @haribshahbaz1237
      @haribshahbaz1237 2 месяца назад

      or just simply int(a / b). a / b already gives us a float, therefore converting a to float explicitly is not required.

  • @anirbandas12
    @anirbandas12 10 месяцев назад

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

  • @VitaliyBorisok
    @VitaliyBorisok 5 месяцев назад

    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.

  • @moonlight-td8ed
    @moonlight-td8ed 5 месяцев назад

    or we can just append int(a/b)