Skip to main content
deleted 4 characters in body
Source Link
Julian
  • 3.2k
  • 7
  • 17

PowerShell Core, 103102 bytes

$s=-1
switch -r($args[$args.LengthCount..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a"|% *ce '+-','-'|% *ce '1x','x'

Try it online!Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x

PowerShell Core, 103 bytes

$s=-1
switch -r($args[$args.Length..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a"|% *ce '+-','-'|% *ce '1x','x'

Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x

PowerShell Core, 102 bytes

$s=-1
switch -r($args[$args.Count..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a"|% *ce '+-','-'|% *ce '1x','x'

Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x

deleted 5 characters in body
Source Link
Julian
  • 3.2k
  • 7
  • 17

PowerShell Core, 107103 bytes

$s=-1
switch -r($args[$args.Length..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a".Replace("$s`x+$a"|% *ce '+-','-')-replace"1x"'|% *ce '1x','x'

Try it online!Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x

PowerShell Core, 107 bytes

$s=-1
switch -r($args[$args.Length..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a".Replace('+-','-')-replace"1x",'x'

Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x

PowerShell Core, 103 bytes

$s=-1
switch -r($args[$args.Length..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a"|% *ce '+-','-'|% *ce '1x','x'

Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x

Source Link
Julian
  • 3.2k
  • 7
  • 17

PowerShell Core, 107 bytes

$s=-1
switch -r($args[$args.Length..0]){.{$a=-$a;$s=-$s}~{$a--}}"$s`x+$a".Replace('+-','-')-replace"1x",'x'

Try it online!

Passes an expression as a string using splatting
Then processes it from right to left
For each char, negate $s and $a, and if the char is ~ , decrement $a
Then concatenate them all: $s + "x+" + $a
Replace +- with -
Remove the useless 1 before x