diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-06-25 10:15:36 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-06-25 10:15:36 -0500 |
| commit | b7062908ebd9c9ecd432a97475ccaee9d04840bb (patch) | |
| tree | dc6a5eb46e2c1e7e7c7013225826fe1abeaa78e1 | |
| parent | ea9d20e7f81b548f49d778fd12865cfaf3692513 (diff) | |
| download | perlweeklychallenge-club-b7062908ebd9c9ecd432a97475ccaee9d04840bb.tar.gz perlweeklychallenge-club-b7062908ebd9c9ecd432a97475ccaee9d04840bb.tar.bz2 perlweeklychallenge-club-b7062908ebd9c9ecd432a97475ccaee9d04840bb.zip | |
Try task 1 with bit vectors
| -rw-r--r-- | challenge-327/bob-lied/perl/ch-1.pl | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/challenge-327/bob-lied/perl/ch-1.pl b/challenge-327/bob-lied/perl/ch-1.pl index b8944059ec..70f5da103a 100644 --- a/challenge-327/bob-lied/perl/ch-1.pl +++ b/challenge-327/bob-lied/perl/ch-1.pl @@ -41,20 +41,32 @@ exit( runBenchmark($Benchmark) ) if $Benchmark; say '(', join(', ', missing(@ARGV)), ')'; #============================================================================= -sub missing(@ints) +sub missing($ints) { - my @present = (true, (false) x @ints); - $present[$_] = true for @ints; + my @present = (true, (false) x @$ints); + $present[$_] = true for @$ints; return grep { not $present[$_] } 1 .. $#present; } +sub missing_bv($ints) +{ + my $present = ""; + vec($present, $_, 1) = 1 for 0, @$ints; + return grep { vec($present, $_, 1) == 0 } 1 .. @$ints; +} + + sub runTest { use Test2::V0; - is( [ missing(1,2,1,3,2,5) ], [4,6], "Example 1"); - is( [ missing(1,1,1) ], [2,3], "Example 2"); - is( [ missing(2,2,1) ], [3 ], "Example 3"); + is( [ missing([1,2,1,3,2,5]) ], [4,6], "Example 1"); + is( [ missing([1,1,1]) ], [2,3], "Example 2"); + is( [ missing([2,2,1]) ], [3 ], "Example 3"); + + is( [ missing_bv([1,2,1,3,2,5]) ], [4,6], "Example 1"); + is( [ missing_bv([1,1,1]) ], [2,3], "Example 2"); + is( [ missing_bv([2,2,1]) ], [3 ], "Example 3"); done_testing; } @@ -63,7 +75,10 @@ sub runBenchmark($repeat) { use Benchmark qw/cmpthese/; + my @ints = 1 .. 1000; + $ints[$_] = 1 for ( map { int(rand(1000)) } 1 .. 25 ); cmpthese($repeat, { - label => sub { }, + array => sub { missing(\@ints) }, + bitvec => sub { missing_bv(\@ints) }, }); } |
