Skip to content

Instantly share code, notes, and snippets.

@nanis
Created June 4, 2015 15:35
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/16786cf8e6a8ecb868c8 to your computer and use it in GitHub Desktop.
Save nanis/16786cf8e6a8ecb868c8 to your computer and use it in GitHub Desktop.
My first entry in brian d foy's "Be better than Quorum challange"
#!/usr/bin/env perl
use v5.20;
=for commentary
See L<http://www.learning-perl.com/2015/06/learning-perl-challenge-be-better-than-quorum/>
I know there will be reasonable entries, so here's one for variety ;-)
-- Sinan
=cut
use feature qw(postderef signatures);
no warnings qw(experimental::postderef experimental::signatures);
use Test::More;
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 ($p, $q);
for my $x ($start .. $end) {
[sub { $p += 1}, sub { $q += 1}]->[ $x % $divisor != 0 ]->();
}
($p > $q) ? $p : $q;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment