The problem
Create a operate that differentiates a polynomial for a given worth of x
.
Your operate will obtain 2 arguments: a polynomial as a string, and some extent to judge the equation as an integer.
Assumptions:
- There will probably be a coefficient close to every
x
, except the coefficient equals1
or-1
. - There will probably be an exponent close to every
x
, except the exponent equals “ or1
. - All exponents will probably be higher or equal to zero
Examples:
Equation.differenatiate("12x+2", 3) ==> 12
Equation.differenatiate("x^2+3x+2", 3) ==> 9
The answer in Java code
Choice 1:
import java.math.BigInteger;
import java.util.regex.Sample;
import java.util.regex.Matcher;
public class Equation {
non-public static remaining Sample TERM = Sample.compile("(.*?)x(^d+)?");
public static BigInteger differentiate(String equation, lengthy x) {
BigInteger xb = BigInteger.valueOf(x);
BigInteger consequence = BigInteger.ZERO;
Matcher m = TERM.matcher(equation);
whereas (m.discover()) {
lengthy okay = 1;
String ks = m.group(1);
if (!ks.isEmpty() && !ks.equals("+"))
okay = ks.equals("-") ? -1 : Lengthy.parseLong(ks);
int p = 1;
String ps = m.group(2);
if (ps != null)
p = Integer.parseInt(ps.substring(1));
consequence = consequence.add(BigInteger.valueOf(okay).multiply(BigInteger.valueOf(p)).multiply(xb.pow(p - 1)));
}
return consequence;
}
}
Choice 2:
import java.math.BigInteger;
import java.util.ArrayList;
public class Equation {
public static BigInteger differentiate(String equation, lengthy x) {
if(equation.equals("")) {
return new BigInteger("0");
}
ArrayList<String> checklist = new ArrayList<>();
char[] charEquation = equation.toCharArray();
String temp = "" + charEquation[0];
for (int i = 1; i < charEquation.size; i++) {
if (charEquation[i] == '-' || charEquation[i] == '+') {
checklist.add(temp);
temp = "" + charEquation[i];
}
else temp += charEquation[i];
}
checklist.add(temp);
BigInteger sum = new BigInteger("0");
for (String el : checklist) {
if(el.incorporates("^")) {
sum = sum.add(pow(el, x));
} else if (el.incorporates("x")){
sum = sum.add(noPow(el));
}
}
BigInteger bi = new BigInteger(String.valueOf(sum));
return bi;
}
public static BigInteger noPow (String equation) {
if (equation.toCharArray()[0] == '-' || equation.toCharArray()[0] == '+') {
String coef = "";
for (int i = 1; i < equation.toCharArray().size; i++) {
if(equation.toCharArray()[i] == 'x') {
break;
} else {
coef += equation.toCharArray()[i];
}
}
if (coef.equals("")) coef = "1";
if (equation.toCharArray()[0] == '-') {
return new BigInteger(coef).multiply(new BigInteger("-1")) ;
}
return new BigInteger(coef) ;
} else {
String coef = "";
for (int i = 0; i < equation.toCharArray().size; i++) {
if(equation.toCharArray()[i] == 'x') {
break;
} else {
coef += equation.toCharArray()[i];
}
}
if(coef.equals("")) coef = "1";
return new BigInteger(coef);
}
}
public static BigInteger pow(String equation, lengthy x) {
if(equation.toCharArray()[0] == '-' || equation.toCharArray()[0] == '+') {
String coef = "";
int counter = 0;
for (int i = 1; i < equation.toCharArray().size; i++) {
if(equation.toCharArray()[i] == 'x') {
counter = i;
break;
} else {
coef += equation.toCharArray()[i];
}
}
if(coef.equals("")) coef = "1";
String powCoef = "";
for (int i = counter + 2; i < equation.toCharArray().size; i++) {
powCoef += equation.toCharArray()[i];
}
BigInteger biCoef = new BigInteger(coef);
BigInteger biPowCoef = new BigInteger(powCoef);
biCoef = biCoef.multiply(biPowCoef);
biPowCoef = biPowCoef.add(new BigInteger("-1"));
BigInteger consequence = biCoef.multiply(new BigInteger(String.valueOf(x)).pow(Integer.valueOf(biPowCoef.toString())));
if(equation.toCharArray()[0] == '-') {
return consequence.multiply(new BigInteger("-1"));
}
return consequence;
} else {
String coef = "";
int counter = 0;
for (int i = 0; i < equation.toCharArray().size; i++) {
if(equation.toCharArray()[i] == 'x') {
counter = i;
break;
} else {
coef += equation.toCharArray()[i];
}
}
if(coef.equals("")) coef = "1";
String powCoef = "";
for (int i = counter + 2; i < equation.toCharArray().size; i++) {
powCoef += equation.toCharArray()[i];
}
BigInteger biCoef = new BigInteger(coef);
BigInteger biPowCoef = new BigInteger(powCoef);
biCoef = biCoef.multiply(biPowCoef);
biPowCoef = biPowCoef.add(new BigInteger("-1"));
BigInteger consequence = biCoef.multiply(new BigInteger(String.valueOf(x)).pow(Integer.valueOf(biPowCoef.toString())));
return consequence;
}
}
}
Choice 3:
import java.util.*;
import java.math.BigInteger;
public class Equation {
public static BigInteger differentiate(String equation, lengthy x) {
BigInteger res = BigInteger.ZERO,
X = BigInteger.valueOf(x);
equation += "+";
Map <Integer, Integer> copt = new HashMap <> ();
char[] token = equation.toCharArray();
int coeff = 1,
energy = 1,
leng = token.size;
boolean isPow = false;
for (int i = 0; i < leng; i++) {
if (token[i] == '+' || token[i] == '-') {
if (isPow) copt.put(energy, coeff);
isPow = false;
energy = 1;
coeff = token[i] == '+' ? 1 : -1;
}
if (token[i] == 'x') isPow = true;
if (Character.isDigit(token[i])) {
StringBuilder sb = new StringBuilder();
sb.append(token[i]);
whereas (i < leng - 1 && Character.isDigit(token[i+1])) sb.append(token[++i]);
int worth = Integer.parseInt(sb.toString());
if (isPow) energy *= worth;
else coeff *= worth;
}
}
for (int p : copt.keySet()) {
BigInteger mul = BigInteger.valueOf(p * copt.get(p));
res = res.add(X.pow(p - 1).multiply(mul));
}
return res;
}
}
Check instances to validate our answer
import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.math.BigInteger;
public class SolutionTest {
@Check
public void sampleTests() {
assertEquals(new BigInteger("12"), Equation.differentiate("12x+2", 3));
assertEquals(new BigInteger("5"), Equation.differentiate("x^2-x", 3));
assertEquals(new BigInteger("-20"), Equation.differentiate("-5x^2+10x+4", 3));
}
}
Extra check instances
import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.math.BigInteger;
import java.util.*;
import java.util.regex.*;
import java.util.stream.*;
// TODO: Exchange examples and use TDD growth by writing your individual assessments
public class SolutionTest {
@Check
public void sampleTests() {
assertEquals(new BigInteger("12"), Equation.differentiate("12x+2", 3));
assertEquals(new BigInteger("5"), Equation.differentiate("x^2-x", 3));
assertEquals(new BigInteger("-20"), Equation.differentiate("-5x^2+10x+4", 3));
}
@Check
public void moreTests() {
assertEquals(new BigInteger("9"), Equation.differentiate("x^2+3x+3",3) );
assertEquals(new BigInteger("1062300"), Equation.differentiate("1000x^2+300x+200",531) );
assertEquals(new BigInteger("87017"), Equation.differentiate("21x^2+35x+3",2071) );
assertEquals(new BigInteger("38509884"), Equation.differentiate("66x^3+3x^2+3",441) );
assertEquals(new BigInteger("5962009860"), Equation.differentiate("21x^4+3x^3",414) );
assertEquals(new BigInteger("-2480823269890144044"), Equation.differentiate("-21x^5+3x^3",12398) );
assertEquals(new BigInteger("-2469135813"), Equation.differentiate("-x^2+3x-3",1234567908) );
assertEquals(new BigInteger("-6045"), Equation.differentiate("-7x^5+22x^4-55x^3-94x^2+87x-56",-3) );
assertEquals(new BigInteger("-3300404885229567012"), Equation.differentiate("-123x^5+3x",8559) );
assertEquals(new BigInteger("119769696967118"), Equation.differentiate("x^2",59884848483559L) );
}
non-public static class RefSolver {
remaining static non-public Sample PATTERN = Sample.compile("+?(?<coef>-?d*)(?<var>x?^?)(?<exp>d*)");
remaining static non-public BigInteger ZERO = BigInteger.ZERO;
non-public static BigInteger differentiate(String s, lengthy x) {
BigInteger bigX = new BigInteger(""+x);
Map<Integer,BigInteger> derivate = new HashMap<>();
Matcher m = PATTERN.matcher(s);
whereas (m.discover()) {
BigInteger coef = new BigInteger( "-".equals(m.group("coef")) ? "-1"
: m.group("coef").isEmpty() ? "1": m.group("coef") ),
exp = new BigInteger(!m.group("exp").isEmpty() ? m.group("exp")
: m.group("var").isEmpty() ? "0" : "1");
int exp_1 = exp.intValue()-1;
if (exp_1!=-1 && !exp.equals(ZERO) && !coef.equals(ZERO)) {
derivate.put(exp_1, derivate.getOrDefault(exp_1, ZERO)
.add(exp.multiply(coef)) );
}
}
return derivate.entrySet()
.stream()
.map( me -> me.getValue().multiply( bigX.pow(me.getKey()) ) )
.cut back(BigInteger.ZERO, (a,b) -> a.add(b));
}
}
non-public static remaining Random rnd = new Random();
non-public static int rand(int x) { return rnd.nextInt(x+1); }
non-public static int rand(int a, int b) { return a + rnd.nextInt(b-a+1); }
@Check
public void randomTests() {
for (int i=0 ; i<100 ; i++) {
lengthy x = rand(1,10000);
String s = "";
whereas (s.isEmpty()) b1(?=x)", "");
BigInteger exp = RefSolver.differentiate(s,x);
//System.out.println(s + " => "+exp);
assertEquals(exp, Equation.differentiate(s,x));
}
}
non-public static String getRndTerm(int exp) {
int coef = rand(9)==0 ? 0 : rand(-100,100);
if (coef==0) return "";
if (exp==0) return coef+"";
if (exp==1) return coef+"x";
return String.format("%dx^%d", coef, exp);
}
}