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
c: an integer, the result given by the kid.
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?
- The names, specific to table, included in the code isn't subject to change.
selectis 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()