Skip to main content
Commonmark migration
Source Link

#Java 8, 202 176 174 173 bytes

Java 8, 202 176 174 173 bytes

#Java 8, 202 176 174 173 bytes

Java 8, 202 176 174 173 bytes

added 9 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 8, 202 176 174174 173 bytes

a->{String r="";int j=a.length;for(int i:a)r+=i==0*j--?"":"+"+i+(j<1?"":j<2?"x":"x^"+j);return r.isEmpty()?"0":r.substring(1).replace("+-","-").replaceAll("([+-]\\D)1x","$1x");}

Try it online.Try it online.

a->{                     // Method with String-array parameter and String return-type
  String r="";           //  Result-String, starting empty
  int j=a.length;        //  Power-integer, starting at the size of the input-array
  for(int i:a)           //  Loop over the array
    r+=i==0              //   If the current item is 0
           *j--?         //   (And decrease `j` by 1 at the same time)
        ""               //    Append the result with nothing
       :                 //   Else:
        "+"              //    Append the result with a "+",
        +i               //    and the current item,
        +(j<1?           //    +If `j` is 0:
           ""            //      Append nothing more
          :j<2?          //     Else-if `j` is 1:
           "x"           //      Append "x"
          :              //     Else:
           "x^"+j);      //      Append "x^" and `j`
  return r.isEmpty()?    //  If `r` is still empty
    "0"                  //   Return "0"
   :                     //  Else:
    r.substring(1)       //   Return the result minus the leading "+",
     .replace("+-","-")  //   and change all occurrences of "+-" to "-",
     .replaceAll("([+-]\\D)1x","$1x");}
                         //   and all occurrences of "1x" to "x"

#Java 8, 202 176 174 bytes

a->{String r="";int j=a.length;for(int i:a)r+=i==0*j--?"":"+"+i+(j<1?"":j<2?"x":"x^"+j);return r.isEmpty()?"0":r.substring(1).replace("+-","-").replaceAll("([+-])1x","$1x");}

Try it online.

a->{                     // Method with String-array parameter and String return-type
  String r="";           //  Result-String, starting empty
  int j=a.length;        //  Power-integer, starting at the size of the input-array
  for(int i:a)           //  Loop over the array
    r+=i==0              //   If the current item is 0
           *j--?         //   (And decrease `j` by 1 at the same time)
        ""               //    Append the result with nothing
       :                 //   Else:
        "+"              //    Append the result with a "+",
        +i               //    and the current item,
        +(j<1?           //    +If `j` is 0:
           ""            //      Append nothing more
          :j<2?          //     Else-if `j` is 1:
           "x"           //      Append "x"
          :              //     Else:
           "x^"+j);      //      Append "x^" and `j`
  return r.isEmpty()?    //  If `r` is still empty
    "0"                  //   Return "0"
   :                     //  Else:
    r.substring(1)       //   Return the result minus the leading "+",
     .replace("+-","-")  //   and change all occurrences of "+-" to "-",
     .replaceAll("([+-])1x","$1x");}
                         //   and all occurrences of "1x" to "x"

#Java 8, 202 176 174 173 bytes

a->{String r="";int j=a.length;for(int i:a)r+=i==0*j--?"":"+"+i+(j<1?"":j<2?"x":"x^"+j);return r.isEmpty()?"0":r.substring(1).replace("+-","-").replaceAll("(\\D)1x","$1x");}

Try it online.

a->{                     // Method with String-array parameter and String return-type
  String r="";           //  Result-String, starting empty
  int j=a.length;        //  Power-integer, starting at the size of the input-array
  for(int i:a)           //  Loop over the array
    r+=i==0              //   If the current item is 0
           *j--?         //   (And decrease `j` by 1 at the same time)
        ""               //    Append the result with nothing
       :                 //   Else:
        "+"              //    Append the result with a "+",
        +i               //    and the current item,
        +(j<1?           //    +If `j` is 0:
           ""            //      Append nothing more
          :j<2?          //     Else-if `j` is 1:
           "x"           //      Append "x"
          :              //     Else:
           "x^"+j);      //      Append "x^" and `j`
  return r.isEmpty()?    //  If `r` is still empty
    "0"                  //   Return "0"
   :                     //  Else:
    r.substring(1)       //   Return the result minus the leading "+",
     .replace("+-","-")  //   and change all occurrences of "+-" to "-",
     .replaceAll("(\\D)1x","$1x");}
                         //   and all occurrences of "1x" to "x"
deleted 113 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#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 "+"

#Java 8, 202 bytes

a->{String r="";for(int i=a.length,j=0;i-->0;j++)r=a[i].equals("0")?r:a[i]+(j<1?"":j<2?"x":"x^"+j)+"+"+r;return r.isEmpty()?"0":r.replace("+-","-").replaceAll("([\\-\\+])1x","$1x").replaceAll(".$","");}

Can definitely be golfed some more..

Try it online.

a->{                         // Method with String-array parameter and String return-type
  String r="";               //  Result-String, starting empty
  for(int i=a.length,j=0;i-->0;j++)
                             //  Loop over the array,
                             //  where `i` goes backwards, and `j` forwards
    r=a[i].equals("0")?      //   If the `i`'th item is 0
       r                     //    Leave `r` the same
      :                      //   Else, change `r` to:
       a[i]                  //    The `i`'th item
       +(j<1?                //    If `j` is 0 (first iteration)
          ""                 //     Append nothing
        :j<2?                //    Else if `j` is 1 (second iteration)
         "x"                 //     Append "x"
        :                    //    Else (`j` is 2 or larger):
         "x^"+j)             //     Append "x^" with `j`
       +"+"                  //    Append a "+"
       +r;                   //    And append the previous `r`
  return r.isEmpty()?        //  If `r` is still empty
    "0"                      //   Return "0"
   :                         //  Else:
    r.replace("+-","-")      //   Change all occurrences of "+-" to "-"
     .replaceAll("([\\-\\+])1x","$1x")
                             //   All occurrences of "1x" to "x"
     .replaceAll(".$","");}  //   And remove the trailing "+"

#Java 8, 202 176 174 bytes

a->{String r="";int j=a.length;for(int i:a)r+=i==0*j--?"":"+"+i+(j<1?"":j<2?"x":"x^"+j);return r.isEmpty()?"0":r.substring(1).replace("+-","-").replaceAll("([+-])1x","$1x");}
  • 26 bytes thanks to @Nevay.

Try it online.

a->{                     // Method with String-array parameter and String return-type
  String r="";           //  Result-String, starting empty
  int j=a.length;        //  Power-integer, starting at the size of the input-array
  for(int i:a)           //  Loop over the array
    r+=i==0              //   If the current item is 0
           *j--?         //   (And decrease `j` by 1 at the same time)
        ""               //    Append the result with nothing
       :                 //   Else:
        "+"              //    Append the result with a "+",
        +i               //    and the current item,
        +(j<1?           //    +If `j` is 0:
           ""            //      Append nothing more
          :j<2?          //     Else-if `j` is 1:
           "x"           //      Append "x"
          :              //     Else:
           "x^"+j);      //      Append "x^" and `j`
  return r.isEmpty()?    //  If `r` is still empty
    "0"                  //   Return "0"
   :                     //  Else:
    r.substring(1)       //   Return the result minus the leading "+",
     .replace("+-","-")  //   and change all occurrences of "+-" to "-",
     .replaceAll("([+-])1x","$1x");}
                         //   and all occurrences of "1x" to "x"
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading