diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-05-02 09:49:50 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-05-02 09:49:50 +0100 |
| commit | 632ee1d3e18fe9399b67bfe8b3eb472eefbf87df (patch) | |
| tree | 0a0687b60a3e3b3e4a3d88b425e5d7d52b721e4e /challenge-163 | |
| parent | 1d90daab5c06100eebd541c96ea05601145fc713 (diff) | |
| download | perlweeklychallenge-club-632ee1d3e18fe9399b67bfe8b3eb472eefbf87df.tar.gz perlweeklychallenge-club-632ee1d3e18fe9399b67bfe8b3eb472eefbf87df.tar.bz2 perlweeklychallenge-club-632ee1d3e18fe9399b67bfe8b3eb472eefbf87df.zip | |
soln for ch 1 tidied up with comments
Diffstat (limited to 'challenge-163')
| -rw-r--r-- | challenge-163/james-smith/perl/ch-1.pl | 25 |
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 +} |
