Skip to content

Instantly share code, notes, and snippets.

@hirokidaichi
Created April 30, 2014 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokidaichi/690c0f378b7781440a3e to your computer and use it in GitHub Desktop.
Save hirokidaichi/690c0f378b7781440a3e to your computer and use it in GitHub Desktop.
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