-
-
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"
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 | |
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