#Java 8, 202202 176 174 bytes
a->{String r="";forr="";int j=a.length;for(int i=a.length,j=0;ii:a)r+=i==0*j-->0;j++)r=a[i].equals("0")?r"":a[i]+"+"+i+(j<1?"":j<2?"x":"x^"+j)+"+"+r;return;return r.isEmpty()?"0":r.substring(1).replace("+-","-").replaceAll("([\\[+-\\+]])1x","$1x").replaceAll(".$","");}
Can definitely be golfed some more..
- 26 bytes thanks to @Nevay.
Try it online.Try it online.
a->{ // Method with String-array parameter and String return-type
String r=""; // Result-String, starting empty
for(int i=aj=a.length,j=0;i-->0;j++)
length; // Power-integer, starting at the size of the input-array
for(int i:a) // Loop over the array,
r+=i==0 // where `i` goes backwards, and `j` forwards
r=a[i].equals("0")? // If the `i`'thcurrent item is 0
r *j--? // Leave(And `r`decrease the`j` same
by 1 at the same time)
: "" // Else, changeAppend `r`the to:
result with nothing
a[i] : // The `i`'th itemElse:
+(j<1? "+" // IfAppend `j`the isresult 0with (firsta iteration)"+",
"" +i // and Appendthe nothingcurrent item,
:j<2+(j<1? // Else if+If `j` is 1 (second iteration)0:
"x" "" // Append "x"
Append nothing :more
:j<2? // Else-if (`j` is 2 or larger)1:
"x^"+j) "x" // Append "x^" withAppend `j`"x"
+"+" : // Append a "+"Else:
+r; "x^"+j); // And append theAppend previous"x^" `r`and `j`
return r.isEmpty()? // If `r` is still empty
"0" // Return "0"
: // Else:
r.substring(1) // Else: Return the result minus the leading "+",
r .replace("+-","-") // Changeand change all occurrences of "+-" to "-",
.replaceAll("([\\[+-\\+]])1x","$1x");}
// Alland all occurrences of "1x" to "x"
.replaceAll(".$","");} // And remove the trailing "+"