# Extract the Coefficients of a Linear Equation

Given a linear equation mx+n, return n and m. However, the equation may be in any of the following formats:

5x+2 -> 5 2
5x-2 -> 5 -2
5x   -> 5 0
-2   -> 0 -2
x    -> 1 0


Here are the formats, where each ? stands for a positive integer written without leading zeroes

?x+?
?x-?
-?x+?
-?x-?
x+?
x-?
?
-?
?x
-?x
x


All of these cases must be handled.

## Specifications:

• You can assume that the equation is in one of the above formats, i.e. it matches the regex ^(-?[123456789]\d*)?x([+-][123456789]\d*)?|-?[123456789]\d*$. ## Test Cases: -2x+3 -> -2 3 44x -> 44 0 -123 -> 0 -123 x -> 1 0 -1x -> -1 0  • What about output formats? Would e.g. 1 +2 be a valid output for 1x+2? – Peter Taylor May 12 '17 at 19:24 • Adding on to what @PeterTaylor said, the python answer adds an L to the end of numbers if they get too big. Should this be allowed? – Okx May 12 '17 at 19:30 • @PeterTaylor You can use any valid output format. – Esolanging Fruit May 12 '17 at 19:49 • Is it valid to output constants as length-1 lists, and true linear polynomials as length-2 lists? So for example, 7x is output as 7 0 but 7 is output as just 7? – Greg Martin May 12 '17 at 20:06 • @GregMartin No, you must always output both numbers. – Esolanging Fruit May 12 '17 at 20:07 ## 13 Answers # Python 2, 55 bytes j=1j c=eval(input().replace(*'xj')) print c.imag,c.real  Try it online! Uses Python's built-in code evaluation. The input is formatted like a complex number by replacing x withj, Python's complex unit. Because only literals like 2j are recognized, but not j or -j, the variable j is assigned as 1j to cover those. Unfortunately, Python doesn't seem to have a built-in to convert a complex number to a pair of reals. • Oh nice thinking :D – Jonathan Allan May 12 '17 at 19:50 ## Mathematica, 16 bytes Inspired by xnor's Python 2 answer: ReIm[I#/.x->-I]&  Takes input as a literal expression (not a string), and returns a pair of numbers. It works by making x a complex number then taking the real and imaginary parts — the only non-obvious bit is multiplying by i to start with, to get the output in the right order. We can also use ReIm[x=-I;I#]&  for 14 bytes (tied with Jelly!), but setting x to equal -i before taking the input, instead of just replacing x afterwards, feels like cheating… • Nice solution. I never new it had a ReIm function, I've always been doing {Re[#],Im[#]}&. You've helped a lot there. – Ian Miller May 13 '17 at 7:33 • @IanMiller, ReIm was only introduced in version 10.1, so it's possible your copy of Mathematica doesn't have it. – Not a tree May 13 '17 at 7:36 • Ah. I do have 10.1 but I have been using old versions for far longer. They add way too many functions each update. :). – Ian Miller May 13 '17 at 7:39 ## JavaScript (ES6), 53 48 bytes s=>([a,b]=s.split(/x\+?/),1/b?[a||1,b||0]:[0,s])  ### Test cases let f = s=>([a,b]=s.split(/x\+?/),1/b?[a||1,b||0]:[0,s]) console.log(f("-2x+3")) // -> -2 3 console.log(f("44x")) // -> 44 0 console.log(f("-123")) // -> 0 -123 console.log(f("x")) // -> 1 0 console.log(f("-1x")) // -> -1 0 • works not for -x try my regex – Jörg Hülsermann May 12 '17 at 22:08 • @JörgHülsermann If I understand the rules correctly, -x is not a valid input. – Arnauld May 12 '17 at 22:10 • You are right sorry – Jörg Hülsermann May 12 '17 at 22:12 # sed, 44 42 bytes s:+:: s:x: : t s:^:0 : : s:^ :1 : s:$: 0:


Try it online!

I/O: one per line.

-2 bytes thanks to @KritixiLithos.

• You can do t and : instead of using the a label – user41805 May 13 '17 at 6:43
• @KritixiLithos Seems to work somehow, thanks! – eush77 May 13 '17 at 9:03
• The first two substitutions can be combined into one. – user41805 Nov 22 '19 at 7:04

# Jelly, 18 14 bytes

”x;ṣ”xVṫ-µ¬ṂW+


Test suite at Try it online!

### How?

”x;ṣ”xVṫ-µ¬ṂW+ - Main link: list of characters s  examples: "5x-2"         "x"       "-123"
”x             - literal 'x'
;            - concatenate with s                        "x5x-2"        "xx"      "x-123"
ṣ”x         - split on 'x's                        ["","5","-2]  ["","",""]  ["","-123"]
V        - evaluate as Jelly code (vectorises)      [0,5,-2]     [0,0,0]     [0,-123]
ṫ-      - tail from index -1 inclusive               [5,-2]       [0,0]     [0,-123]
µ     - monadic chain separation (call that z)
¬    - not z                                       [0,0]       [1,1]        [1,0]
Ṃ   - minimum                                         0           1            0
W  - wrap in a list                                [0]         [1]          [0]
+ - add to z (vectorises)                      [5,-2]       [1,0]     [0,-123]


# PHP, 73 Bytes

preg_match("#((.*)x)?\+?(.*)#",$argn,$t);echo$t[1]?$t[2]?:1:0," ",+$t[3];  Try it online! # PHP, 91 Bytes works also with -x preg_match("#((-)?(\d+)?x)?\+?(-?\d+)?#",$argn,$t);echo$t[2],$t[1]?$t[3]?:1:0," ",$t[4]?:0;  Try it online! ## Mathematica, 23 bytes Coefficient[#,x,{1,0}]&  Try it online copy and paste with ctrl-v this code Coefficient[#,x,{1,0}]&[-2x+3]  and press shift+enter to run # Python 2, 71 bytes lambda s:([0,]+[int(x or1-i)for i,x in enumerate(s.split('x'))])[-2:]  Try it online! # Mathematica, 33 bytes #~CoefficientList~x/.{a_}:>{a,0}&  Pure function taking an expression in the expected format (note: not a string, but a pure expression like -2x+3), and returning an ordered pair of integers with the constant coefficient appearing first (for example, -2x+3 returns {3,-2}). The builtin CoefficientList (which works for polynomials of any degree) does the heavy lifting; its default behavior is to return constants as length-1 lists, so /.{a_}:>{a,0} overrides that and makes the 0 coefficient of x appear explicitly. • I was wondering how long it would be until a Mathematica answer was posted. – Esolanging Fruit May 12 '17 at 20:19 # Retina, 38 bytes x$
x+0
^[^+-]*$0x+$+
^[^-\d]
1\$+
-
+-


Definitely can be improved.

Possibly stretching how we are allowed to format output. It just outputs a complete equation, as in n and m separated by x+

Try it online!

• This fails for an input like x-1 – Leo May 12 '17 at 19:34
• @Leo Thanks, fixed. – Okx May 12 '17 at 19:36
• I'm fairly confident you can replace [^+-]* with \w* – user41805 May 12 '17 at 20:17

# Octave, 45 bytes

@(a)[imag(u=eval(strrep(a,'x','j'))),real(u)]


Try it online!

Evaluate the expression, with x replaced by complex j. Octave is very flexible when it comes to complex expressions, with j,1j,i and 1i all equivalent (with the advantage that 1j and 1i cannot be overwritten by variables, but that's irrelevant for this challenge). Inline expression is used to build a list of [imag(u) real(u)] with u the complex number.

I initially wanted to try a more idiomatic approach, using the built-in sym2poly. For example (already at 47 bytes to make sure 0*x is handled):

@(a)sym2poly([strrep(a,'x','*x') '+x^2']))(2:3)


This one however failed on the case of x without coefficient. In the end a real/complex setup like xnor turned out to be optimal.

# MATL, 12 bytes

120'i'ZtU&Zj


Try it online!

Port of my Octave answer, but of course adapted for compactness in MATL. Explanation:

120'i'Zt     % Replace character 120 ('x') by 'i' in input (implicit).
% (Used 120 instead of 'x' to save a separator between 'x' and 'i')
U    % Convert string to complex number
&Zj % Split complex number in real and imaginary part. Implicit display.


# C (gcc),145138 112 bytes

f(char*s){int a,b,i=0;sscanf(s,"%dx%d",&a,&b);if(L!=0)i=(int)(L-s);printf("%d %d",s[i]=='x'?a:0,s[i]=='x'?b:a);}


Try it online!

void f(char*s)
{
int a,b,i=0;
sscanf(s,"%dx%d",&a,&b);

if(L!=0)
i=(int)(L-s);

printf("%d %d",s[i]=='x'?a:0,s[i]=='x'?b:a);
}


Can definitely be shortened, but dont see it now!

• 1. You don't need the space in char *s 2. Can't you remove whitespace? – Esolanging Fruit May 15 '17 at 0:52
• @Challenger5 Thanks! I used a random online byte counter website yesterday, tried it now on TIO, code is a lot shorter, thanks for pointing out! :) – Abel Tom May 15 '17 at 11:49
• Suggest printf("%d %d",s[i]-'x'?:a,s[i=L?L-s:i]-'x'?a:b); instead of if(L!=0)i=(int)(L-s);printf("%d %d",s[i]=='x'?a:0,s[i]=='x'?b:a); and index() instead of strchr() – ceilingcat Nov 7 '18 at 0:07