The challenge
Your task is to programe:
String convertToString(int[] pol) {....}
The method returns given by a parameter, (integer array) which represents a polynomial, the representation of the polynomial in String format. NOTE:
array{a, b, c, d, e,...., z}
0 1 2 3 4 .... n
The numbers indicate the grade of the polynomial, where n is a positive integer always. Some examples:
array{0,1,-2,0,5} -> convertToString(array) -> "5x^4-2x^2+x"
array{1,1,-2,0,0} -> convertToString(array) -> "-2x^2+x+1"
array{} -> convertToString(array) -> ""
array{0,0,7,0,0} -> convertToString(array) -> "7x^2"
array{1,0,0,0,0} -> convertToString(array) -> "1"
array{0,1,-1,0,5,0,0,1} -> convertToString(array) -> "x^7+5x^4-x^2+x"
The solution in Java code
Option 1:
import java.util.stream.*;
public class Polinomio{
static String convertString(final int[] pol){
final int len = pol.length;
if (len==0) return "";
return IntStream.range(0, len)
.map( n -> len-1-n )
.filter( n -> pol[n] != 0 )
.mapToObj( n -> ""+pol[n] + ( n == 1 ? "x"
: n != 0 ? "x^"+n : "") )
.collect(Collectors.joining("+"))
.replaceAll("\\b1(?=x)|\\+(?=-)", "");
}
}
Option 2:
import java.util.*;
public class Polinomio{
static String convertString(int[] pol){
List<String> res = new ArrayList<String>();
for(int p=pol.length-1; p>=0; p--) if(pol[p]!=0)res.add((pol[p]<0?'-':res.isEmpty()?"":'+')+(Math.abs(pol[p])==1&&p>0?"":String.valueOf(Math.abs(pol[p])))+(p==0?"":p==1?'x':"x^"+String.valueOf(p)));
return String.join("",res);
}
}
Option 3:
import java.util.List;
import java.util.ArrayList;
public class Polinomio {
static String convertString(int[] a) {
List<String> lst = new ArrayList<>();
for(int i = a.length-1; i >= 0; --i)
if(a[i] != 0) lst.add(Integer.toString(a[i]) + (i > 0 ? "x" : "") + (i > 1 ? "^" + Integer.toString(i) : ""));
return String.join("+", lst).replaceAll("1(?=x)", "").replaceAll("\\+-", "-");
}
}
Option 4:
public class Polinomio{
static String convertString(int[] pol) {
if (pol.length > 0) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
sb.append(pol[0] > 0 ? "+" + pol[0] : pol[0] < 0 ? String.valueOf(pol[0]) : "");
sb.reverse();
for (int i = 1; i < pol.length; i++) {
if (pol[i] != 0) {
if (pol[i] == 1 || pol[i] == -1) {
if (i == 1) {
sb.append(pol[i] > 0 ? "x+" : "x-");
} else
sb.append(pol[i] > 0 ? i + "^x" + "+" : i + "^x" + "-");
} else {
sb.append(pol[i] > 0 ? i + "^x" + pol[i] + "+" : i + "^x" + temp.append(pol[i]).reverse());
}
}
temp.delete(0, temp.length() + 1);
}
if (sb.reverse().indexOf("+") == 0) {
sb.deleteCharAt(0);
}
return sb.toString();
}
return "";
}
}
Test cases to validate our solution
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class PolinomioShould{
@Test
public void basicTests(){
assertThat("error when pol={0,1}",Polinomio.convertString(new int[]{0,1}), is("x") );
assertThat("error when pol={-1,0}",Polinomio.convertString(new int[]{-1,0}), is("-1") );
}
}
Additional test cases
import org.junit.Test;
import java.util.Random;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class PolinomioShould {
@Test
public void test4 (){
assertThat("error when pol={}",Polinomio.convertString(new int[]{}), is("") );
assertThat("error when pol={0,1}",Polinomio.convertString(new int[]{0,1}), is("x") );
assertThat("error when pol={-1,0}",Polinomio.convertString(new int[]{-1,0}), is("-1") );
assertThat("error when pol={-1,0,0,0,0,0}",Polinomio.convertString(new int[]{-1,0,0,0,0,0}), is("-1") );
assertThat("error when pol={-1,1,1}",Polinomio.convertString(new int[]{-1,1,1}), is("x^2+x-1") );
assertThat("error when pol={3,0,2}",Polinomio.convertString(new int[]{3,0,2}),is("2x^2+3"));
assertThat("error when pol={0,-1,0,0,3}",Polinomio.convertString(new int[]{0,-1,0,0,3}), is("3x^4-x"));
assertThat("error when pol={0,1,-2,4,0,0,-1,1}",Polinomio.convertString(new int[]{0,1,-2,4,0,0,-1,1}), is("x^7-x^6+4x^3-2x^2+x"));
assertThat("error when pol={1,1,-2,4,0,0,-1,1}",Polinomio.convertString(new int[]{1,1,-2,4,0,0,-1,1}), is("x^7-x^6+4x^3-2x^2+x+1"));
assertThat("error when pol={1,0,0,0,0,0}",Polinomio.convertString(new int[]{1,0,0,0,0,0}), is("1"));
}
@Test
public void randomTests(){
Random rnd = new Random();
Polinomiop po=new Polinomiop();
for(int j=0; j<25; j++){
int[] pol = new int[(int) (rnd.nextDouble() * 10 + 0)];
for(int i=0; i<pol.length; i=i+2)
pol[i]=((int) (rnd.nextDouble() * 10 -rnd.nextDouble()*10));
assertEquals("error when pol = "+Arrays.toString(pol),
po.convertString(pol), Polinomio.convertString(pol));
}
}
}
class Polinomiop {
static String convertString(int[] pol){
String result="";
for(int i = pol.length-1; i>=0; i--) {
if (pol[i] == 0){
continue;
}
if (i == 0) {
if (pol[i] < 0) {
result += "" + pol[i];
} else {
if(result.isEmpty()){
result+=pol[i];
continue;
}
result += "+" + pol[i];
}
continue;
} else if (i == pol.length - 1) {
if (pol[i] > 0) {
if (i == 1){
result += "" + dame(pol[i]) + "x";
continue;
}
result += "" + dame(pol[i]) + "x^" + i;
} else {
if (i == 1){
result += dame(pol[i]) + "x";
continue;
}
result += "" + dame(pol[i]) + "x^" + i;
}
continue;
}
if (pol[i] < 0){
if(i==1){
result += dame(pol[i]) + "x";
continue;
}
result += dame(pol[i]) + "x^" + i;
continue;
}
if(i==1){
result += "+" + dame(pol[i])+ "x";
continue;
}
if(result.isEmpty()){
result += "" + dame(pol[i])+ "x^" + i;
continue;
}
result += "+" + dame(pol[i])+ "x^" + i;
}
return result;
}
private static String dame(int n){
if(n<0){
return n==-1 ? "-":n+"";
}else{
return n==1 ? "":n+"";
}
}
}