Last active
April 21, 2021 14:55
-
-
Save Daymannovaes/60a140758ce2a6047b17ea70bae1a672 to your computer and use it in GitHub Desktop.
Equation Parser and Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash'); | |
function getEquationVariables(equation) { | |
variables = equation.match(/[a-z]/g); | |
return _.chain(variables).compact().uniq().value(); | |
} | |
function getAllVariables(equations) { | |
return _.chain(equations) | |
.map(getEquationVariables) | |
.flatten() | |
.uniq() | |
.value(); | |
} | |
function invertTerm(term) { | |
const NEGATIVE_TERM_REGEX = /^-/; | |
return term.match(NEGATIVE_TERM_REGEX) | |
? term.replace(NEGATIVE_TERM_REGEX, '') | |
: ( | |
term === '0' ? term : `-${term}` | |
); | |
} | |
function matchEquationToZero(equation) { | |
const parts = splitEquationInTwoParts(isZeroLeft(equation) ? invertZero(equation) : equation); | |
const leftTerms = removeMinusSign(parts[0]); | |
const rightTerms = removeMinusSign(parts[1]); | |
const zeroEq = leftTerms.concat(rightTerms.map(invertTerm)); | |
return zeroEq[zeroEq.length -1].match(/[a-z]/) ? zeroEq.concat('0') : zeroEq; | |
} | |
function extractCoeficient(part) { | |
const coeficient = part.match(/(-?[\d\.]*)/); | |
if(!coeficient || !coeficient[0]) return 1; | |
if(coeficient[0] === '-') return -1; | |
return parseFloat(coeficient[0], 10); | |
} | |
function extractCoeficients(parts) { | |
return parts.map(extractCoeficient); | |
} | |
function removeMinusSign(part) { | |
const terms = part.split(' '); | |
return terms.map((term, i) => isMinus(term) | |
? '+' | |
: ( | |
isMinus(terms[i - 1]) | |
? invertTerm(term) | |
: term | |
) | |
).filter(term => term !== '+'); | |
} | |
const isMinus = term => term === '-'; | |
function isEquationEqualsZero(equation) { | |
const parts = splitEquationInTwoParts(equation); | |
return parts[0] === '0' || parts[1] === '0'; | |
} | |
function isZeroLeft(equation) { | |
const parts = splitEquationInTwoParts(equation); | |
return parts[0] === '0'; | |
} | |
function invertZero(equation) { | |
const parts = splitEquationInTwoParts(equation); | |
return `${parts[1]} = ${parts[0]}`; | |
} | |
function convertEquationToCoeficients(equation) { | |
const eq = simplifyEquation(equation); | |
const terms = matchEquationToZero(eq); | |
return extractCoeficients(terms); | |
} | |
const splitEquationInTwoParts = equation => equation.replace(/ ?= ?/, '=').split('='); | |
function simplifyEquation(equation) { | |
return equation.toLowerCase() | |
.replace(/\+/g, ' + ') | |
.replace(/-/g, ' - ') | |
.replace(/=/g, ' = ') | |
.replace(/ +/g, ' ') // trailing spaces | |
.replace(/^ /g, '') // space in beggining | |
.replace(/ $/g, '') // space in end | |
.replace(/^\+ /, '') // remove + from beggining | |
} | |
module.exports = { | |
getEquationVariables, | |
getAllVariables, | |
simplifyEquation, | |
invertTerm, | |
isEquationEqualsZero, | |
matchEquationToZero, | |
removeMinusSign, | |
extractCoeficient, | |
extractCoeficients, | |
convertEquationToCoeficients, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment