Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sub sample3 {
my ( $a, $b, $c, $d ) = @_;
if ( $a && $b && !$c && !$d) {
return "a and b";
}
if ( $a && !$b && $c && !$d) {
return "a and c";
}
if ( $a && !$b && !$c && $d) {
return "a and d";
}
if ( !$a && $b && $c && !$d) {
return "b and c";
}
if ( !$a && $b && !$c && $d) {
return "b and d";
}
if ( !$a && !$b && $c && $d) {
return "c and d";
}
return $a+$b+$c+$d;
}
sub bit2name{
my @bits = @_;
my @names = qw/a b c d/;
my $count = 0;
return grep{$_} map{
$count++;
$_ ? $names[$count-1] : undef;
} @bits
}
use List::Util qw/sum/;
sub answer3{
my ( @bits ) = @_;
my $sum = sum @bits;
return $sum unless $sum == 2;
return join " and ",bit2name(@bits);
}
sub check_bool_eq {
my ( $num, $func_a, $func_b ) = @_;
for my $s ( 1 .. 2**$num ) {
my @args = map { ($s & ( 1 << $_ ))? 1: 0 } ( 1 .. $num );
is( $func_a->(@args), $func_b->(@args) );
}
}
check_bool_eq( 4, \&sample3, \&answer3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.