aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-163/james-smith/perl/ch-1.pl25
1 files 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
+}