"be consistent" PerlMonks

### Re: List all different equations for a given one

by kcott (Chancellor)
 on Sep 24, 2013 at 20:33 UTC ( #1055539=note: print w/replies, xml ) Need Help??

in reply to List all different equations for a given one

G'day jess195,

Welcome to the monastery.

"I need to check what user enters against my answer. Say I have x + 1 as a correct answer, but user entered 1 + x. How can I build something that would check for all associativity and commutativity in a given equation?"

My thoughts on this was to substitute the variables with real values and then compare the results. However, looking at some of your subsequent replies in this thread, I'm wondering if you really do want to compare two answers or generate a list of all possible answers: assuming the former, here's some code I've been playing around with that you can possibly adapt to your needs.

```#!/usr/bin/env perl

use strict;
use warnings;
use constant DEBUG => 0;
use constant FUZZINESS => 1e-15;

use Scalar::Util 'looks_like_number';

my %vals = map { \$_ => rand } 'a' .. 'z';

my @equations = (
'x + y',
'x - y',
'x * y',
'x / y',
'x % y',
'x ^ y',
'x * (y + z)',
'x / (y + z)',
'(x + y) * (x + y)',
'(x + y) * (x - y)',
);

EQUATION:
for my \$equation (@equations) {
TRY:
while (1) {
print '-' x 40, "\n";
print "Base equation: \$equation\n";
print 'Try/Skip/Quit (t/s/q) [t]: ';
my \$choice = <>;
next EQUATION if \$choice =~ /^s/i;
last EQUATION if \$choice =~ /^q/i;
print 'Equivalent equation: ';
my \$try = <>;
chomp \$try;
if (equivalent(\$equation, \$try)) {
print "'\$equation' and '\$try' are equivalent.\n";
print 'Try again (y/n) [n]: ';
my \$choice = <>;
next TRY if \$choice =~ /^y/i;
last TRY;
}
else {
print "'\$equation' and '\$try' are not equivalent.\n";
next TRY;
}
}
}

sub equivalent {
my (\$equation, \$try) = @_;

my \$eqn_val = get_value(\$equation);
return 0 unless defined \$eqn_val;

my \$try_val = get_value(\$try);
return 0 unless defined \$try_val;

return abs(\$eqn_val - \$try_val) < FUZZINESS ? 1 : 0;
}

sub get_value {
my \$in_equation = shift;

print "get_value(\$in_equation)\n" if DEBUG;
(my \$equation = \$in_equation) =~ s/([a-z])/\$vals{\$1}/g;
\$equation =~ s/\^/**/g;
print "SUBS: \$equation\n" if DEBUG;

my \$value = eval {
eval \$equation;
};

if (\$@) {
warn \$@;
return undef;
}

if (not defined \$value && length \$value && looks_like_number \$valu
+e) {
return undef;
}

print "VALUE: \$value\n" if DEBUG;

return \$value;
}

Here's some example output:

```'x + y' and 'y + x' are equivalent.
...
'x - y' and 'y - x' are not equivalent.
...
'x - y' and 'x-y' are equivalent.
...
'x * (y + z)' and 'x*y + x*z' are equivalent.
...
'x * (y + z)' and 'z * x + y * x' are equivalent.
...
'x / (y + z)' and 'x/y + x/z' are not equivalent.
...
'(x + y) * (x + y)' and 'x^2 + 2*x*y + y^2' are equivalent.
...
'(x + y) * (x - y)' and 'x^2 + y^2' are not equivalent.
...
'(x + y) * (x - y)' and 'x^2 - y^2' are equivalent.

-- Ken

Replies are listed 'Best First'.
Re^2: List all different equations for a given one
by jess195 (Novice) on Sep 25, 2013 at 02:46 UTC

Thanks for bringing this to my attention! I'll test against ranges of numbers instead, which looks to be the most reasonable solution!

Create A New User
Node Status?
node history
Node Type: note [id://1055539]
help
Chatterbox?
and God said, "Let Newton be!"...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-11-23 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (343 votes). Check out past polls.

Notices?