diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-02-13 00:19:57 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-02-13 00:19:57 +0000 |
| commit | d4a26cc664ff008ae1a723854df738101e7ee17b (patch) | |
| tree | c783f34eda4a4cdcb771b6b2b9728e4a4e6546af | |
| parent | ade5d9a9e4585f1ec707a898824558affc9a8797 (diff) | |
| download | perlweeklychallenge-club-d4a26cc664ff008ae1a723854df738101e7ee17b.tar.gz perlweeklychallenge-club-d4a26cc664ff008ae1a723854df738101e7ee17b.tar.bz2 perlweeklychallenge-club-d4a26cc664ff008ae1a723854df738101e7ee17b.zip | |
saved a byte
| -rw-r--r-- | challenge-097/james-smith/perl/ch-2.pl | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl index d625ab9d87..be87aefd86 100644 --- a/challenge-097/james-smith/perl/ch-2.pl +++ b/challenge-097/james-smith/perl/ch-2.pl @@ -121,8 +121,8 @@ done_testing(); sub min_flips { ## Golf mode on... [ - local$\=local$/=length$_[0], - $\/=$_[1], + local$/=length$_[0], + local$\=$//$_[1], map{$/=$_<$/?$_:$/} map{($_[0]^$_)=~y/\1/\1/} map{$_ x$\} @@ -132,18 +132,19 @@ sub min_flips { ]->[-1] } -sub mf_1{[local$\=local$/=length$_[0],$\/=$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_)=~y/\1/\1/}map{$_ x$\}map{substr$_[0],$_,$_[1]}map{$_*$_[1]}0..$\-1]->[-1]} -## Now as a one liner [all 156 characters] - 146 inside the curly braces.. +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_2{[local$\=local$/=length$_[0],$\/=$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_ -)=~y/\1/\1/}map{$_ x$\}map{substr$_[0],$_,$_[1]}map{$_*$_[1]}0..$\-1]->[-1]} +## Now as a one liner [all 155 characters] - 145 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]} ## With the white space back in.. sub min_flips_more_readable { return [ - local $\ = local $/ = length($_[0]), # Minimum value (bounded above by length) - $\/=$_[1], # No of blocks + 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 |
