Skip to main content
edited body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Q           e# Leave an empty array on the bottom of the stack.
l           e# Read a line from STDIN.
eu'^/';*    e# Convert to uppercase and replace carets with semicolons.
'+/         e# Split at plus signs.

{           e# For each resulting chunk:
  ~         e#   Evaluate it. "X" pushes 1 and ";" discards.
            e#   For example, "3X" pushes (3 1) and "3X;2" (3 2).
   :E       e#   Save the rightmost integer (usually the exponent) in E.
   [        e#
     *      e#   Multiply both integers.
            e#   For a constant term c, this repeats the empty string (Q) c times.
     'x     e#   Push the character 'x'.
     ['^E(] e#   Push ['^' E-1].
   ]        e#
   E<       e#  Keep at most E elements of this array.
            e#  If E == 1, 'x' and ['^' E-1] are discarded.
            e#  If E == 2, ['^' E-1] is discarded.
            e#  If E >= 3, nothing is discarded.
}/          e#

]           e# Wrap the entire stack in an array.
1>          e# Discard its first element.
            e# If the first term was constant, this discards [""]. ["" 'x']
            e# or ["" 'x' ['^' E-1]], depending on the constant.
            e# In all other cases, it discards the untocuheduntouched empty array (Q).
" + "*      e# Join all kept array elements, separating by " + ".
Q           e# Leave an empty array on the bottom of the stack.
l           e# Read a line from STDIN.
eu'^/';*    e# Convert to uppercase and replace carets with semicolons.
'+/         e# Split at plus signs.

{           e# For each resulting chunk:
  ~         e#   Evaluate it. "X" pushes 1 and ";" discards.
            e#   For example, "3X" pushes (3 1) and "3X;2" (3 2).
   :E       e#   Save the rightmost integer (usually the exponent) in E.
   [        e#
     *      e#   Multiply both integers.
            e#   For a constant term c, this repeats the empty string (Q) c times.
     'x     e#   Push the character 'x'.
     ['^E(] e#   Push ['^' E-1].
   ]        e#
   E<       e#  Keep at most E elements of this array.
            e#  If E == 1, 'x' and ['^' E-1] are discarded.
            e#  If E == 2, ['^' E-1] is discarded.
            e#  If E >= 3, nothing is discarded.
}/          e#

]           e# Wrap the entire stack in an array.
1>          e# Discard its first element.
            e# If the first term was constant, this discards ["" 'x' ['^' E-1]].
            e# In all other cases, it discards the untocuhed empty array (Q).
" + "*      e# Join all kept array elements, separating by " + ".
Q           e# Leave an empty array on the bottom of the stack.
l           e# Read a line from STDIN.
eu'^/';*    e# Convert to uppercase and replace carets with semicolons.
'+/         e# Split at plus signs.

{           e# For each resulting chunk:
  ~         e#   Evaluate it. "X" pushes 1 and ";" discards.
            e#   For example, "3X" pushes (3 1) and "3X;2" (3 2).
   :E       e#   Save the rightmost integer (usually the exponent) in E.
   [        e#
     *      e#   Multiply both integers.
            e#   For a constant term c, this repeats the empty string (Q) c times.
     'x     e#   Push the character 'x'.
     ['^E(] e#   Push ['^' E-1].
   ]        e#
   E<       e#  Keep at most E elements of this array.
            e#  If E == 1, 'x' and ['^' E-1] are discarded.
            e#  If E == 2, ['^' E-1] is discarded.
            e#  If E >= 3, nothing is discarded.
}/          e#

]           e# Wrap the entire stack in an array.
1>          e# Discard its first element.
            e# If the first term was constant, this discards [""]. ["" 'x']
            e# or ["" 'x' ['^' E-1]], depending on the constant.
            e# In all other cases, it discards the untouched empty array (Q).
" + "*      e# Join all kept array elements, separating by " + ".
added 1369 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Qleu'^/';*'+/{~:E[*'x"^"EE[*'x['^E(+]E<]]E<}/]1>" + "*

How it works

Q           e# Leave an empty array on the bottom of the stack.
l           e# Read a line from STDIN.
eu'^/';*    e# Convert to uppercase and replace carets with semicolons.
'+/         e# Split at plus signs.

{           e# For each resulting chunk:
  ~         e#   Evaluate it. "X" pushes 1 and ";" discards.
            e#   For example, "3X" pushes (3 1) and "3X;2" (3 2).
   :E       e#   Save the rightmost integer (usually the exponent) in E.
   [        e#
     *      e#   Multiply both integers.
            e#   For a constant term c, this repeats the empty string (Q) c times.
     'x     e#   Push the character 'x'.
     ['^E(] e#   Push ['^' E-1].
   ]        e#
   E<       e#  Keep at most E elements of this array.
            e#  If E == 1, 'x' and ['^' E-1] are discarded.
            e#  If E == 2, ['^' E-1] is discarded.
            e#  If E >= 3, nothing is discarded.
}/          e#

]           e# Wrap the entire stack in an array.
1>          e# Discard its first element.
            e# If the first term was constant, this discards ["" 'x' ['^' E-1]].
            e# In all other cases, it discards the untocuhed empty array (Q).
" + "*      e# Join all kept array elements, separating by " + ".

[1]: http://cjam.aditsu.net/#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE%5B*'x%22%5E%22E#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE%5B*'x%5B'%5EE(%2B%5DE%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20%5D%5DE%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20-3x%5E2%20%2B%2017x%5E17%20%2B%20-1x%5E107

Qleu'^/';*'+/{~:E[*'x"^"E(+]E<}/]1>" + "*

[1]: http://cjam.aditsu.net/#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE%5B*'x%22%5E%22E(%2B%5DE%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20-3x%5E2%20%2B%2017x%5E17%20%2B%20-1x%5E107

Qleu'^/';*'+/{~:E[*'x['^E(]]E<}/]1>" + "*

How it works

Q           e# Leave an empty array on the bottom of the stack.
l           e# Read a line from STDIN.
eu'^/';*    e# Convert to uppercase and replace carets with semicolons.
'+/         e# Split at plus signs.

{           e# For each resulting chunk:
  ~         e#   Evaluate it. "X" pushes 1 and ";" discards.
            e#   For example, "3X" pushes (3 1) and "3X;2" (3 2).
   :E       e#   Save the rightmost integer (usually the exponent) in E.
   [        e#
     *      e#   Multiply both integers.
            e#   For a constant term c, this repeats the empty string (Q) c times.
     'x     e#   Push the character 'x'.
     ['^E(] e#   Push ['^' E-1].
   ]        e#
   E<       e#  Keep at most E elements of this array.
            e#  If E == 1, 'x' and ['^' E-1] are discarded.
            e#  If E == 2, ['^' E-1] is discarded.
            e#  If E >= 3, nothing is discarded.
}/          e#

]           e# Wrap the entire stack in an array.
1>          e# Discard its first element.
            e# If the first term was constant, this discards ["" 'x' ['^' E-1]].
            e# In all other cases, it discards the untocuhed empty array (Q).
" + "*      e# Join all kept array elements, separating by " + ".

[1]: http://cjam.aditsu.net/#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE%5B*'x%5B'%5EE(%5D%5DE%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20-3x%5E2%20%2B%2017x%5E17%20%2B%20-1x%5E107

deleted 4 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 43 41 bytes

Qleu'^/';*'+/{~:E[*'x"^"E(+]E<}/]1>" + "*

Thanks to @jimmy23013 for pointing out one bug and golfing off two bytes!

Try it online in the [CJam interpreter][1].

[1]: http://cjam.aditsu.net/#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE*%22x%5E%22%2BE#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE%5B*'x%22%5E%22E(%2BE_3%3D%2B%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20%2B%5DE%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20-3x%5E2%20%2B%2017x%5E17%20%2B%20-1x%5E107

CJam, 43 41 bytes

Qleu'^/';*'+/{~:E[*'x"^"E(+]E<}/]1>" + "*

Thanks to @jimmy23013 for pointing out one bug and golfing off two bytes!

Try it online in the [CJam interpreter][1].

[1]: http://cjam.aditsu.net/#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE*%22x%5E%22%2BE(%2BE_3%3D%2B%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20-3x%5E2%20%2B%2017x%5E17%20%2B%20-1x%5E107

CJam, 43 41 bytes

Qleu'^/';*'+/{~:E[*'x"^"E(+]E<}/]1>" + "*

Thanks to @jimmy23013 for pointing out one bug and golfing off two bytes!

Try it online in the [CJam interpreter][1].

[1]: http://cjam.aditsu.net/#code=Qleu'%5E%2F'%3B*'%2B%2F%7B~%3AE%5B*'x%22%5E%22E(%2B%5DE%3C%7D%2F%5D1%3E%22%20%2B%20%22*&input=1%20%2B%202x%20%2B%20-3x%5E2%20%2B%2017x%5E17%20%2B%20-1x%5E107

added 87 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
deleted 38 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 6 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 2 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 8 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 6 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 2 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading