-5
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 andx
- 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 code-golf, the shortest solution (in bytes) wins. (NOTE: You may not use built-ins that trivialize the problem.)
5You should avoid edits which disqualify existing answers. – Mego – 2016-03-08T02:03:43.390
6
In general we recommend challenges be posted to the Sandbox where they can get feedback from the community prior to being posted live.
– Alex A. – 2016-03-08T02:06:41.6333please don't use built-ins is neither here or there. Are they allowed or are they not? And what built-ins does this cover? – Dennis – 2016-03-08T02:19:28.320
What do you mean "is neither here or there"? And no, they are not allowed if they trivialize the problem (e.g. you can do something like
print solveThisEquation('x+4=5')
– AMACB – 2016-03-08T02:27:05.0673>
>
Solve
.3For 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? – Alex A. – 2016-03-08T04:45:45.547
Can there by signs on the numbers ? E.g.
+5 + -3 * x
– Ton Hospel – 2016-03-08T13:06:58.277Can I assume the multiplier will always be before the variable (eg
x*3
never occurs)? – CalculatorFeline – 2016-03-08T15:16:58.220@AlexA. Then it is a rule. – AMACB – 2016-03-08T15:32:51.833