Problem Statement | |||||||||||||
Given a polynomial function of the variables x, y, and z, write a program to compute the mixed partial derivative with respect to a given list of variables. The function will be expressed as a sum of terms. Each term will be a product of factors. Each factor will be either:
7 x 3*y y*3 z^5 z^4*x^9 3*x*z^5*y Examples of functions are: 7 x z + 6 y + y + 2*y 3 + y^3*2 + z^4*7*x^3 Given a String of the characters 'x', 'y', and 'z', compute the mixed partial derivative with respect to the variables in the string. To compute a mixed partial derivative, repeatedly take the partial derivative with respect to each variable in the string in turn, using the result of each step as the input to the next. (Incidentally, because we are working with nice polynomial functions, the order in which you compute the partial derivatives does not matter.) To compute a partial derivative:
Examples of taking the partial derivatives of functions with respect to y: 3 -> 0 x^2 -> 0 y -> 1 4*y -> 4 2*y^5 -> 10*y^4 7*y*z^3 -> 7*z^3 x^2*y^3 + 9*y^4*z^5 -> 3*x^2*y^2 + 36*y^3*z^5 Your program should return the result as a String, formatted the same way as the input string, with the following additional constraints. Terms with the same powers of all three variables should be combined into a single term, by adding their coefficients. The terms should be sorted with the terms of higher degree (sum of powers of x, y, and z) before terms of lower degree. If two terms have the same degree, output the term with the higher power of x first. If two terms have the same degree and same power of x, output the term with the higher power of y first. Within a term, output the integer coefficient first, followed by the x, y, and z factors, in that order. As with the input String, exponents should only be included only if greater than 1. If as the result of differentiation the power of a variable is reduced to zero, that factor should be omitted. Similarly, integer coefficients of 1 should be omitted. Exception: if a term consists of only the integer coefficient 1, with no x, y, or z factors, that term should be output as "1". Entire terms reduced to zero should be completely omitted. If the result contains no terms, return the String "0". | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | Pay careful attention to the instructions regarding formatting of the output string. | ||||||||||||
- | The only spaces in the input and output are immediately before and after all '+' characters. | ||||||||||||
- | If the vars String is empty, return the given function, subject to the output formatting rules specified in the problem. | ||||||||||||
Constraints | |||||||||||||
- | expr will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | vars will contain between 0 and 5 characters, inclusive. | ||||||||||||
- | vars will contain only the characters 'x', 'y', and 'z', in any order, possibly with multiple occurrences of the same character. | ||||||||||||
- | No term in expr will contain more than one constant factor. | ||||||||||||
- | No term in expr will contain the same variable more than once. | ||||||||||||
- | expr will be formatted as described in the problem statement. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
| |||||||||||||
5) | |||||||||||||
| |||||||||||||
6) | |||||||||||||
|