Create a function which takes a polynomial equation, a value for x
and returns the result of the operation.
Example: given 4x^2+2x-5
and x=3
output 37
. This is the result of 4(3)^2+2(3)-5
- Assume all polynomials are valid
- Polynomial format will always be
coefficient(variable)^exponent => 4x^2
except :- When exponent is
1
it will becoefficient(variable) => 4x
- When coefficient is
1
it will be(variable)^exponent => x^2
- When exponent is
- Polynomials are one variable only
- Use of external libraries are forbidden
- The coefficient and variable input can be positive and negative numbers.
Test cases
("3x^3-5x^2+2x-10", 5) => 250
("10x^4-5x^3-10x^2+3x+50", 3) => 644
("10x+20", 10) => 120
("-20x^2+20x-50", -8) => -1490
("9", 5) => 9
("8x^2+5", 0) => 5
Update
- Polynomial format will always be
coefficient(variable)^exponent => 4x^2
except :- When exponent is
1
it will becoefficient(variable) => 4x
- When coefficient is
1
it will be(variable)^exponent => x^2
- When exponent is
- Removed the rule of negative exponent. My mistake. A valid polynomial does not contain negative exponent
- An exponent of
0
would be justcoefficient
- Added test case for
input 0
This is code-golf, so the shortest answer in bytes win.
3x^3-5x^2+2x-10
can we input3*x^3-5*x^2+2*x-10
? Or[3 -5 2 -10]. [3 2 1 0]
? \$\endgroup\$