diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-05-04 10:37:35 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-05-04 10:37:35 +0100 |
| commit | 3f9bfeb3ef1924567f8601d6d1b23f816c8a43b7 (patch) | |
| tree | a6c93b10824e74a7a4839d2d41d200f986c18a3c /challenge-163/james-smith | |
| parent | b64306eb9029d8d355e2172036db9f90272f89f8 (diff) | |
| download | perlweeklychallenge-club-3f9bfeb3ef1924567f8601d6d1b23f816c8a43b7.tar.gz perlweeklychallenge-club-3f9bfeb3ef1924567f8601d6d1b23f816c8a43b7.tar.bz2 perlweeklychallenge-club-3f9bfeb3ef1924567f8601d6d1b23f816c8a43b7.zip | |
adding in sanity checks so we work with numbers only
Diffstat (limited to 'challenge-163/james-smith')
| -rw-r--r-- | challenge-163/james-smith/perl/ch-1.pl | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/challenge-163/james-smith/perl/ch-1.pl b/challenge-163/james-smith/perl/ch-1.pl index f50c600cbd..88f09d24c7 100644 --- a/challenge-163/james-smith/perl/ch-1.pl +++ b/challenge-163/james-smith/perl/ch-1.pl @@ -11,25 +11,33 @@ use Data::Dumper qw(Dumper); my @TESTS = ( [ [1..3], 3 ], [ [2..4], 2 ], + [ [1..7], 42 ], + [ [1..15], 420 ], + [ [1..31], 3720 ], [ [2..4,2..4], 2 ], ## Check uniquification... + [ [qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)], 420 ], ); is( bit_sum( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; is( bit_sum_compact( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; is( bit_sum_splat( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; -is( bit_sum_splat_unique( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; is( bsm( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; +is( bit_sum_splat_unique( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; is( bsu( @{$_->[0]} ), $_->[1] ) for @TESTS; say''; done_testing(); - +## Test 15 & 18 will fail as they rely on the list being unique and +## in the 3rd test case it isn't..... sub bit_sum { my $t = 0; ## Initialize sum my %hash = map { $_ => 1 } @_; ## quick uniquification - if worried - @_ = keys %hash; ## that there may be duplicates and + @_ = map {$_+0} keys %hash; ## that there may be duplicates and ## question talks of unique pairs... + ## We need to convert these back + ## to int's otherwise bitwise & + ## will work on strings.... while(@_>1) { ## Loop through array until we have ## no pairs left @@ -42,15 +50,15 @@ sub bit_sum { sub bit_sum_compact { my $t = 0, my %h = map { $_ => 1 } @_; - @_ = keys %h; + @_ = map{0+$_}keys %h; while(@_>1) { my $a = shift; $t+= $a&$_ for @_ } $t } ##########----------##########----------##########----------##########----------##########---------- -sub bit_sum_splat{for($a=0,(%^H=map{$_,1}@_),(@_=keys%^H);@_;){$b=shift;$a+=$b&$_ for@_};$a} -sub bit_sum_splat_unique{for($a=0;@_;){$b=shift;$a+=$b&$_ for@_};$a} -sub bsm{%^H=map{$_,1}@_;@_=keys%^H;$a=0;$b=shift,map{$a+=$b&$_}@_ while@_;$a} -sub bsu{$a=0;$b=shift,map{$a+=$b&$_}@_ while@_;$a} +sub bit_sum_splat{for($a=0,(%^H=map{$_,1}@_),(@_=map{$_+0}keys%^H);@_;){$b=shift;$a+=$b&$_ for@_};$a} +sub bsm{%^H=map{$_,1}@_;@_=map{$_+0}keys%^H;$a=0;$b=shift,(map{$a+=$b&$_}@_) while@_;$a} +sub bit_sum_splat_unique{@_=map{$_+0}@_;for($a=0;@_;){$b=shift;$a+=$b&$_ for@_};$a} +sub bsu{@_=map{$_+0}@_;$a=0;$b=shift,(map{$a+=$b&$_}@_) while@_;$a} |
