From 632ee1d3e18fe9399b67bfe8b3eb472eefbf87df Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 2 May 2022 09:49:50 +0100 Subject: soln for ch 1 tidied up with comments --- challenge-163/james-smith/perl/ch-1.pl | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/challenge-163/james-smith/perl/ch-1.pl b/challenge-163/james-smith/perl/ch-1.pl index 2348c8b946..cd88d3bee8 100644 --- a/challenge-163/james-smith/perl/ch-1.pl +++ b/challenge-163/james-smith/perl/ch-1.pl @@ -9,14 +9,27 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my @TESTS = ( - [ 0, 1 ], + [ [1..3], 3 ], + [ [2..4], 2 ], + [ [2..4,2..4], 2 ], ## Check uniquification... ); -is( my_function($_->[0]), $_->[1] ) foreach @TESTS; - +is( bit_sum(@{$_->[0]}), $_->[1] ) for @TESTS; done_testing(); -sub my_function { - return 1; -} +sub bit_sum { + my $t = 0; ## Initialize sum + + my %hash = map { $_ => 1 } @_; ## quick uniquification - if worried + @_ = keys %hash; ## that there may be duplicates and + ## question talks of unique pairs... + + while(@_>1) { ## Loop through array until we have + ## no pairs left + my $a = shift; ## we shift off the value; + $t+= $a&$_ for @_; ## and and it with all remaining + ## elements for each element in list + } + $t; ## Return sum +} -- cgit