The challenge given to me is comparatively easy, and specific to MySQL only. I'm given a table expressions
which have mathematical calculations done by a kid. Basically, I've to select
all the right calculations!
The table have the following properties,
a
: an integer, the left operand.b
: an integer, the right operand.operation
: a char, any one operation from'+'
,'-'
,'*'
and'/'
(pure-div).c
: an integer, the result given by the kid.
Example
a b operation c
------------------
2 3 + 5
4 2 / 3
6 1 * 9
8 5 - 3
As we can see, the second and third operations are wrongly calculated, so they need to be filtered out. Here is what I've done till now,
MySQL, 88 chars
Rule: 88 chars is calculated by removing all spaces (\s+
) from the given code. This is MySQL, so they simply take amount of chars (calculated here).
select * from expressions
where if(
'+' = @o := operation,
a + b,
if(
@o = '-',
a - b,
if(
@o = '*',
a * b,
a / b
)
)
) = c
But, I'm sure that this can be golfed further, as many other submissions are of 73
chars, which is 15
bytes fewer than mine!
I think I need to change the idea of my solution, but I can't find anything which can evaluate expressions directly in MySQL. Any ideas?
Rules
- The names, specific to table, included in the code isn't subject to change.
select
is considered to be the only method to output/result.- For multiple statements, it should be wrapped inside
begin ... end
, as the whole code is the body of a function, defined as,
CREATE PROCEDURE expressionsVerification()
operation
orexpressions
in your code would reduce bytes, are they acceptable golfs? \$\endgroup\$mysql
tag, as in the example post? \$\endgroup\$oper
at the top, butoperation
in your rextester code and link. Which of the two should it be? Also, I barely know anything about MySQL, but maybe you can somehow create an array[a+b,a-b,a*b,a/b]
, get the index of the operator-character in"+-*/"
and use it to index into the created value array? Not sure how to accomplish it, and not sure if it's shorter though. Just an idea. \$\endgroup\$view
definition? This will provide the ultimative solution:select * from e where c = r
. Notee
is view onexpressions
that defines a columnr
with the rigth answer. This is the database approach. \$\endgroup\$