aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-02-13 00:52:13 +0000
committerdrbaggy <js5@sanger.ac.uk>2021-02-13 00:52:13 +0000
commit5f7d54f31adafd003af9af147217eb745ee33c36 (patch)
tree2e270094568d6385351c173c152c32a6822a1cb2
parentd4a26cc664ff008ae1a723854df738101e7ee17b (diff)
downloadperlweeklychallenge-club-5f7d54f31adafd003af9af147217eb745ee33c36.tar.gz
perlweeklychallenge-club-5f7d54f31adafd003af9af147217eb745ee33c36.tar.bz2
perlweeklychallenge-club-5f7d54f31adafd003af9af147217eb745ee33c36.zip
flatten 1 map in the submitted versions - and a couple more in an albatros of golf
-rw-r--r--challenge-097/james-smith/perl/ch-2.pl46
1 files changed, 33 insertions, 13 deletions
diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl
index be87aefd86..aa1f3cfb02 100644
--- a/challenge-097/james-smith/perl/ch-2.pl
+++ b/challenge-097/james-smith/perl/ch-2.pl
@@ -27,6 +27,20 @@ is( mf_2('101100100100101',3), 2 );
is( mf_2('101101',3), 0 );
is( mf_2('0000000100100011010001010110011110001001101010111100110111101111',4), 32 );
+is( mf_3('101100101',3), 1 );
+is( mf_3('10110111', 4), 2 );
+is( mf_3('100101100',3), 1 );
+is( mf_3('101100100100101',3), 2 );
+is( mf_3('101101',3), 0 );
+is( mf_3('0000000100100011010001010110011110001001101010111100110111101111',4), 32 );
+
+is( mf_g('101100101',3), 1 );
+is( mf_g('10110111', 4), 2 );
+is( mf_g('100101100',3), 1 );
+is( mf_g('101100100100101',3), 2 );
+is( mf_g('101101',3), 0 );
+is( mf_g('0000000100100011010001010110011110001001101010111100110111101111',4), 32 );
+
is( min_flips_more_readable('101100101',3), 1 );
is( min_flips_more_readable('10110111', 4), 2 );
is( min_flips_more_readable('100101100',3), 1 );
@@ -126,30 +140,36 @@ sub min_flips {
map{$/=$_<$/?$_:$/}
map{($_[0]^$_)=~y/\1/\1/}
map{$_ x$\}
- map{substr$_[0],$_,$_[1]}
- map{$_*$_[1]}
+ map{substr$_[0],$_*$_[1],$_[1]}
0..$\-1
]->[-1]
}
-sub mf_1{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_)=~y/\1/\1/}map{$_ x$\}map{substr$_[0],$_,$_[1]}map{$_*$_[1]}0..$\-1]->[-1]}
+sub mf_1{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_)=~y/\1/\1/}map{$_ x$\}map{substr$_[0],$_*$_[1],$_[1]}0..$\-1]->[-1]}
-## Now as a one liner [all 155 characters] - 145 inside the curly braces..
+## Now as a one liner [all 148 characters] - 138 inside the curly braces..
sub mf_2{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_)
=~y/\1/\1/}map{$_ x$\}map{substr$_[0],$_,$_[1]}map{$_*$_[1]}0..$\-1]->[-1]}
+## Golf extra (less readable!) [at 135 characters] - 125 inside the curly braces..
+sub mf_g{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?
+$_:$/}map{($_[0]^substr($_[0],$_*$_[1],$_[1])x$\)=~y/\1/\1/}
+0..$\-1]->[-1]}
+
+sub mf_3{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?
+$_:$/}map{($_[0]^$_)=~y/\1/\1/}map{$_ x$\}map{substr$_[0],$_
+*$_[1],$_[1]}0..$\-1]->[-1]}
## With the white space back in..
sub min_flips_more_readable {
return [
- local $/ = length($_[0]), # Minimum value (bounded above by length)
- local $\ = $/ / $_[1], # No of blocks
- map { $/ = $_ < $/ ? $_ : $/ } # Update min if first block OR value < min
- map { ( $_[0] ^ $_ ) =~ y/\1/\1/ } # Get number of flipped letters
- map { $_ x $\ } # Repeat sub-block so same length as string
- map { substr $_[0], $_, $_[1] } # Get "nth" sub-block
- map { $_ * $_[1] } # Convert index to start location
- 0 .. $\ - 1 # Block indexes...
- ]->[ -1 ]; # Get last value from list {minimum}
+ local $/ = length($_[0]), # Minimum value (bounded above by length)
+ local $\ = $/ / $_[1], # No of blocks
+ map { $/ = $_ < $/ ? $_ : $/ } # Update min if first block OR value < min
+ map { ( $_[0] ^ $_ ) =~ y/\1/\1/ } # Get number of flipped letters
+ map { $_ x $\ } # Repeat sub-block so same length as string
+ map { substr $_[0], $_ * $_[1], $_[1] } # Get "nth" sub-block
+ 0 .. $\ - 1 # Block indexes...
+ ]->[-1]
}