12
2
This challenge but with a better spec.
Spec
Your program will take a linear equation containing a single variable x
and output the value of x
.
Input / Parsing
- The input will only contain numbers, operators, parenthesis (
()
),x
, and an=
sign (this means no whitespace). - Parenthesis will always be balanced.
- There will always be at least 1
x
. Anx
may be preceded by a number. - All equations will exactly have one result.
A number can be defined by following these steps. A number can be defined by the regex: -?(\d+(\.\d+)?|\.\d+)
.
If you don't speak regex: A digit is defined as 0-9
- It may have a
-
at the beginning of it signifying negative - Then there may be some digits. If they aren't any digits there will be a decimal point
- If a decimal point exists, at least one digit will follow it
The biggest a number / value will be is defined by your language's capabilities.
An operator is any of: +-*/
, they will always appear between numbers, and or parenthesis
this means (5)(5)
is not a valid input for the sake of simplicity.
Parenthesis will always contain a valid expression (a valid combination of numbers and/or operators) inside them. "Balanced" parenthesis is defined as every (
will have an associated closing )
Evaluation
- Order of operations should be followed and the precedences are (highest to lowest):
- Parenthesis (most deeply nested first)
- Multiplication & Division
- Addition & Subtraction
- If two operators with the same precedence are occurred you should prefer going left -> right
Output
You should output the result in some way. If you don't output just number result, clarify in your answer how the output is outputted. Your output format should be consistent. Output may be a decimal, but it will always be rational, the precision is limited to your language's precision. Only if your language does not support floating point arithmetic, you do not need to support it.
Rules
- Built-ins trivializing this task are allowed but, you must clearly add
[uses built-in]
clearly to the header of the answer. This exempts your answer from winning - A "Built-ins trivializing this task" is any of:
- Something which takes in an equation and outputs the value for a/the variable
- Something which will completely simplify an equation
- Using
eval
or a related function to do a significant amount of the parsing. Use ofeval
and related functions are disallowed if they are used to (with minimal modification to the input) solve linear equations. - If you're in doubt, just ask in a comment.
- Built-ins which parse the equation are allowed
Examples
3+4=x
7
4+x=5
1
3+3*3=x
12
3x-4=7+2x
11
3--1=x
4
3*(2+4x)=7x-4
-2
1.2+2.3x=5.8
2
10=4x
2.5
INVALID Inputs:
(5)(4)=x no operator between (5) and (4)
5(x+3)=2 no operator 5 and (...)
x=y the only variable is x
4=3 there is no x
x+3=x-7 no solution
x=x infinite solutions
+5=x + is not an unary operator. -5=x would be valid though
1/(x-3)=5 Nonlinear
3/x Nonlinear
8You say that built-ins disqualify your submission, but clarify this to refer only to operations that do equation solving and parsing and the like. I think it would be clearer to use a different term, since I think of any named operation as a built-in. – xnor – 2016-03-24T04:53:49.753
How accurate do the answers have to be? – flawr – 2016-03-24T09:39:17.550
@MrPublic Your program will take a linear equation containing a single variable... – Luis Mendo – 2016-03-24T12:19:15.717
Also, does JavaScript's
eval
count as trivializing the challenge? Also, would forms ofnew Function(...)
count? – Conor O'Brien – 2016-03-26T21:19:52.197@CᴏɴᴏʀO'Bʀɪᴇɴ depends what you use it for. But assuming you're using JavaScript I don't see how it will trivialize the challenge so sure – Downgoat – 2016-03-26T21:23:21.460
Can there be
x*x-x*x+1=x
? (also linear) – l4m2 – 2018-04-16T19:29:00.740