Back to writing

Evaluate Reverse Polish Notation


Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

Note that division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

 

Example 1:

Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

 

Constraints:

  • 1 <= tokens.length <= 104
  • tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].



class Solution:

    def  result(self, n1, n2, op):
        if op == '+':
            return n1+n2
        elif op == '-':
            return n1-n2
        elif op == '*':
            return n1*n2
        else:
            return int(n1 / n2)

    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        operators = ('+', '-', '*', '/')
        r = None

        for item in tokens:
            if item not in operators:
                stack.append(item)
            else:
                val2 = stack.pop()
                val1 = stack.pop()

                r = self.result(int(val1), int(val2), item)
                #print(r)
                #print(val1, item, val2, r)
                stack.append(r)

        return int(stack.pop())

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀:

  1. Scalability: https://lnkd.in/gpge_z76
  2. Latency vs Throughput: https://lnkd.in/g_amhAtN
  3. CAP Theorem: https://lnkd.in/g3hmVamx
  4. ACID Transactions: https://lnkd.in/gMe2JqaF
  5. Rate Limiting: https://lnkd.in/gWsTDR3m
  6. API Design: https://lnkd.in/ghYzrr8q
  7. Strong vs Eventual Consistency: https://lnkd.in/gJ-uXQXZ
  8. Distributed Tracing: https://lnkd.in/d6r5RdXG
  9. Sync vs Async Communication: https://lnkd.in/gC3F2nvr
  10. Batch vs Stream Processing: https://lnkd.in/g4_MzM4s
  11. Fault Tolerance: https://lnkd.in/dVJ6n3wA

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗕𝗹𝗼𝗰𝗸𝘀:

  1. Database: https://lnkd.in/gti8gjpz
  2. Horizontal vs Vertical Scaling: https://lnkd.in/gAH2e9du
  3. Caching: https://lnkd.in/gC9piQbJ
  4. Distributed Caching: https://lnkd.in/g7WKydNg
  5. Load Balancing: https://lnkd.in/gQaa8sXK
  6. SQL vs NoSQL: https://lnkd.in/g3WC_yxn
  7. Database Scaling: https://lnkd.in/gAXpSyWQ
  8. Data Replication: https://lnkd.in/gVAJxTpS
  9. Data Redundancy: https://lnkd.in/gNN7TF7n
  10. Database Sharding: https://lnkd.in/gMqqc6x9
  11. Database Indexes: https://lnkd.in/gCeshYVt
  12. Proxy Server: https://lnkd.in/gi8KnKS6
  13. WebSocket: https://lnkd.in/g76Gv2KQ
  14. API Gateway: https://lnkd.in/gnsJGJaM
  15. Message Queues: https://lnkd.in/gTzY6uk8

𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀:

  1. Event-Driven Architecture: https://lnkd.in/dp8CPvey
  2. Client-Server Architecture: https://lnkd.in/dAARQYzq
  3. Serverless Architecture: https://lnkd.in/gQNAXKkb
  4. Microservices Architecture: https://lnkd.in/gFXUrz_T

𝗟𝗼𝘄-𝗟𝗲𝘃𝗲𝗹 𝗗𝗲𝘀𝗶𝗴𝗻 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀:

  1. Design Parking Lot: https://lnkd.in/dQaAuFd2
  2. Design Splitwise: https://lnkd.in/dF5fBnex
  3. Design Chess Validator: https://lnkd.in/dfAQHvN4
  4. Design Distributed Queue | Kafka: https://lnkd.in/dQ6_B4_M

Share with someone who might need this.

FAANGTips