Re: Golf Challenge - does $x and $y fit into the same slot on @z?
by Aristotle (Chancellor) on Dec 27, 2002 at 23:26 UTC
|
use strict;
use Test::More q(no_plan);
my @z = qw(1 3 27 38);
my $x = shift();
my $y = shift();
my @test = (
[qw( 4 5) => 1],
[qw( 0 -1) => 1],
[qw(100 100) => 1],
[qw( 10 27) => 1],
[qw( 3 27) => 0],
[qw( 0 100) => 0],
);
sub bool { not not shift }
for(@test) {
my ($x, $y, $works) = @$_;
is(
# 1 2 3
#23456789012345678901234567890
((grep$x<=$_,@z)==grep$y<=$_,@z),
bool($works),
"x=$x y=$y"
);
}
__END__
ok 1 - x=4 y=5
ok 2 - x=0 y=-1
ok 3 - x=100 y=100
ok 4 - x=10 y=27
ok 5 - x=3 y=27
ok 6 - x=0 y=100
1..6
Makeshifts last the longest. | [reply] [d/l] |
|
Wow. You came up with the exact same thing as I did, except for two little syntax differences. This is what I had. If I had knon that I didn't need the curly's on the grep, and if you reversed the order in your evaluations, they would be the same.
use strict;
my $x = 4;
my $y = 38;
my @z = ( 1, 3, 38, 83 );
print "Yes\n" if ((grep{$_<$x}@z)==(grep{$_<$y}@z));
| [reply] [d/l] |
Re: Golf Challenge - does $x and $y fit into the same slot on @z?
by BrowserUk (Patriarch) on Dec 28, 2002 at 00:38 UTC
|
#! perl -slw
use strict;
my @z = ( 1, 3, 27, 38 );
while( my ($x,$y) = split/,/,<DATA> ) {
# 1 2
# 1234567890123456789012345
print "YES!\n" if @z==grep$x<=$_==$y<=$_,@z;
}
__END__
4, 5
0, -1
100, 100
10, 27
3, 27
Examine what is said, not who speaks. | [reply] [d/l] |
|
#! perl -slw
use strict;
my @z = ( 1, 3, 27, 38 );
while( my ($x,$y) = split/,/,<DATA> ) {
# 1 2
# 1234567890123456789012345
print "YES!\n" if @z==grep$x>$_==$y>$_,@z;
}
__END__
4, 5
0, -1
100, 100
10, 27
3, 27
3, 3
Okay, this is not exactly fair on my part, credit goes to BrowserUK.
-gjb- | [reply] [d/l] |
|
| [reply] |
|
#! perl -slw
use strict;
my @z = ( 1, 3, 27, 38 );
while( my ($x,$y) = split/,/,<DATA> ) {
# 1 2
# 12345678901234567890
print "YES!\n" if !grep$x>$_!=$y>$_,@z;
}
__END__
4, 5
0, -1
100, 100
10, 27
3, 27
3, 3
| [reply] [d/l] |
Re: Golf Challenge - does $x and $y fit into the same slot on @z?
by sauoq (Abbot) on Dec 28, 2002 at 01:39 UTC
|
I hate to spoil the party here but all of the solutions thus far fail on one or more boundary condition.
I suggest adding two test cases:
$x = 1;
$y = 2;
@z = (0, 1);
and
$x = 0;
$y = -1;
@z = (0, 1);
It is possible that the specification was poorly written but, as it is stated, there is no mention of cases where $x and/or $y are equal to a number in the array. It stands to reason that the safe assumption is that such condition should be considered a failure condition. At the very least, it should be handled consistently. All of the current solutions handle it differently depending on which end of the array the equality occurs at.
Take, for example, the first test I proposed above: $x = 1; $y = 2; @z = (0, 1);. In that case, it obviously not true that $x and $y are between the same numbers in @z. It is also not true that they are both less than all the numbers in @z. Finally, since $x is equal to the last element in @z, it is not true that they are both greater than all elements in @z.
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] [select] |
|
| [reply] |
|
Erm. Isn't that covered by the bit that says:
#When determining "betweeness", treat any cases where $x or $y equals a value in @z as if $x or $y was actually less than that value. ?
By my (probably flawed) reading of that, your first case would be a fail because the $x would be judged less than the second element of @z
And the second case would be a pass as both $x and $y would be judges as being lower than the first valeue of @z
Examine what is said, not who speaks.
| [reply] |
|
Erm. Isn't that covered by the bit that says
Hrm. It's a debatable point, I suppose.
It explicitly states that that should be the strategy when determining "betweeness" whereas the example test cases I gave were those dealing with "greater than" or "less than" all the numbers in the array. I'm wavering more toward the poorly stated specification option now, though.
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
|
| [reply] |