From ba3cf0ea2ae362aadf4bb82e18802c6fb7816923 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 16:16:47 +0530 Subject: [PATCH 1/8] add RPN snippets --- snippets/infixToPostFix.md | 24 ++++++++++++++++++++++++ snippets/postfixToInfix.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 snippets/infixToPostFix.md create mode 100644 snippets/postfixToInfix.md diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md new file mode 100644 index 000000000..3fd859e37 --- /dev/null +++ b/snippets/infixToPostFix.md @@ -0,0 +1,24 @@ +const infix = expr => { + let rpn = '' + let solve = expr.replace(/\^/g,'**').match(/([0-9]+|[\+\(\)\/\-]|\*+)/g).filter(el => !/\s+/.test(el) && el !== '') + let stack = [] + let precedence = {'**':5,'/':4,'*':4,'+':3,'-':3} + solve.forEach(symbol => { + if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ + rpn += symbol + ' ' + } + else if (Object.keys(precedence).includes(symbol)) { + while(((precedence[symbol] < precedence[stack[stack.length-1]]) || ((precedence[symbol] == precedence[stack[stack.length-1]]) && symbol !== "**"))&&(stack[stack.length - 1] !== '(')) { + rpn += stack.pop() + ' ' + } + stack.push(symbol) + } + else if(symbol === '('){stack.push(symbol)} + else if(symbol === ')'){while (stack[stack.length - 1] !== '('){rpn += stack.pop() + ' '; if (stack.length === 0){throw `Mismatched parantheses`}} stack.pop()} + else {throw `${symbol} is not a recognized symbol`} + }) + while(stack.length !== 0){ + rpn += stack.pop() + ' ' + } + return rpn +} diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md new file mode 100644 index 000000000..c54bd44f0 --- /dev/null +++ b/snippets/postfixToInfix.md @@ -0,0 +1,29 @@ +const postFix = RPN => { + let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') + let stack = [] + let result = [] + let friends = {"+" : ["+","-","*","/"],"-":[],"/":["*"],"*":["/","*"],"**":["+","-","*","/"]} + convert.forEach(symbol => { + if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ + result.push(symbol) + } + else if (Object.keys(friends).includes(symbol)) { + a = result.pop() + b = result.pop() + if(stack.length !==0){ + if(friends[symbol].includes(stack.pop())){ + result.push(`${b} ${symbol} ${a}`) + stack.push(symbol) + } + else{ + result.push(`(${b}) ${symbol} ${a}`) + stack.push(symbol) + } + } + else {result.push(`${b} ${symbol} ${a}`);stack.push(symbol)} + } + else throw `${symbol} is not a recognized symbol` + }) + if(result.length === 1) return result.pop() + else throw `${RPN} is not a correct RPN` +} From 78c29a1ddb76b7eb76536b0c5c15d31e66193a02 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 16:39:26 +0530 Subject: [PATCH 2/8] fixed typos --- snippets/RPNSolver.md | 24 ++++++++++++++++++++++++ snippets/infixToPostFix.md | 11 ++++++++++- snippets/postfixToInfix.md | 11 ++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 snippets/RPNSolver.md diff --git a/snippets/RPNSolver.md b/snippets/RPNSolver.md new file mode 100644 index 000000000..2cfb59be5 --- /dev/null +++ b/snippets/RPNSolver.md @@ -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 + +``` diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md index 3fd859e37..b05496977 100644 --- a/snippets/infixToPostFix.md +++ b/snippets/infixToPostFix.md @@ -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 + +``` diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md index c54bd44f0..19e191c6b 100644 --- a/snippets/postfixToInfix.md +++ b/snippets/postfixToInfix.md @@ -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 + +``` From a5fc517f2b732c2a492c6af89be6488ceee0cbd2 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 16:40:19 +0530 Subject: [PATCH 3/8] fixed typos --- snippets/RPNSolver.md | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 snippets/RPNSolver.md diff --git a/snippets/RPNSolver.md b/snippets/RPNSolver.md deleted file mode 100644 index 2cfb59be5..000000000 --- a/snippets/RPNSolver.md +++ /dev/null @@ -1,24 +0,0 @@ -### 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 - -``` From abbfd00707e6a5c26cd0b5438af4d5d988975889 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Tue, 2 Jan 2018 21:14:44 +0530 Subject: [PATCH 4/8] fixed typos --- snippets/infixToPostFix.md | 2 +- snippets/postfixToInfix.md | 37 ++++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md index b05496977..84371ddbc 100644 --- a/snippets/infixToPostFix.md +++ b/snippets/infixToPostFix.md @@ -1,6 +1,6 @@ ### infixToPostfix -Works perfectly! +Works perfectly! ```js const infixToPostfix = expr => { diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md index 19e191c6b..52d1a89bb 100644 --- a/snippets/postfixToInfix.md +++ b/snippets/postfixToInfix.md @@ -1,32 +1,35 @@ ### postfixToInfix - This one has few errors and does not work properply -```js +``` js const postfixToInfix = RPN => { let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') let stack = [] let result = [] - let friends = {"+" : ["+","-","*","/"],"-":[],"/":["*"],"*":["/","*"],"**":["+","-","*","/"]} + let precedence = {null : 4 ,'**':3 ,'/' : 2,'*': 2,'+':1,'-':1 } convert.forEach(symbol => { + let stra,strb if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ result.push(symbol) + stack.push(null) } - else if (Object.keys(friends).includes(symbol)) { - a = result.pop() - b = result.pop() - if(stack.length !==0){ - if(friends[symbol].includes(stack.pop())){ - result.push(`${b} ${symbol} ${a}`) - stack.push(symbol) - } - else{ - result.push(`(${b}) ${symbol} ${a}`) - stack.push(symbol) - } + else if (Object.keys(precedence).includes(symbol)) { + let [a,b,opa,opb] = [result.pop(),result.pop(),stack.pop(),stack.pop()] + if(precedence[opb] < precedence[symbol]) { + strb = `(${b})` } - else {result.push(`${b} ${symbol} ${a}`);stack.push(symbol)} - } + else{ + strb = `${b}` + } + if((precedence[opa] < precedence[symbol]) || ((precedence[opa] === precedence[symbol]) && ["/","-"].includes(symbol) )){ + stra = `(${a})` + } + else { + stra = `${a}` + } + result.push(strb +symbol + stra) + stack.push(symbol) + } else throw `${symbol} is not a recognized symbol` }) if(result.length === 1) return result.pop() From b3f6f8e7e630bb01dee4ce229aa125184d0659bf Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 3 Jan 2018 08:48:29 +0530 Subject: [PATCH 5/8] add minN and maxN --- snippets/maxN.md | 19 +++++++++++++++++++ snippets/minN.md | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 snippets/maxN.md create mode 100644 snippets/minN.md diff --git a/snippets/maxN.md b/snippets/maxN.md new file mode 100644 index 000000000..8efc46112 --- /dev/null +++ b/snippets/maxN.md @@ -0,0 +1,19 @@ +### maxN + +Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order). + +Sort's the array's shallow copy in descending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +``` js +const maxN = (arr,n = 1) => { + let rra = [...arr].sort((a,b) => b-a) + return rra.splice(0,n) +} +``` + +``` js +maxN([1,2,3]); // [3] +maxN([1,2,3],2); // [3,2] +maxN([1,2,3],4); // [3,2,1] +``` diff --git a/snippets/minN.md b/snippets/minN.md new file mode 100644 index 000000000..0f40f1d36 --- /dev/null +++ b/snippets/minN.md @@ -0,0 +1,18 @@ +### minN + +Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order). + +Sort's the array's shallow copy in ascending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +``` js +const minN = (arr,n = 1) => { + let rra = [...arr].sort((a,b) => a-b) + return rra.splice(0,n) +} +``` +``` js +maxN([1,2,3]); // [1] +maxN([1,2,3],2); // [1,2] +maxN([1,2,3],4); // [1,2,3] +``` From 94028604f9f209b74010873aed7bb0f8c957e27f Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 3 Jan 2018 08:51:23 +0530 Subject: [PATCH 6/8] add minN and maxN --- snippets/infixToPostFix.md | 33 ------------------------------ snippets/postfixToInfix.md | 41 -------------------------------------- 2 files changed, 74 deletions(-) delete mode 100644 snippets/infixToPostFix.md delete mode 100644 snippets/postfixToInfix.md diff --git a/snippets/infixToPostFix.md b/snippets/infixToPostFix.md deleted file mode 100644 index 84371ddbc..000000000 --- a/snippets/infixToPostFix.md +++ /dev/null @@ -1,33 +0,0 @@ -### 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 = [] - let precedence = {'**':5,'/':4,'*':4,'+':3,'-':3} - solve.forEach(symbol => { - if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ - rpn += symbol + ' ' - } - else if (Object.keys(precedence).includes(symbol)) { - while(((precedence[symbol] < precedence[stack[stack.length-1]]) || ((precedence[symbol] == precedence[stack[stack.length-1]]) && symbol !== "**"))&&(stack[stack.length - 1] !== '(')) { - rpn += stack.pop() + ' ' - } - stack.push(symbol) - } - else if(symbol === '('){stack.push(symbol)} - else if(symbol === ')'){while (stack[stack.length - 1] !== '('){rpn += stack.pop() + ' '; if (stack.length === 0){throw `Mismatched parantheses`}} stack.pop()} - else {throw `${symbol} is not a recognized symbol`} - }) - while(stack.length !== 0){ - rpn += stack.pop() + ' ' - } - return rpn -} -``` -```js - -``` diff --git a/snippets/postfixToInfix.md b/snippets/postfixToInfix.md deleted file mode 100644 index 52d1a89bb..000000000 --- a/snippets/postfixToInfix.md +++ /dev/null @@ -1,41 +0,0 @@ -### postfixToInfix - - -``` js -const postfixToInfix = RPN => { - let convert = RPN.replace(/\^/g,'**').split(/\s+/g).filter(el => !/\s+/.test(el) && el !== '') - let stack = [] - let result = [] - let precedence = {null : 4 ,'**':3 ,'/' : 2,'*': 2,'+':1,'-':1 } - convert.forEach(symbol => { - let stra,strb - if(!isNaN(parseFloat(symbol)) && isFinite(symbol)){ - result.push(symbol) - stack.push(null) - } - else if (Object.keys(precedence).includes(symbol)) { - let [a,b,opa,opb] = [result.pop(),result.pop(),stack.pop(),stack.pop()] - if(precedence[opb] < precedence[symbol]) { - strb = `(${b})` - } - else{ - strb = `${b}` - } - if((precedence[opa] < precedence[symbol]) || ((precedence[opa] === precedence[symbol]) && ["/","-"].includes(symbol) )){ - stra = `(${a})` - } - else { - stra = `${a}` - } - result.push(strb +symbol + stra) - stack.push(symbol) - } - else throw `${symbol} is not a recognized symbol` - }) - if(result.length === 1) return result.pop() - else throw `${RPN} is not a correct RPN` -} -``` -```js - -``` From bfb21ff71a3c950ddca6013c65e5c003d05e55f3 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 11:39:40 +0530 Subject: [PATCH 7/8] Update maxN.md --- snippets/maxN.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/maxN.md b/snippets/maxN.md index 8efc46112..e777b1949 100644 --- a/snippets/maxN.md +++ b/snippets/maxN.md @@ -6,10 +6,7 @@ Sort's the array's shallow copy in descending order and returns the first n elem Skip the second argument to get a single element(in the form of a array) ``` js -const maxN = (arr,n = 1) => { - let rra = [...arr].sort((a,b) => b-a) - return rra.splice(0,n) -} +const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); ``` ``` js From 231ff2331bc7c0966965c17b83d37af4bb9b0f2a Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 11:40:39 +0530 Subject: [PATCH 8/8] Update minN.md --- snippets/minN.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/snippets/minN.md b/snippets/minN.md index 0f40f1d36..d3a7bd13f 100644 --- a/snippets/minN.md +++ b/snippets/minN.md @@ -6,13 +6,11 @@ Sort's the array's shallow copy in ascending order and returns the first n eleme Skip the second argument to get a single element(in the form of a array) ``` js -const minN = (arr,n = 1) => { - let rra = [...arr].sort((a,b) => a-b) - return rra.splice(0,n) -} +const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n); + ``` ``` js -maxN([1,2,3]); // [1] -maxN([1,2,3],2); // [1,2] -maxN([1,2,3],4); // [1,2,3] +minN([1,2,3]); // [1] +minN([1,2,3],2); // [1,2] +minN([1,2,3],4); // [1,2,3] ```