From 5f7d54f31adafd003af9af147217eb745ee33c36 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sat, 13 Feb 2021 00:52:13 +0000 Subject: flatten 1 map in the submitted versions - and a couple more in an albatros of golf --- challenge-097/james-smith/perl/ch-2.pl | 46 ++++++++++++++++++++++++---------- 1 file 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] } -- cgit From 3957a4fbac71408ac235fff57696f9bf84eea053 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sat, 13 Feb 2021 01:00:24 +0000 Subject: golfed even harder! --- challenge-097/james-smith/perl/ch-2.pl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl index aa1f3cfb02..42e29e5a59 100644 --- a/challenge-097/james-smith/perl/ch-2.pl +++ b/challenge-097/james-smith/perl/ch-2.pl @@ -152,10 +152,11 @@ sub mf_1{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_) 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]} +## Golf extra (less readable!) [at 125 characters] - 115 inside the curly braces.. +## but side effects as no longer localise $/ & $\ +sub mf_g{[$/=length$_[0],$\=$//$_[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],$_ -- cgit From 034057dcb7b2a08bee0a408726121320c1355fb2 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sat, 13 Feb 2021 14:46:13 +0000 Subject: golfed even further --- challenge-097/james-smith/perl/ch-2.pl | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl index 42e29e5a59..881a3a24b0 100644 --- a/challenge-097/james-smith/perl/ch-2.pl +++ b/challenge-097/james-smith/perl/ch-2.pl @@ -34,12 +34,12 @@ 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( 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 ); @@ -152,11 +152,13 @@ sub mf_1{[local$/=length$_[0],local$\=$//$_[1],map{$/=$_<$/?$_:$/}map{($_[0]^$_) 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 125 characters] - 115 inside the curly braces.. +## Golf extra (less readable!) [at 112 characters] - 104 inside the curly braces.. ## but side effects as no longer localise $/ & $\ -sub mf_g{[$/=length$_[0],$\=$//$_[1],map{$/=$_<$/?$_:$/}map{ -($_[0]^substr($_[0],$_*$_[1],$_[1])x$\)=~y/\1/\1/}0..$\-1]-> -[-1]} +## 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],$_ -- cgit