fixed typos

This commit is contained in:
Rohit Tanwar
2018-01-02 16:39:26 +05:30
parent f5ba3f6146
commit d69598f36a
3 changed files with 44 additions and 2 deletions

24
snippets/RPNSolver.md Normal file
View File

@ -0,0 +1,24 @@
### RPNSolver
Solves the given reverse polish notation
``` js
const RPNSolver = RPN => {
const operators = {'*' : (a,b) => a * b, '+' : (a,b) => a + b, '-' : (a,b) => a - b, '/' : (a,b) => a / b, '**': (a,b) => a ** b}
let [stack,solve] = [[],RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')]
solve.forEach(symbol => {
if(!isNaN(parseFloat(symbol)) && isFinite(symbol)) {stack.push(symbol)}
else if (Object.keys(operators).includes(symbol)) {
let [a,b] = [stack.pop(),stack.pop()]
stack.push(operators[symbol](parseFloat(b),parseFloat(a)))
} else { throw `${symbol} is not a recognized symbol` }
}
)
if(stack.length === 1) return stack.pop()
else throw `${RPN} is not a proper RPN. Please check it and try again`
}
```
```js
```

View File

@ -1,4 +1,9 @@
const infix = expr => {
### infixToPostfix
Works perfectly!
```js
const infixToPostfix = expr => {
let rpn = ''
let solve = expr.replace(/\^/g,'**').match(/([0-9]+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '')
let stack = []
@ -22,3 +27,7 @@ const infix = expr => {
}
return rpn
}
```
```js
```

View File

@ -1,4 +1,9 @@
const postFix = RPN => {
### postfixToInfix
This one has few errors and does not work properply
```js
const postfixToInfix = RPN => {
let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '')
let stack = []
let result = []
@ -27,3 +32,7 @@ const postFix = RPN => {
if(result.length === 1) return result.pop()
else throw `${RPN} is not a correct RPN`
}
```
```js
```