405152: GYM101807 D Differentiation
Description
Write a program to differentiate a polynomial in x.
Here's how to differentiate a polynomial in the form
$$$c_nx^n+c_{n-1}x^{n-1}+\cdots+c_1x+c_0$$$
Each term $$$c_ix^i$$$ where $$$i \ge 1$$$ can be handled individually:
- Multiply the coefficient $$$c_i$$$ by $$$i$$$.
- Decrease the exponent of $$$x$$$ by 1.
- Therefore, the result of the term is $$$(c_i\times i)x^{i-1}$$$
Finally, join the results of the terms together to form the final polynomial. Also, the derivative of the constant term is 0.
For example, the derivative of $$$-3x^3+5x^2-7x+4$$$ is $$$(-3\times 3)x^{3-1}+(5\times 2)x^{2-1}+(7\times 1)x^{1-1}+0$$$ = $$$-9x^2+10x-7$$$
The polynomial's format is similar to the requirements for math homework:
- The polynomial does not start with a plus sign.
- The polynomial contains no spaces.
- The terms should be ordered in strictly decreasing degree of $$$x$$$.
- There should be no redundant terms. The term is omitted if the coefficient is 0.
- The 1 is omitted if the coefficient is 1 or -1. (see sample 1)
- The degree is written directly after x. The exponent is omitted if the degree is 1.
- The constant term is omitted if it is zero, except that when the whole polynomial is zero (see sample 3).
The input consists of a string: the polynomial. The maximum degree of $$$x$$$ is 99 and the coefficients are integers between -99 and 99, including -99 and 99 but excluding 0. The length of the string is at most 100.
It's guaranteed that the string strictly follows the format, i.e. +0x1-1x2+0 is not a possible input.
OutputOutput the derivative of the polynomial. It must strictly follow the format.
ExamplesInput4x5-x2Output
20x4-2xInput
-6x+3Output
-6Input
5Output
0Input
-3x3+5x2-7x+4Output
-9x2+10x-7