18
3
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.
3How flexible is the input format? Instead of
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]
? – Luis Mendo – 2018-06-04T13:59:03.977Can the exponent input sign be
**
instead of^
? – sergiol – 2018-06-04T17:20:38.720@sergiol no.... – Luis felipe De jesus Munoz – 2018-06-04T17:21:36.233
Can we take
X
in uppercase? – Arnauld – 2018-06-04T20:01:51.3871@Arnauld Yes... – Luis felipe De jesus Munoz – 2018-06-04T20:04:20.767
4What is an "external library" and how is it fair, compared to languages who have "eval" already implemented as a feature? – Olivier Grégoire – 2018-06-04T23:02:59.913
1My apologies I haven't use my pc since yesterday. I have updated the challenge with the suggestions you gave me. Please take a look at it and reopen it if everything is ok. – Luis felipe De jesus Munoz – 2018-06-05T12:32:13.757
1Please merge the "Update" part. – user202729 – 2018-06-05T13:39:33.890
(also don't worry too much about slow update, closed challenges can be reopened, etc.) – user202729 – 2018-06-05T13:41:04.230
Your specs don't match your examples. It should be 10x^0 under your specs, but your examples use 10. – Yakk – 2018-06-05T19:50:03.567
1Your test cases don't have any coefficients of
1
– Noodle9 – 2018-06-05T21:01:18.223