Representation of an array as a polynomial

Viewed 332

What this method should do is take an array of doubles and convert them into a polynomial, for example, if the array is given was [2.0, 3.0, -2.0] the method's output would be 2.0x^2 + 3.0x^1 - 2.0. What I have done is created two loops however, when I do this both outputs are separated as expected. My output looks like this 2.0 1.0 2.0 1.0 x^3 x^2 x^1, if there is a way to have the exponents printed after the coefficient that would solve this issue.

public String printPoly(Double[] doubles) {
    String polynomialString = "";

    for (int i = 0; i < doubles.length; i++) {
        polynomialString += doubles[i] + " ";
    }

    for (int j = doubles.length - 1; j >= 0; j--) {
        if (j == 0) {
            polynomialString += " ";
            break;
        }
        polynomialString += "x^" + j + " ";
    }
    return polynomialString;
}
2 Answers

You can build the resultString using one for-loop. Initialize a counter variable which you count backwards which is representing your ^3 part. This variable starts at your array.length-1 end runs down to zero in the iterations this is how you can create

4.0 x^3
2.0 x^2
0.5 x^1
2.0 x^0

It runs basically backwards so you should decrement it in each iteration

int backCounter = doubles.length-1;

inside your one for-loop do

polynomialString += doubles[j] + "x^" + backCounter + " ";

backCounter--;

Here is a working solution

package so;

import java.util.*;

public class RemoveString {

    public static void main(String[] args) {
        Double[] someNumbers = { 2.0, 3.0, 1.0, 0.5 };
        String s = RemoveString.printPoly(someNumbers);
        System.out.println(s);
    }

    public static String printPoly(Double[] doubles) {

        String polynomialString = "";
        int backwardsCounter = doubles.length - 1;

        for (int i = 0; i < doubles.length; i++) {
            polynomialString += doubles[i] + "x^" + backwardsCounter + " ";
            backwardsCounter --;
        }

        return polynomialString;
    }
}

Produces the output

2.0x^3 3.0x^2 1.0x^1 0.5x^0 

One loop is enough here, or one stream:

public static void main(String[] args) {
    System.out.println(printPoly(new double[]{2.0, 3.0, -2.0}));
    // 2.0x^2 + 3.0x^1 - 2.0
    System.out.println(printPoly(new double[]{-2.0, 3.6, -2.1, 5.2}));
    // -2.0x^3 + 3.6x^2 - 2.1x^1 + 5.2
}
public static String printPoly(double[] doubles) {
    return IntStream
            // iterate through the array indices
            .range(0, doubles.length)
            // string representation of the coefficient
            .mapToObj(i -> coefficient(i, doubles))
            // concatenate into one string
            .collect(Collectors.joining());
}
// separate method because inside the lambda it is too big
static String coefficient(int i, double[] doubles) {
    String val = "";
    // take value from an array
    double element = doubles[i];
    if (i == 0) {
        // first element
        val += element;
    } else {
        // other elements
        if (element <= 0.0D) {
            val += " - " + (0.0D - element);
        } else {
            val += " + " + element;
        }
    }
    // if non-last element, then
    // raised to the power of
    if (i < (doubles).length - 1) {
        // inverted index
        val += "x^" + (doubles.length - 1 - i);
    }
    return val;
}
Related