diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-14 04:34:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-14 04:34:04 +0000 |
| commit | 9247dd9840b98bb834b5f1263cff286eb3b2f567 (patch) | |
| tree | 88480baa5b33e1f869d3248846231dad7240eb01 | |
| parent | 332c8d4fe4811353e6bd76c6b3a526238228605d (diff) | |
| parent | 10f9036a427c3d38ec3dbc5ef1428b90b4443c27 (diff) | |
| download | perlweeklychallenge-club-9247dd9840b98bb834b5f1263cff286eb3b2f567.tar.gz perlweeklychallenge-club-9247dd9840b98bb834b5f1263cff286eb3b2f567.tar.bz2 perlweeklychallenge-club-9247dd9840b98bb834b5f1263cff286eb3b2f567.zip | |
Merge pull request #3510 from drbaggy/master
OK - probably the last one - golfed ch-2 down to 104 characters!
| -rw-r--r-- | challenge-097/james-smith/perl/ch-2.pl | 49 |
1 files changed, 36 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..881a3a24b0 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('101100101',3), 1 ); +is( mf('10110111', 4), 2 ); +is( mf('100101100',3), 1 ); +is( mf('101100100100101',3), 2 ); +is( mf('101101',3), 0 ); +is( mf('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,39 @@ 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 112 characters] - 104 inside the curly braces.. +## but side effects as no longer localise $/ & $\ +## Note as $a/$b are used in sort - you don't need to my them even though we are using strict... + +sub mf{(($a,$b)=@_,$/=length$a,$\=$//$b, +map{$/=$_<$/?$_:$/}map{($a^substr($a,$_* +$b,$b)x$\)=~y/\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] } |
