From 6b38261f1d5d3a6cd17e2aa4836d3c15a7f08dcb Mon Sep 17 00:00:00 2001 From: guiribeirodev Date: Thu, 20 Jul 2023 00:40:14 -0300 Subject: [PATCH 1/6] =?UTF-8?q?Resolu=C3=A7=C3=A3o=20desafio=2014?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- desafio-14/javascript/.valid | 1 + desafio-14/javascript/README.md | 12 +++ desafio-14/javascript/index.js | 164 ++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 desafio-14/javascript/.valid create mode 100644 desafio-14/javascript/README.md create mode 100644 desafio-14/javascript/index.js diff --git a/desafio-14/javascript/.valid b/desafio-14/javascript/.valid new file mode 100644 index 000000000..84200d7d1 --- /dev/null +++ b/desafio-14/javascript/.valid @@ -0,0 +1 @@ +1caf27399c62528bd7452ad78f682c8cc \ No newline at end of file diff --git a/desafio-14/javascript/README.md b/desafio-14/javascript/README.md new file mode 100644 index 000000000..1caf1da18 --- /dev/null +++ b/desafio-14/javascript/README.md @@ -0,0 +1,12 @@ +# Versão do Node :package: + +Utilizado a versão mais recente LTS do Node (18.16.1). Porém deve funcionar desde a versão (14.15.0). + +# Como rodar o programa :wrench: + +Para rodar o programa basta digitar: + ``` + $ node index.js "filepath" + ``` + +No caso o filepath seria o arquivo com as expressões, ex "d14.txt". \ No newline at end of file diff --git a/desafio-14/javascript/index.js b/desafio-14/javascript/index.js new file mode 100644 index 000000000..c76eb06b8 --- /dev/null +++ b/desafio-14/javascript/index.js @@ -0,0 +1,164 @@ +const fs = require("fs"); + +function get(file) { + try { + const data = fs.readFileSync(file, "utf8").split("\n"); + const expressions = []; + + for (let expression of data) { + if (expression !== "") { + expression = expression.split(" ").join(""); + expressions.push(expression); + } + } + + return expressions; + } catch (err) { + console.error(err, "Read Failed"); + return err; + } +} + +function isValidExpression(expression) { + const haveParentheses = expression.includes("("); + + if (haveParentheses) { + expression = expression.split(""); + + const amountOpeningParenthesis = expression.reduce( + (count, char) => (char === "(" ? count + 1 : count), + 0 + ); + + const amountClosingParenthesis = expression.reduce( + (count, char) => (char === ")" ? count + 1 : count), + 0 + ); + + if (amountOpeningParenthesis !== amountClosingParenthesis) { + return false; + } + } + + for (let i = 0; i <= expression.length; i++) { + const operators = ["+", "-", "*", "/", "^"]; + + const currentChar = expression[i]; + const nextChar = expression[i + 1]; + + if (operators.includes(currentChar) && operators.includes(nextChar)) { + return false; + } + } + + return true; +} + +function infixToPostfix(expression) { + const precedence = { + "+": 0, + "-": 0, + "*": 1, + "/": 1, + "^": 2 + }; + + const result = []; + const stack = []; + + for (const token of expression) { + const isNumber = !isNaN(token); + + if (isNumber) { + result.push(token); + } else if (token === "(") { + stack.push(token); + } else if (token === ")") { + while (stack[stack.length - 1] !== "(") { + const lastOperator = stack.pop(); + + result.push(lastOperator); + } + + stack.pop(); + } else { + while (stack[stack.length - 1] && + precedence[token] <= precedence[stack[stack.length - 1]] + ) { + const lastOperator = stack.pop(); + + result.push(lastOperator); + } + stack.push(token); + } + } + + while (stack.length > 0) { + result.push(stack.pop()); + } + + return result; +} + +function solvePostfixExpression(expression) { + const stack = []; + + for (const token of expression) { + const isNumber = !isNaN(token); + + if (isNumber) { + stack.push(parseInt(token)); + } else { + const operand1 = stack.pop(); + const operand0 = stack.pop(); + + switch (token) { + case "+": + stack.push(operand0 + operand1); + break; + case "-": + stack.push(operand0 - operand1); + break; + case "*": + stack.push(operand0 * operand1); + break; + case "/": + if (operand1 === 0) { + return "ERR DIVBYZERO"; + } + stack.push(operand0 / operand1); + break; + case "^": + stack.push(operand0 ** operand1); + break; + } + } + } + const result = stack.pop(); + + return result; +} + +function main() { + const file = process.argv[2]; + const data = get(file); + + for (let expression of data) { + const isValid = isValidExpression(expression); + + if (!isValid) { + console.log("ERR SYNTAX"); + continue; + } + + expression = expression.split(/([()+\-/*^])/).filter(Boolean); + + const postfix = infixToPostfix(expression); + + const result = solvePostfixExpression(postfix); + + console.log(result); + } +} + +main(); \ No newline at end of file From 0245e88f0a34bfa3ae44d65c1dc7ed2b363e839b Mon Sep 17 00:00:00 2001 From: guiribeirodev Date: Thu, 20 Jul 2023 00:45:57 -0300 Subject: [PATCH 2/6] Fix directory --- desafio-14/guiribeirodev/javascript/.valid | 1 + desafio-14/guiribeirodev/javascript/README.md | 12 ++ desafio-14/guiribeirodev/javascript/index.js | 164 ++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 desafio-14/guiribeirodev/javascript/.valid create mode 100644 desafio-14/guiribeirodev/javascript/README.md create mode 100644 desafio-14/guiribeirodev/javascript/index.js diff --git a/desafio-14/guiribeirodev/javascript/.valid b/desafio-14/guiribeirodev/javascript/.valid new file mode 100644 index 000000000..84200d7d1 --- /dev/null +++ b/desafio-14/guiribeirodev/javascript/.valid @@ -0,0 +1 @@ +1caf27399c62528bd7452ad78f682c8cc \ No newline at end of file diff --git a/desafio-14/guiribeirodev/javascript/README.md b/desafio-14/guiribeirodev/javascript/README.md new file mode 100644 index 000000000..1caf1da18 --- /dev/null +++ b/desafio-14/guiribeirodev/javascript/README.md @@ -0,0 +1,12 @@ +# Versão do Node :package: + +Utilizado a versão mais recente LTS do Node (18.16.1). Porém deve funcionar desde a versão (14.15.0). + +# Como rodar o programa :wrench: + +Para rodar o programa basta digitar: + ``` + $ node index.js "filepath" + ``` + +No caso o filepath seria o arquivo com as expressões, ex "d14.txt". \ No newline at end of file diff --git a/desafio-14/guiribeirodev/javascript/index.js b/desafio-14/guiribeirodev/javascript/index.js new file mode 100644 index 000000000..c76eb06b8 --- /dev/null +++ b/desafio-14/guiribeirodev/javascript/index.js @@ -0,0 +1,164 @@ +const fs = require("fs"); + +function get(file) { + try { + const data = fs.readFileSync(file, "utf8").split("\n"); + const expressions = []; + + for (let expression of data) { + if (expression !== "") { + expression = expression.split(" ").join(""); + expressions.push(expression); + } + } + + return expressions; + } catch (err) { + console.error(err, "Read Failed"); + return err; + } +} + +function isValidExpression(expression) { + const haveParentheses = expression.includes("("); + + if (haveParentheses) { + expression = expression.split(""); + + const amountOpeningParenthesis = expression.reduce( + (count, char) => (char === "(" ? count + 1 : count), + 0 + ); + + const amountClosingParenthesis = expression.reduce( + (count, char) => (char === ")" ? count + 1 : count), + 0 + ); + + if (amountOpeningParenthesis !== amountClosingParenthesis) { + return false; + } + } + + for (let i = 0; i <= expression.length; i++) { + const operators = ["+", "-", "*", "/", "^"]; + + const currentChar = expression[i]; + const nextChar = expression[i + 1]; + + if (operators.includes(currentChar) && operators.includes(nextChar)) { + return false; + } + } + + return true; +} + +function infixToPostfix(expression) { + const precedence = { + "+": 0, + "-": 0, + "*": 1, + "/": 1, + "^": 2 + }; + + const result = []; + const stack = []; + + for (const token of expression) { + const isNumber = !isNaN(token); + + if (isNumber) { + result.push(token); + } else if (token === "(") { + stack.push(token); + } else if (token === ")") { + while (stack[stack.length - 1] !== "(") { + const lastOperator = stack.pop(); + + result.push(lastOperator); + } + + stack.pop(); + } else { + while (stack[stack.length - 1] && + precedence[token] <= precedence[stack[stack.length - 1]] + ) { + const lastOperator = stack.pop(); + + result.push(lastOperator); + } + stack.push(token); + } + } + + while (stack.length > 0) { + result.push(stack.pop()); + } + + return result; +} + +function solvePostfixExpression(expression) { + const stack = []; + + for (const token of expression) { + const isNumber = !isNaN(token); + + if (isNumber) { + stack.push(parseInt(token)); + } else { + const operand1 = stack.pop(); + const operand0 = stack.pop(); + + switch (token) { + case "+": + stack.push(operand0 + operand1); + break; + case "-": + stack.push(operand0 - operand1); + break; + case "*": + stack.push(operand0 * operand1); + break; + case "/": + if (operand1 === 0) { + return "ERR DIVBYZERO"; + } + stack.push(operand0 / operand1); + break; + case "^": + stack.push(operand0 ** operand1); + break; + } + } + } + const result = stack.pop(); + + return result; +} + +function main() { + const file = process.argv[2]; + const data = get(file); + + for (let expression of data) { + const isValid = isValidExpression(expression); + + if (!isValid) { + console.log("ERR SYNTAX"); + continue; + } + + expression = expression.split(/([()+\-/*^])/).filter(Boolean); + + const postfix = infixToPostfix(expression); + + const result = solvePostfixExpression(postfix); + + console.log(result); + } +} + +main(); \ No newline at end of file From 1fa49ce9a8dedcd99b5809ec1f828b0d38bf589e Mon Sep 17 00:00:00 2001 From: Guilherme Ribeiro Date: Thu, 20 Jul 2023 00:47:25 -0300 Subject: [PATCH 3/6] Delete .valid --- desafio-14/javascript/.valid | 1 - 1 file changed, 1 deletion(-) delete mode 100644 desafio-14/javascript/.valid diff --git a/desafio-14/javascript/.valid b/desafio-14/javascript/.valid deleted file mode 100644 index 84200d7d1..000000000 --- a/desafio-14/javascript/.valid +++ /dev/null @@ -1 +0,0 @@ -1caf27399c62528bd7452ad78f682c8cc \ No newline at end of file From 6782c7afb78da06e87d25e1a005497d31577ef88 Mon Sep 17 00:00:00 2001 From: Guilherme Ribeiro Date: Thu, 20 Jul 2023 00:48:14 -0300 Subject: [PATCH 4/6] Delete README.md --- desafio-14/javascript/README.md | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 desafio-14/javascript/README.md diff --git a/desafio-14/javascript/README.md b/desafio-14/javascript/README.md deleted file mode 100644 index 1caf1da18..000000000 --- a/desafio-14/javascript/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Versão do Node :package: - -Utilizado a versão mais recente LTS do Node (18.16.1). Porém deve funcionar desde a versão (14.15.0). - -# Como rodar o programa :wrench: - -Para rodar o programa basta digitar: - ``` - $ node index.js "filepath" - ``` - -No caso o filepath seria o arquivo com as expressões, ex "d14.txt". \ No newline at end of file From 2da3bbb8b91c2d10fb772c4c31dbbfba185e60fb Mon Sep 17 00:00:00 2001 From: Guilherme Ribeiro Date: Thu, 20 Jul 2023 00:48:22 -0300 Subject: [PATCH 5/6] Delete index.js --- desafio-14/javascript/index.js | 164 --------------------------------- 1 file changed, 164 deletions(-) delete mode 100644 desafio-14/javascript/index.js diff --git a/desafio-14/javascript/index.js b/desafio-14/javascript/index.js deleted file mode 100644 index c76eb06b8..000000000 --- a/desafio-14/javascript/index.js +++ /dev/null @@ -1,164 +0,0 @@ -const fs = require("fs"); - -function get(file) { - try { - const data = fs.readFileSync(file, "utf8").split("\n"); - const expressions = []; - - for (let expression of data) { - if (expression !== "") { - expression = expression.split(" ").join(""); - expressions.push(expression); - } - } - - return expressions; - } catch (err) { - console.error(err, "Read Failed"); - return err; - } -} - -function isValidExpression(expression) { - const haveParentheses = expression.includes("("); - - if (haveParentheses) { - expression = expression.split(""); - - const amountOpeningParenthesis = expression.reduce( - (count, char) => (char === "(" ? count + 1 : count), - 0 - ); - - const amountClosingParenthesis = expression.reduce( - (count, char) => (char === ")" ? count + 1 : count), - 0 - ); - - if (amountOpeningParenthesis !== amountClosingParenthesis) { - return false; - } - } - - for (let i = 0; i <= expression.length; i++) { - const operators = ["+", "-", "*", "/", "^"]; - - const currentChar = expression[i]; - const nextChar = expression[i + 1]; - - if (operators.includes(currentChar) && operators.includes(nextChar)) { - return false; - } - } - - return true; -} - -function infixToPostfix(expression) { - const precedence = { - "+": 0, - "-": 0, - "*": 1, - "/": 1, - "^": 2 - }; - - const result = []; - const stack = []; - - for (const token of expression) { - const isNumber = !isNaN(token); - - if (isNumber) { - result.push(token); - } else if (token === "(") { - stack.push(token); - } else if (token === ")") { - while (stack[stack.length - 1] !== "(") { - const lastOperator = stack.pop(); - - result.push(lastOperator); - } - - stack.pop(); - } else { - while (stack[stack.length - 1] && - precedence[token] <= precedence[stack[stack.length - 1]] - ) { - const lastOperator = stack.pop(); - - result.push(lastOperator); - } - stack.push(token); - } - } - - while (stack.length > 0) { - result.push(stack.pop()); - } - - return result; -} - -function solvePostfixExpression(expression) { - const stack = []; - - for (const token of expression) { - const isNumber = !isNaN(token); - - if (isNumber) { - stack.push(parseInt(token)); - } else { - const operand1 = stack.pop(); - const operand0 = stack.pop(); - - switch (token) { - case "+": - stack.push(operand0 + operand1); - break; - case "-": - stack.push(operand0 - operand1); - break; - case "*": - stack.push(operand0 * operand1); - break; - case "/": - if (operand1 === 0) { - return "ERR DIVBYZERO"; - } - stack.push(operand0 / operand1); - break; - case "^": - stack.push(operand0 ** operand1); - break; - } - } - } - const result = stack.pop(); - - return result; -} - -function main() { - const file = process.argv[2]; - const data = get(file); - - for (let expression of data) { - const isValid = isValidExpression(expression); - - if (!isValid) { - console.log("ERR SYNTAX"); - continue; - } - - expression = expression.split(/([()+\-/*^])/).filter(Boolean); - - const postfix = infixToPostfix(expression); - - const result = solvePostfixExpression(postfix); - - console.log(result); - } -} - -main(); \ No newline at end of file From b5248ecd268297b9abfcba0679a54bb3d79bfa63 Mon Sep 17 00:00:00 2001 From: Guilherme Ribeiro Date: Thu, 20 Jul 2023 00:52:08 -0300 Subject: [PATCH 6/6] Fix end line --- desafio-14/guiribeirodev/javascript/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desafio-14/guiribeirodev/javascript/index.js b/desafio-14/guiribeirodev/javascript/index.js index c76eb06b8..990d4c145 100644 --- a/desafio-14/guiribeirodev/javascript/index.js +++ b/desafio-14/guiribeirodev/javascript/index.js @@ -161,4 +161,4 @@ function main() { } } -main(); \ No newline at end of file +main();