With equations that have more than one unknown, simultaneous equations can be used in solving for them. This challenge will only deal with linear equations with two unknowns, to make things as elementary as possible.

The program should take two lines of input from stdin, each containing a linear equation with two unknowns (x and y). Output should be in the form x=<x-value>, y=<y-value>, where the angled bracketed text should be replaced by the appropriate value. Non-integer values can be outputted in either decimal point form rounded to at least 3 decimal places or until the fraction terminates (eg. 1/2 would be 0.5 not 0.500), or simplest (improper, not mixed) fraction form (as all results will be rational). If the equations are unsolvable (ie are parallel but not equivalent), output Unsolvable. If the equations are equivalent and would therefore have an infinite number of solutions, output Degenerate.

The use of external modules is not allowed. Also, any library functions that solve equations (this applies especially to Mathematica) are not allowed.

Each equation in the input will be in general form, with no whitespace. If a coefficent or the constant term is 0, the whole term will not appear, and if a coefficient is ±1, only the pronumeral will appear.

Test cases:

Input:                 Input:                 Input:                 Input:
7x+4y-8=0              4x+3y+32=0             x-y=0                  2x+5y-1=0
x-8y+1=0               x-4=0                  x-y+5=0                2x+5y-1=0
Output:                Output:                Output:                Output:
x=1, y=1/4 (or 0.25)   x=4, y=-16             Unsolvable             Degenerate

This is once again , so shortest code wins.


3 Answers 3


Python 3, 250

I reuse the code for parsing the input for each of the "coefficients" of x, y, and "=" by putting it into a string and multiplying it the right number of times.

For the "coefficient of =" case to work (absence means 0, not 1, like for x and y), the last "0" of the input has to be thrown away (and then magic takes over...). Makes heavy use of short circuiting in logic expressions, and of False==0.

Uses Python's feature of leaving off trailing zeros of floats (finding out if the solution is an integer is still necessary).

exec((("R=input()[:-1];"+"*_,R=I,*_=R.split(%r);%s=R!=I and int(I in'+-'and I+'1'or I);"*3)*2)%tuple("xXyY=Cxxyy=c"))
print(d and"x=%s, y=%s"%(u%d and-u/d or-u//d,v%d and-v/d or-v//d)or"UDnesgoelnvearbaltee"[u==v==0::2])
  • \$\begingroup\$ "UDnesgoelnvearbaltee"[u==v==0::2] might be one of the neatest little Python tricks I've ever seen. Kudos for that. \$\endgroup\$
    – Kasran
    Nov 25, 2014 at 7:10

Python 2 - 497

def i(x):return-1 if x=="-"else int(x)if x else 1
def g(a,b):return g(b,a%b) if b else a
a,u=0 if a<0 else i(u[:a]),u[a+1:]
b,u=0 if b<0 else i(u[:b]),u[b+1:]
e=-i(u) if u else 0
c,v=0 if c<0 else i(v[:c]),v[c+1:]
d,v=0 if d<0 else i(v[:d]),v[d+1:]
f=-i(v) if v else 0
print"Degenerate"if t else"x=%d/%d, y=%d/%d"%(x/m,z/m,y/n,z/n)if z else"Unsolvable"
  • \$\begingroup\$ Sorry - I just realised I typed Unsolveable in the test cases wrong. It should be Unsolvable. Edited now. \$\endgroup\$
    – Volatility
    Aug 28, 2013 at 6:43
  • \$\begingroup\$ It's okay, it doesn't seem like a misspelled word. \$\endgroup\$
    – miles
    Aug 28, 2013 at 6:45
  • \$\begingroup\$ I tried this and got Degenerate for x=0,y=0, which is wrong. \$\endgroup\$ Aug 28, 2013 at 12:51
  • \$\begingroup\$ I see it now, testing for equality after modifying the string, how dumb \$\endgroup\$
    – miles
    Aug 28, 2013 at 13:14

Python 2, 244 chars

import re
def P():e=re.sub(r'(\d)(x|y)',r'\1*\2',raw_input()[:-2]);x=y=0.;c=eval(e);x=1;a=eval(e)-c;x,y=y,x;return a,eval(e)-c,c
print'x=%g, y=%g'%(X/D,(c*d-a*f)/D)if D else'UDnesgoelnvearbaltee'[X==0::2]

Throws some * characters in the right place, then evaluates the LHS on various combinations of x,y in {0,1} to derive the coefficients. Then it does the standard determinant computations to find the solution.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.