-
-
Save nanis/5e65ab423c39faf9f5fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use v5.20; | |
=for commentary | |
This is my second entry in brian d foy's "Be Better than Quorum" challenge | |
See L<http://www.learning-perl.com/2015/06/learning-perl-challenge-be-better-than-quorum/> | |
I went for baroqueness with this one. | |
-- Sinan | |
=cut | |
use feature qw(postderef signatures); | |
no warnings qw(experimental::postderef experimental::signatures); | |
use Test::More; | |
use List::Util qw( max ); | |
my @tests = ( | |
# start end divisor answer | |
[ 0, 10, 3, 7 ], | |
[ 0, 20, 3, 14 ], | |
[ 5, 100, 37, 94 ], | |
[ -9, 9, 4, 14 ], | |
); | |
foreach my $row ( @tests ) { | |
is( count( $row->@[0..2] ), $row->[-1] ); | |
} | |
done_testing(); | |
sub count ( $start, $end, $divisor ) { | |
my @counters = map make_condition_counter($_), | |
sub { !($_[0] % $divisor) }, # divisible | |
sub { ($_[0] % $divisor) }, # not divisible | |
; | |
for my $v ($start .. $end) { | |
$_->($v) for @counters; | |
} | |
max map $_->(), @counters; | |
} | |
sub make_condition_counter { | |
my $condition = shift; | |
my $n = 0; | |
sub { | |
$n += (!!$condition->(@_)) if @_; | |
$n; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment