Skip to content

Instantly share code, notes, and snippets.

@nanis
Last active August 29, 2015 14:22
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 nanis/5e65ab423c39faf9f5fe to your computer and use it in GitHub Desktop.
Save nanis/5e65ab423c39faf9f5fe to your computer and use it in GitHub Desktop.
#!/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