aboutsummaryrefslogtreecommitdiff
path: root/challenge-163
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-05-09 01:25:05 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-05-09 01:25:05 +0100
commitfe8150670a12a08bb09efbca8fad17cb1ab166d3 (patch)
treec3d2e1cbd5e77b9cbc91dfc92339cbf7e6aeba3b /challenge-163
parent56b08fe9acf98e08a933ed6d7e91baf30911fe26 (diff)
downloadperlweeklychallenge-club-fe8150670a12a08bb09efbca8fad17cb1ab166d3.tar.gz
perlweeklychallenge-club-fe8150670a12a08bb09efbca8fad17cb1ab166d3.tar.bz2
perlweeklychallenge-club-fe8150670a12a08bb09efbca8fad17cb1ab166d3.zip
added some one liners
Diffstat (limited to 'challenge-163')
-rw-r--r--challenge-163/james-smith/perl/ch-1.pl20
1 files changed, 17 insertions, 3 deletions
diff --git a/challenge-163/james-smith/perl/ch-1.pl b/challenge-163/james-smith/perl/ch-1.pl
index d5ace1095c..713b316348 100644
--- a/challenge-163/james-smith/perl/ch-1.pl
+++ b/challenge-163/james-smith/perl/ch-1.pl
@@ -29,8 +29,14 @@ is( bit_sum_splat( @{$_->[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();
+
+cmpthese( 5e4, {
+ 'bs' => sub { bit_sum( @{$_->[0]}) for @TESTS; },
+ 'bsc' => sub { bit_sum_compact(@{$_->[0]}) for @TESTS; },
+ 'bss' => sub { bit_sum_splat( @{$_->[0]}) for @TESTS; },
+ 'bsm' => sub { bsm( @{$_->[0]}) for @TESTS; },
+});
## Test 15 & 18 will fail as they rely on the list being unique and
## in the 3rd test case it isn't.....
@@ -38,7 +44,7 @@ sub bit_sum {
my $t = 0; ## Initialize sum
my %hash = map { $_ => 1 } @_; ## quick uniquification - if worried
- @_ = map {$_+0} 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 &
@@ -63,6 +69,14 @@ sub bit_sum_compact {
##########----------##########----------##########----------##########----------##########----------
sub bit_sum_splat{for($a=0,(%^H=map{$_,1}@_),(@_=map{$_+0}keys%^H);@_;){$b=shift;$a+=$b&$_ for@_};$a}
-sub bsm{$a=0;%^H=map{$_,1}@_;@_=keys%^H;$b=0+pop,map{$a+=$b&$_}@_ while@_;$a}
+#sub bsm{$a=0;%^H=map{$_,1}@_;@_=keys%^H;$b=0+pop,map{$a+=$b&$_}@_ while@_;$a}
+#sub bsm{$a=0;%^H=map{$_,1}@_;@_=keys%^H;while(@_){$b=0+pop;$a+=$b&$_ for@_};$a}
+sub bsm{my$a=0;my%t=map{$_,1}@_;@_=keys%t;while(@_){my$b=0+pop;$a+=$b&$_ for@_};$a}
sub bit_sum_splat_unique{@_=map{$_+0}@_;for($a=0;@_;){$b=shift;$a+=$b&$_ for@_};$a}
sub bsu{$a=0;$b=pop,map{$a+=$b&$_}@_ while@_;$a}
+
+#### Re-written as perl 1-liners
+##########----------##########----------##########----------##########----------##########----------
+## BSM # perl -E '$a=0;%t=map{$_,1}@ARGV;@t=keys%t;$b=0+pop@t,map{$a+=$b&$_}@t while@t;say$a' ##84 chars
+## BSU # perl -E '$a=0;$b=pop,map{$a+=$b&$_}@ARGV while@ARGV;say$a' ##58 chars
+