Given a input of an algebraic term, I am trying to get the coefficients of the variables. The only operators in the input is + -
and only one variable.
Examples:
2x^2+3x+4 => [ 2, 3, 4 ]
3-x => [ -1, 3 ]
x^2+x => [ 1, 1, 0 ]
x+x^3 => [ 1, 0, 1, 0 ]
Invalid Input:
2x^2+2x^2
This is my first attempt at it:
var str = " 2x^4-1+9x^3-100x^2";
function getCoeff(term) {
var nterm = (term.replace(/[^0-9|-]x(?!\^)/g,"1x")).replace(/[^0-9|\+]x(?!\^)/g,"-1x"); // ==> Replace ‘-/x’ with ‘-/1x’
for ( var i = 0; i < 10; i++ ) { // ==> Loop true the regexs to replace all ‘x^n’ to ‘1x^n’
var re = new RegExp('[^0-9|\-]x\\^' + i); // ==> Regex for x^n
var re2 = new RegExp('[^0-9|]x\\^' + i); // ==> Regex for -x^n
nterm = (nterm.replace(re,"1x^" + i)).replace(re2,"-1x^" + i); }
for ( var m = 10; m > 1; m-- ) { // ==> Get the coefficients of ax^n in descending order
var re3 = new RegExp('\\W?\\d+(?=x\\^' + m + ')' );
if ( nterm.match(re3) === null ) {
var result = "";
} else {
result += ((nterm.match(re3)+', ').toString()).replace(/\+/g,""); }}
if ( nterm.match(/\W?\d+(?=x(?!\^))/g) === null ) { // Regex for coefficient x
var result2 = "";
} else {
result2 = ((nterm.match(/\W?\d+(?=x(?!\^))/g)).toString()).replace(/\+/g,"") + ','; }
if ( nterm.match(/[^\^]\d+(?!\d|x)/g) === null ) { // Regex for constant
var result3 = "";
} else {
result3 = ((nterm.match(/[^\^]\d+(?!\d|x)/g)).toString()).replace(/\+/g,""); }
console.log(('[' + ' ' + result + result2 + ' ' + result3 + ']' ).replace(/\s/g,"")); }
getCoeff(str)
Problems:
- Does not work when there is a missing
x term
.
Eg: x^4 + x + 1 ==> Expected: [1, 0, 0, 1, 1] ==> Actual: [ 1, 1 ]
x
should return[ 1,0 ]
, but it returns[ 1, ]
This is my second attempt at it.
var str = "-999x^2+x^3+x+3";
function getCoeff(string) {
if ( string.charAt(0) === 'x' ) { // If the first term is x, because of my regex it needs a space to match it
string = ' ' + string;
}
for ( var i = 0; i < 10; i++ ) { // ==> Loop true the regexs to replace all ‘x^n’ to ‘1x^n’
var re = new RegExp('[^0-9|\-]x\\^' + i);
var re2 = new RegExp('[^0-9|]x\\^' + i);
string = (string.replace(re,"+1x^" + i)).replace(re2," -1x^" + i); }
var final = string.replace(/-/g,'+-'); // ==> Spilt(‘x’) later so to retain the -ve sign
final = (final.replace(/[^0-9|-]x(?!\^)/g,"+1x")).replace(/[^0-9|+]x(?!\^)/g,"-1x"); // ==> Replace ‘-/x’ with ‘-/1x’
final = final.replace(/[^\^](\d+(?!\d|x))/g,'+$1x^0'); // ==> Replace ‘c’ with ‘cx^0’
final = final.replace(/x(?!\^)/g, "x^1"); // ==> Replace ‘x’ with ‘x^1’
final = final.split('+'); // ==> Right now array looks something like this [ ax^(n), bx^(n-1), … yx^1, zx^0]
final = final.filter(function(entry) { return entry.trim() !== ''; }); // Sorts array by the number behind in descending order
var reS = /^-?\d+/,
reE = /\d+$/;
var result = final.sort(function(a, b) {
a = reE.exec(a);
b = reE.exec(b);
return b - a;
}).reduce(function(res, str, i) {
var gap = reE.exec(final[i - 1]) - reE.exec(str);
if(gap > 0)
while(--gap) res.push(0);
res.push(+reS.exec(str));
return res;
}, []); // Return the coefficients
console.log("Result:", result);
}
getCoeff(str);
Questions:
Is there a way to do it without using regular expressions mostly?
How do I fix this problem? When no constant term is
getCoeff(“x^3”) ==> [ 1 ] , when it should give [ 1, 0, 0 ]
How can I make my code more efficient?
How to match
x^n
terms but not-x^n
terms using regex? This is my current one:[^0-9|\-]x\\^' + i
, but it needs a space in front of it.
Reference: