-5
\$\begingroup\$

The Challenge

Your task is to create a program that can solve an algebraic equation.

Input

Input will consist of a String. The string will be an equality involving the variable x, and follows the following rules:

  • the equation will always be linear
  • The valid operations in the equation are + - * /
  • Parenthesis will not be used
  • Order of operations must be respected
  • The coefficients of x will always have * in between the coefficient and x
  • You can assume that there will be exactly one solution for x

Output

Output will be through the stdout (or an acceptable equivalent). The output should be the value of x that satisfies the equality. (You can round to the nearest thousandth if the decimal expansion is too long.)


Test cases

x=5
=> 5

2*x=5
=> 2.5

3*x/2=7
=> 4.667

2*x=x+5
=> 5

3*x+3=4*x-1
=> 4

3+x*2+x=x+x/2+x/2
=> -3

Scoring

Because this is a , the shortest solution (in bytes) wins. (NOTE: You may not use built-ins that trivialize the problem.)

\$\endgroup\$
  • 5
    \$\begingroup\$ You should avoid edits which disqualify existing answers. \$\endgroup\$ – Mego Mar 8 '16 at 2:03
  • 6
    \$\begingroup\$ In general we recommend challenges be posted to the Sandbox where they can get feedback from the community prior to being posted live. \$\endgroup\$ – Alex A. Mar 8 '16 at 2:06
  • 3
    \$\begingroup\$ please don't use built-ins is neither here or there. Are they allowed or are they not? And what built-ins does this cover? \$\endgroup\$ – Dennis Mar 8 '16 at 2:19
  • 3
    \$\begingroup\$ 1. please don't use built-ins is a request, not a rule. 2. What about built-ins that parse the equation but do not solve it? Can we use eval? \$\endgroup\$ – Dennis Mar 8 '16 at 2:31
  • 3
    \$\begingroup\$ For the sake of clarity, we consider "request" and "rule" to be mutually exclusive. Challenge specifications should consist entirely of rules, e.g. "you must do this" or "you can't do that," rather than requests. Then it's not clear whether we actually can't do something. Do you see what I mean? \$\endgroup\$ – Alex A. Mar 8 '16 at 4:45
4
\$\begingroup\$

Perl, 49 bytes

Ah, builtins got disallowed. I'll leave this solution up for reference though it is now non competing.

Includes +2 for -lp (-l can be dropped but is ugly)

Run with the equation on STDIN:

perl -lp algebra.pl <<< "x=5"

algebra.pl:

s'x'$.'g;$_=s/=(.*)/-($1)//(1-eval()*$.--/eval)
\$\endgroup\$
  • 1
    \$\begingroup\$ I can't tell whether this is symbolically solving the equation, but if it is then this is invalid as the OP has edited the question to disallow such built-ins. \$\endgroup\$ – Alex A. Mar 8 '16 at 2:05
  • \$\begingroup\$ @AlexA. I don't think it is. \$\endgroup\$ – a spaghetto Mar 8 '16 at 2:16
  • 2
    \$\begingroup\$ @AlexA. No, it isn't symbolically solving. Perl has no "solve". It replaces f(x)=g(x) by f(x)-g(x), then uses a builtin to evaluates this at x=0 and x=1 respectively. It then calculates where the 0 must be \$\endgroup\$ – Ton Hospel Mar 8 '16 at 2:19
2
\$\begingroup\$

Javascript, 62 bytes

Without builtins, but with eval.

(s,f=eval("x=>"+s.replace(/=(.*)/,"-($1)")))=>f(0)/(f(0)-f(1))

Uses eval to transform foo(x) = bar(x) into a function f(x) = foo(x) - bar(x), then calculates x = f(0) / (f(0) - f(1)).

\$\endgroup\$
  • \$\begingroup\$ Utterly explodes for input close() \$\endgroup\$ – CalculatorFeline Mar 8 '16 at 4:40
2
\$\begingroup\$

Python 3, 587 bytes

from re import sub as r,findall as f
a=input()
i=int;s=str;l=len;d=r"(\d+\.?\d*)";d=d,d
a=r(r"%s\*%s"%d,(lambda x:s(i(x.group(1))*i(x.group(2)))),a)
a=r(r"%s\/%s"%d,(lambda x:s(i(x.group(1))/i(x.group(2)))),a)
a=r(r"%s\-%s"%d,(lambda x:s(i(x.group(1))-i(x.group(2)))),a)
a=r(r"%s\+%s"%d,(lambda x:s(i(x.group(1))+i(x.group(2)))),a)
a=f(r"(\d+\.?\d*.)",a+"=")
if l(a)==0 or a[0][1]!="*":a[0:0]=["1"]
if l(a)==1 or a[1][1]!="=":a[1:1]=["0"]
if l(a)==2 or a[2][1]!="*":a[2:2]=["1"]
if l(a)==3 or a[3][1]!="=":a[3:3]=["0"]
a=[int(x[0])for x in a]
z=a[1]-a[3]
print(z/(z-a[0]-a[1]+a[2]+a[3]))

The only longer answers I've seen use 80% of the code to store a big data table. But unlike eval-based solutions, it only uses e twice! (In re and len.)

Log, Day 20: Used Solve. Solve got banned. Learned re. Used re. Slept.

\$\endgroup\$
1
\$\begingroup\$

Matlab, 61 bytes

x=[1 0];a=eval([strrep(input(''),'=','-(') ')']);a(2)/diff(a)

Uses the same algorithm as Ton Haspel to solve the equation. Replaces the = by -( and adds a ) at the end to make the string an equation that should equal zero, and solves (Newton's method is exact for linear functions).

Input is just a string, e.g.: '3+x*2+x=x+x/2+x/2'.

Matlab has a nice builtin for this too,

solve(input(''))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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