aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Lied <boblied+github@gmail.com>2024-08-25 14:33:32 -0500
committerBob Lied <boblied+github@gmail.com>2024-08-25 14:33:32 -0500
commitfc5e5ce18da937b45a0fe047855c5cf6eb2e7e94 (patch)
treefa1e12423324940f7fec2f28198375a2934d08fc
parent16d0a043e6e2abf93c061bdcb21b3197d24ee43e (diff)
downloadperlweeklychallenge-club-fc5e5ce18da937b45a0fe047855c5cf6eb2e7e94.tar.gz
perlweeklychallenge-club-fc5e5ce18da937b45a0fe047855c5cf6eb2e7e94.tar.bz2
perlweeklychallenge-club-fc5e5ce18da937b45a0fe047855c5cf6eb2e7e94.zip
Week 283 solutions
-rw-r--r--challenge-283/bob-lied/perl/ch-1.pl23
-rw-r--r--challenge-283/bob-lied/perl/ch-2.pl37
2 files changed, 54 insertions, 6 deletions
diff --git a/challenge-283/bob-lied/perl/ch-1.pl b/challenge-283/bob-lied/perl/ch-1.pl
index 69d53c7187..9e02d1fd2d 100644
--- a/challenge-283/bob-lied/perl/ch-1.pl
+++ b/challenge-283/bob-lied/perl/ch-1.pl
@@ -38,6 +38,15 @@ sub uniqNum(@ints)
else { return "none"; }
}
+sub un2(@ints)
+{
+ use List::MoreUtils qw/singleton/;
+ my @candidate = singleton(@ints);
+ if ( @candidate > 1 ) { return "multiple"; }
+ elsif ( @candidate == 1 ) { return $candidate[0]; }
+ else { return "none"; }
+}
+
sub runTest
{
use Test2::V0;
@@ -46,11 +55,18 @@ sub runTest
is( uniqNum(3, 2, 4, 2, 4), 3, "Example 2");
is( uniqNum(1), 1, "Example 3");
is( uniqNum(4, 3, 1, 1, 1, 4), 3, "Example 4");
-
is( uniqNum(1, 2, 3), "multiple", "Multiple unique numbers");
is( uniqNum(8, 8, 8), "none", "No unique numbers");
is( uniqNum( ), "none", "Empty list");
+ is( un2(3, 3, 1), 1, "Example 1");
+ is( un2(3, 2, 4, 2, 4), 3, "Example 2");
+ is( un2(1), 1, "Example 3");
+ is( un2(4, 3, 1, 1, 1, 4), 3, "Example 4");
+ is( un2(1, 2, 3), "multiple", "Multiple unique numbers");
+ is( un2(8, 8, 8), "none", "No unique numbers");
+ is( un2( ), "none", "Empty list");
+
done_testing;
}
@@ -58,7 +74,10 @@ sub runBenchmark($repeat)
{
use Benchmark qw/cmpthese/;
+ my @ints = ( (1..100), (1..99) );
+
cmpthese($repeat, {
- label => sub { },
+ hash => sub { uniqNum(@ints) },
+ util => sub { un2(@ints) },
});
}
diff --git a/challenge-283/bob-lied/perl/ch-2.pl b/challenge-283/bob-lied/perl/ch-2.pl
index 7691fd9a28..e42d9fac7c 100644
--- a/challenge-283/bob-lied/perl/ch-2.pl
+++ b/challenge-283/bob-lied/perl/ch-2.pl
@@ -40,8 +40,29 @@ sub digCountVal(@ints)
my @appear = (0) x scalar(@ints);
$appear[$_]++ for @ints;
- use List::Util qw/all/;
- return all { $appear[$_] == $ints[$_] } 0 .. $#ints;
+ my $i = 0;
+ for ( @ints )
+ {
+ return false if $appear[$i++] != $_;
+ }
+ return true;
+}
+
+use List::Util qw/all/;
+use List::MoreUtils qw/frequency/;
+sub dc2(@ints)
+{
+ my %freq;
+ %freq = frequency(@ints);
+ return all { ($freq{$_} // 0) == $ints[$_] } 0 .. $#ints;
+}
+
+sub dc3(@ints)
+{
+ my %freq;
+ %freq = frequency(@ints);
+ my $i;
+ return all { ($freq{$i++} // 0) == $_ } @ints;
}
sub runTest
@@ -49,7 +70,11 @@ sub runTest
use Test2::V0;
is( digCountVal(1,2,1,0), true, "Example 1");
- is( digCountVal(0,3,0 ), false, "Example 1");
+ is( digCountVal(0,3,0 ), false, "Example 2");
+ is( dc2(1,2,1,0), true, "Example 1 dc2");
+ is( dc2(0,3,0 ), false, "Example 2 dc2");
+ is( dc3(1,2,1,0), true, "Example 1 dc3");
+ is( dc3(0,3,0 ), false, "Example 2 dc3");
done_testing;
}
@@ -58,7 +83,11 @@ sub runBenchmark($repeat)
{
use Benchmark qw/cmpthese/;
+ my @ints = (1, 1, 3, 1, 0, 0, 0, 0 );
+
cmpthese($repeat, {
- label => sub { },
+ array => sub { digCountVal(@ints) },
+ util => sub { dc2(@ints) },
+ index => sub { dc3(@ints) },
});
}