Infix to reverse polish using a stack

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 พ.ค. 2013
  • Converting from infix to reverse polish (postfix) notation using a stack

ความคิดเห็น • 89

  • @computerkeen8210
    @computerkeen8210 5 ปีที่แล้ว +35

    you made a 3 hour lecture in only 9 minutes, thats talent

    • @HurrayBanana
      @HurrayBanana  5 ปีที่แล้ว +11

      That was the intention

  • @eltrasimaco
    @eltrasimaco 9 ปีที่แล้ว +12

    Ive seen several tutorials here but this one is crystal clear

    • @HurrayBanana
      @HurrayBanana  9 ปีที่แล้ว +2

      thanks, that's the idea

    • @T010RD
      @T010RD 6 ปีที่แล้ว

      It's much clearer than virgin's teardrop

  • @wakaboomnick
    @wakaboomnick 10 ปีที่แล้ว +24

    Thank you, made it a lot easier than my teacher

  • @Wintotv
    @Wintotv 7 ปีที่แล้ว +35

    love your english accent too xD i can't understand those Indian tutorials

  • @Tr4vy
    @Tr4vy 10 ปีที่แล้ว +19

    thanks for the "fack"ing help hehehe

  • @HurrayBanana
    @HurrayBanana  9 ปีที่แล้ว +2

    @kiran, your thinking is correct, because they are the same precedence, need to remove from stack (doesn't really matter with + on + or - on -) but is for + on - or - on +

  • @tej_3423
    @tej_3423 5 ปีที่แล้ว +3

    Thanks a lot! I'm taking part in CIE exams computer science tomorrow... you make it more easier

  • @josephburke7924
    @josephburke7924 8 ปีที่แล้ว +1

    every year, when I have to teach RPN, I always end up watching this video. Cheers mate great vid :)

    • @HurrayBanana
      @HurrayBanana  8 ปีที่แล้ว

      +Joseph Burke
      No problems, pity it's come out of the new specifcations. Favourite topic

  • @JP-su9ib
    @JP-su9ib 3 ปีที่แล้ว

    you"re such a charismatic, well explaining teatcher.

  • @MrFacePeck
    @MrFacePeck 4 ปีที่แล้ว +1

    Finally an understandable explanation of this. Thanks so much.

  • @MustafaOzanAlpay
    @MustafaOzanAlpay 6 ปีที่แล้ว

    that video made everything crystal clear, and loved the accent hehe ^^ cheers mate!

  • @OghmaNano
    @OghmaNano ปีที่แล้ว +1

    Really nice expiation, just implemented it in my semiconductor device model in C.

  • @sheilasubbiah5969
    @sheilasubbiah5969 7 ปีที่แล้ว +2

    I had the same question as the student in the video about the left bracket which goes on the stack, but I think I understand now; basically the left bracket 'has a precedence' which other operators compare their own precedence against, but the left bracket ignores its own precedence (i.e. never compares its own precedence against any of the operators (even though it could)) and just goes to sit on top of the stack.
    Thanks for your great example, with an assignment and other tricky bits! It's really helped me.

    • @HurrayBanana
      @HurrayBanana  7 ปีที่แล้ว +1

      glad it helped

    • @HurrayBanana
      @HurrayBanana  7 ปีที่แล้ว +1

      Yeah, the left bracket sits on the stack so further operators can override whatever precedence was already on top.

  • @BarnabaRudge
    @BarnabaRudge 9 ปีที่แล้ว +1

    For the good of humanity, such lectures should records and presents.

  • @serhii_rudakov
    @serhii_rudakov ปีที่แล้ว

    amazing, Thank you for this great explanation

  • @artsyishita8551
    @artsyishita8551 6 ปีที่แล้ว

    Ohh thank you now i understood this concept....really helpful for my exams...thanks a lot...

  • @HawksVideoNest
    @HawksVideoNest 7 ปีที่แล้ว +5

    Well thank you :D Definitely saved me from failing an exam completely

  • @rapramos5687
    @rapramos5687 7 ปีที่แล้ว +1

    wow thank you so much! helped a lot!

  • @Wintotv
    @Wintotv 7 ปีที่แล้ว

    this just saved my life, thanks you so much Mr Banana =)

    • @HurrayBanana
      @HurrayBanana  7 ปีที่แล้ว

      No problem, my favourite topic of all time :)

  • @thebarnold7234
    @thebarnold7234 2 ปีที่แล้ว +3

    I wish this was longer. Love the way you actually taught this.

    • @HurrayBanana
      @HurrayBanana  2 ปีที่แล้ว +3

      Thanks, it's such a cool thing reverse polish

  • @einarstensson4323
    @einarstensson4323 8 ปีที่แล้ว

    Thank you! Interesting.

  • @kombuchamp
    @kombuchamp 4 ปีที่แล้ว +1

    Cheers, helped a lot to figure out an algorithm

  • @Wyattap125
    @Wyattap125 8 ปีที่แล้ว

    holy crap this was so helpful

  • @_Rohan007_
    @_Rohan007_ 2 ปีที่แล้ว

    Thank you so much you explained it really well Sir

  • @CaptainClueless01
    @CaptainClueless01 8 ปีที่แล้ว +1

    Well now I can do reverse polish. Top notch!

    • @HurrayBanana
      @HurrayBanana  8 ปีที่แล้ว +2

      +CaptainClueless Everyone should be able to do it, it's cool

  • @appleraika
    @appleraika 9 ปีที่แล้ว

    awesome!! thanks :)

  • @SnakeOilDev
    @SnakeOilDev 6 ปีที่แล้ว

    purfect !

  • @jakubputynkowski8554
    @jakubputynkowski8554 8 ปีที่แล้ว

    great explaination :) thank you!

    • @HurrayBanana
      @HurrayBanana  8 ปีที่แล้ว

      No problem, happy it was of some use

  • @James-se1rq
    @James-se1rq 2 ปีที่แล้ว +1

    Lindy approved video!

  • @IDONTEATPANTS
    @IDONTEATPANTS 7 ปีที่แล้ว

    Please be my professor. Also the FACK part made me lol

  • @michaelholder5975
    @michaelholder5975 9 ปีที่แล้ว

    What precedence would factorial "!" have. Does it go above exponents?

  • @krzysztofwelc146
    @krzysztofwelc146 5 ปีที่แล้ว

    God bless You

  • @jim93m
    @jim93m 10 ปีที่แล้ว +2

    As easier as it gets :) thnx

  • @kidrahulable
    @kidrahulable 7 ปีที่แล้ว

    Great job! Thank you very much for help! ;)
    No I understand it :D

  • @laureven
    @laureven 4 ปีที่แล้ว +1

    is always better to invest time for finding a good tutorial :) ...and Yours is Awesome :) ...search is over :)

  • @kiranrambha
    @kiranrambha 9 ปีที่แล้ว +1

    plus cannot be placed on plus
    minus cannot be placed on minus
    right?????

  • @saifmohammed1481
    @saifmohammed1481 3 ปีที่แล้ว

    "Facking" awesome !!

  • @samuelhor9700
    @samuelhor9700 9 ปีที่แล้ว

    Any idea what order of precedence the trig functions sin, cos and tan fall under?

    • @michaelholder5975
      @michaelholder5975 9 ปีที่แล้ว

      Trigonometric functions, or functions in general, go above exponents.
      For example: 2 + 3^4 * cos(0)
      Steps:
      1. 2 is an operand; Add 2 to the output expression
      2. + is an operator; Stack = empty; push + onto the stack
      3. 3 is an operand; Add 3 to the output expression
      4. ^ is an operator; Top of stack has lower precedence; push ^ onto stack
      5. 4 is an operand; Add 4 to the output expression
      6. * is an operator; Top of stack has higher precedence; pop stack until lower precedence is found; push * onto stack
      7. cos is treated as an operator; top of stack has lower precedence; push cos onto stack
      8. "(" = push onto stack regardless of lower or higher precedence or stack is empty
      9. 0 is an operand; Add 0 to output expression
      10. ")" = Pop and add to output expression until "(" = top of stack; pop top of stack
      11. Since ")" is the end of the expression, pop the stack until stack = empty
      Result: 2 3 4 ^ 0 cos * +

  • @user-pw3pu8wu3f
    @user-pw3pu8wu3f 5 ปีที่แล้ว

    But how do you deal with expressions with the unary operator like:
    -A + B?

    • @HurrayBanana
      @HurrayBanana  5 ปีที่แล้ว +1

      Higher priority than multiply and divide. Only Pop one item from stack like store does

  • @oguzkaraca1434
    @oguzkaraca1434 6 ปีที่แล้ว

    did he just give an example over "fack up"?

  • @henryl3e
    @henryl3e 9 ปีที่แล้ว

    would like to know what if there is a negative sign meet with a negative sign ? or any other sign meeting the same ones

    • @henryl3e
      @henryl3e 9 ปีที่แล้ว

      for example pq-rs-/

    • @HurrayBanana
      @HurrayBanana  9 ปีที่แล้ว +3

      WooHoooP
      Don't understand your example that would come from the infix
      (p - q) / (r - s)
      But to answer your question:
      if you have an expression of this form
      a - - b
      the minus in front of the b is actually unary minus (negation, applied to one operand)
      we would re-write that expression like this
      a - ~ b (using tilda to represent the unary minus)
      this would generate as unary minus is higher precedence that + or minus.
      a b ~ -
      which would negate the b before attempting to subtract it from a, which is logical in this example
      The precedence of unary minus is open to debate in some environments its placed higher than exponent (raising to the power ^) but others may place it below this but above multiply and divide.
      ~ b ^ a
      if unary minus was higher we would get
      b ~ a ^
      which would negate b then raise to the power of a, but some would argue that we should have exponent higher than unary minus meaning this
      b a ^ ~
      which would raise the b to the power of a and negate the result

  • @s1nister688
    @s1nister688 4 ปีที่แล้ว

    What happens when there is a modulus sign present?

    • @HurrayBanana
      @HurrayBanana  4 ปีที่แล้ว

      Depends what the precedence for modulus is in that language

  • @XXxlightmarex
    @XXxlightmarex 7 ปีที่แล้ว +1

    What do i do when i have multiple open brackets e.g. cos((a + b*c)/(d + e*(f-g^h))) ?

    • @HurrayBanana
      @HurrayBanana  7 ปีที่แล้ว

      XXxlightmarex just keep applying the rules. every left bracket goes on top of stack. when you get a right bracket remove contents of stack untill you find a left bracket, then discard that snd continue to parse the expression

    • @HurrayBanana
      @HurrayBanana  7 ปีที่แล้ว

      work through remember operands just get written down as rpn (don't involve the stack at all)
      ((a + b * c) / (d + e * (f - g ^ h)))
      reaching first right bracket stack contains
      *
      +
      (
      (
      remove up to topmost left bracket and discard giving:
      abc*+
      stack only contains
      (
      carrying on until we reach 2nd right bracket, stack should contain:
      ^
      -
      (
      *
      +
      (
      /
      (
      dump stack upto topmost left bracket and discard giving us this much of the rpn:
      abc*+defgh^-
      stack now contains
      *
      +
      (
      /
      (
      parse 3rd right bracket so remove stack contents again upto topmost left bracket giving rpn of:
      abc*+defgh^-*+
      stack contains
      /
      (
      process final right bracket by removing upto topmost left bracket giving rpn of:
      abc*+defgh^-*+/
      stack now empty
      hope this helps

    • @XXxlightmarex
      @XXxlightmarex 7 ปีที่แล้ว

      Thank you very much, you've been a great help

  • @SuperJatinrajput
    @SuperJatinrajput 8 ปีที่แล้ว

    great video sir, make up for bunk classes.

    • @HurrayBanana
      @HurrayBanana  8 ปีที่แล้ว

      +Jatin Rajput To be fair nothing makes up for bunk'd classes, it's amazing the nuances and added extra's you'll get when someone goes through a topic with you. I always find that I talk about things I never planned.

  • @bryanarp19
    @bryanarp19 2 ปีที่แล้ว

    Saludos a la BUAP xd

  • @LucasAlfare
    @LucasAlfare 4 ปีที่แล้ว

    How to implement unaries using this?

    • @HurrayBanana
      @HurrayBanana  4 ปีที่แล้ว

      Higher precedence than multiply and divide but lower than exponent

  • @syntaxerorr
    @syntaxerorr 3 ปีที่แล้ว

    I can't take it that the K is really an R

  • @Shotpl0xGaming
    @Shotpl0xGaming 6 ปีที่แล้ว

    What happens whens 2 symbols hold the same line of precedence?

    • @HurrayBanana
      @HurrayBanana  6 ปีที่แล้ว +1

      Another burgermate you can only place an operator on top if it is higher so remove the item on top of stack snd write it down in the rp expression, then see if the new operator can now go on the stack (like normal)

    • @Shotpl0xGaming
      @Shotpl0xGaming 6 ปีที่แล้ว

      I see, thank you very much for this. Helped me out heaps. Was just doing past papers, came across this scenario again.

  • @marcinpozniak1605
    @marcinpozniak1605 2 ปีที่แล้ว

    6:32 why is the left bracket of higher precedence than the power of?

    • @HurrayBanana
      @HurrayBanana  2 ปีที่แล้ว

      Left brackets are always going to override precedence, it goes on top of everything, it's also there as a marker for when we encounter a right bracket while parsing, so we know when to stop pulling operators off the stack

    • @marcinpozniak1605
      @marcinpozniak1605 2 ปีที่แล้ว

      @@HurrayBanana okay, the drawing in the top right corner misled me. thank you

    • @HurrayBanana
      @HurrayBanana  2 ปีที่แล้ว

      The diagram shows that all operators go on top of left bracket if it seen on top of the stack. Parsing a left bracket in the expression doesn't look at precedence as you automatically push it onto the stack

  • @JoffreyB
    @JoffreyB 6 ปีที่แล้ว

    somebody add please subtitles

  • @mdy405
    @mdy405 9 ปีที่แล้ว

    why did you pop "*" before you push "/" if they are the same priority??? i dont really understand this!!

    • @HurrayBanana
      @HurrayBanana  9 ปีที่แล้ว +4

      You can only push onto the stack if the precedence is higher, if it is the same you must pop the stack until the operator is higher than the contents of the top

    • @mdy405
      @mdy405 9 ปีที่แล้ว

      HurrayBanana Thank you !

  • @messi-ih4uy
    @messi-ih4uy 5 ปีที่แล้ว

    keke do you love me

  • @saranshdhyani3164
    @saranshdhyani3164 6 ปีที่แล้ว

    Where is code?

    • @HurrayBanana
      @HurrayBanana  6 ปีที่แล้ว +1

      saransh dhyani this process described here is reasonably easy to code up if you parse the expression first to tokenise each component