From e91751a4ce721037272f1847f2d2431c2b3417fc Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Wed, 13 Apr 2022 04:55:09 +0530 Subject: [PATCH] add postfix solns --- .../README.md | 39 +++++++++++++++ .../python3/solution.py | 24 +++++++++ .../README.md | 17 +++++++ .../python3/solution.py | 49 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 0150_evaluate-reverse-polish-notation/README.md create mode 100644 0150_evaluate-reverse-polish-notation/python3/solution.py create mode 100644 lintcode/271-prefix-notation-to-postfix-notation/README.md create mode 100644 lintcode/271-prefix-notation-to-postfix-notation/python3/solution.py diff --git a/0150_evaluate-reverse-polish-notation/README.md b/0150_evaluate-reverse-polish-notation/README.md new file mode 100644 index 0000000..b58d2c7 --- /dev/null +++ b/0150_evaluate-reverse-polish-notation/README.md @@ -0,0 +1,39 @@ +Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/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]`. \ No newline at end of file diff --git a/0150_evaluate-reverse-polish-notation/python3/solution.py b/0150_evaluate-reverse-polish-notation/python3/solution.py new file mode 100644 index 0000000..ea72e91 --- /dev/null +++ b/0150_evaluate-reverse-polish-notation/python3/solution.py @@ -0,0 +1,24 @@ +import math +from collections import deque + +class Solution: + def evalRPN(self, tokens: List[str]) -> int: + compute = { + '+': lambda a, b: a + b, + '-': lambda a, b: a - b, + '*': lambda a, b: a * b, + '/': lambda a, b: math.trunc(a / b) + } + + stack = deque() + + for token in tokens: + if token.lstrip('-').isdigit(): + stack.append(int(token)) + else: + right = stack.pop() + left = stack.pop() + + stack.append(compute[token](left, right)) + + return stack[-1] diff --git a/lintcode/271-prefix-notation-to-postfix-notation/README.md b/lintcode/271-prefix-notation-to-postfix-notation/README.md new file mode 100644 index 0000000..88267f0 --- /dev/null +++ b/lintcode/271-prefix-notation-to-postfix-notation/README.md @@ -0,0 +1,17 @@ +Given a string array representing an expression, and return the Reverse Polish notation of this expression. (remove the parentheses) + +Definition of _Reverse Polish Notation_: + +* [https://en.wikipedia.org/wiki/Reverse\_Polish\_notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) + +Example + +Example 1: + + Input: ["3", "-", "4", "+", "5"] Output: ["3", "4", "-", "5", "+"] Explanation: 3 - 4 + 5 = -1 + 5 = 4 3 4 - 5 + = -1 5 + = 4 + + + +Example 2: + + Input: ["(", "5", "-", "6", ")", "*", "7"] Output: ["5","6","-","7","*"] Explanation: (5 - 6) * 7 = -1 * 7 = -7 5 6 - 7 * = -1 7 * = -7 diff --git a/lintcode/271-prefix-notation-to-postfix-notation/python3/solution.py b/lintcode/271-prefix-notation-to-postfix-notation/python3/solution.py new file mode 100644 index 0000000..b86ec80 --- /dev/null +++ b/lintcode/271-prefix-notation-to-postfix-notation/python3/solution.py @@ -0,0 +1,49 @@ +from typing import ( + List, +) + +from collections import deque + +class Solution: + """ + @param expression: A string array + @return: The Reverse Polish notation of this expression + """ + def convert_to_r_p_n(self, expression: List[str]) -> List[str]: + op_priority = { + '/': 1 << 4, + '*': 1 << 4, + '+': 1 << 2, + '-': 1 << 2, + '(': 1 << 0 + } + + op_stack = deque() + result = [] + + for token in expression: + if token.isdigit(): + result.append(token) + + elif token == '(': + op_stack.append(token) + + elif token == ')': + while len(op_stack) > 0 and op_stack[-1] != '(': + result.append(op_stack.pop()) + + op_stack.pop() + + else: + # For any other operator, keep popping and appending to results as long as there + # are higher priority operators in the stack + while len(op_stack) > 0 and op_priority[op_stack[-1]] >= op_priority[token]: + result.append(op_stack.pop()) + + op_stack.append(token) + + # Pop and append the remaining operators in the stack + while len(op_stack) > 0: + result.append(op_stack.pop()) + + return result