From 47164c5668824375c62fbfee63f59cac01b64227 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sat, 30 Jan 2021 12:29:15 +0000 Subject: move blog.txt and more notes.. and golf --- challenge-096/james-smith/blog.txt | 1 + challenge-096/james-smith/perl/blog.txt | 1 - challenge-097/james-smith/blog.txt | 1 + challenge-097/james-smith/perl/blog.txt | 1 - challenge-097/james-smith/perl/ch-2.pl | 40 ++++++++++++++++++++------------- 5 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 challenge-096/james-smith/blog.txt delete mode 100644 challenge-096/james-smith/perl/blog.txt create mode 100644 challenge-097/james-smith/blog.txt delete mode 100644 challenge-097/james-smith/perl/blog.txt diff --git a/challenge-096/james-smith/blog.txt b/challenge-096/james-smith/blog.txt new file mode 100644 index 0000000000..04c32fcd59 --- /dev/null +++ b/challenge-096/james-smith/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/james_curtis-smith/2021/01/perl-weekly-challenge-96.html diff --git a/challenge-096/james-smith/perl/blog.txt b/challenge-096/james-smith/perl/blog.txt deleted file mode 100644 index 04c32fcd59..0000000000 --- a/challenge-096/james-smith/perl/blog.txt +++ /dev/null @@ -1 +0,0 @@ -http://blogs.perl.org/users/james_curtis-smith/2021/01/perl-weekly-challenge-96.html diff --git a/challenge-097/james-smith/blog.txt b/challenge-097/james-smith/blog.txt new file mode 100644 index 0000000000..5b18ca0d4b --- /dev/null +++ b/challenge-097/james-smith/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/james_curtis-smith/2021/01/perl-weekly-challenge-97.html diff --git a/challenge-097/james-smith/perl/blog.txt b/challenge-097/james-smith/perl/blog.txt deleted file mode 100644 index 5b18ca0d4b..0000000000 --- a/challenge-097/james-smith/perl/blog.txt +++ /dev/null @@ -1 +0,0 @@ -http://blogs.perl.org/users/james_curtis-smith/2021/01/perl-weekly-challenge-97.html diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl index 72ed38f422..d84ab14199 100644 --- a/challenge-097/james-smith/perl/ch-2.pl +++ b/challenge-097/james-smith/perl/ch-2.pl @@ -71,14 +71,15 @@ done_testing(); ## just have to have the same length as our original string - ## by performing a perl "string multiplication" x ## -## * map { [ $_->[0], ( $_[0] ^ $_->[1] ) =~ y/\x01/\x01/ ] } - +## * map { [ $_->[0], ( $_[0] ^ $_->[1] ) =~ y/\1/\1/ ] } - ## count the flips. Two perlisms here - we can use xor operator ## ^ on strings to xor the binary values of each string. ## y/../../ - the translate operator returns the number of ## substitutions it makes - in this case we are substituting ## the ASCII character with decimal a hex value of "01"... when -## the strings are same the byte value of the xor is 0 or "\x00" -## and when they are different the value is 1 or "\x01" +## the strings are same the byte value of the xor is 0 or as +## a character "\0" and when they are different the value is 1 +## or as a characther "\1" ## ## * map { $/ = !$_->[0] || $_->[1] < $/ ? $_->[1] : $/ } - finally ## we keep the running total of the minimum value - Now this is @@ -95,32 +96,41 @@ done_testing(); ## ## * And we return this value. + sub min_flips { ## Golf mode on... - return[ + [ local$/, local$\=length($_[0])/$_[1], map{$/=!$_->[0]||$_->[1]<$/?$_->[1]:$/} - map{[$_->[0],($_[0]^$_->[1])=~y/\x01/\x01/]} + map{[$_->[0],($_[0]^$_->[1])=~y/\1/\1/]} map{[$_->[0],$_->[1]x$\]} map{[$_,substr$_[0],$_,$_[1]]} map{$_*$_[1]} 0..$\-1 - ]->[-1]; + ]->[-1] } +## Now as a one liner [all 213 cha.... + +sub mf_1{[local$/,local$\=length($_[0])/$_[1],map{$/=!$_->[0]||$_->[1]<$/?$_->[1]:$/}map{[$_->[0],($_[0]^$_->[1])=~y/\1/\1/]}map{[$_->[0],$_->[1]x$\]}map{[$_,substr$_[0],$_,$_[1]]}map{$_*$_[1]}0..$\-1]->[-1]} + +sub mf_1wrap{[local$/,local$\=length($_[0])/$_[1],map{$/=!$_->[0]||$_-> +[1]<$/?$_->[1]:$/}map{[$_->[0],($_[0]^$_->[1])=~y/\1/\1/]}map{[$_->[0], +$_->[1]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]) / $_[1], + local $/, # Accumulator for min value + local $\ = length($_[0]) / $_[1], # No of blocks - map { $/ = !$_->[0] || $_->[1] < $/ ? $_->[1] : $/ } - map { [ $_->[0], ( $_[0] ^ $_->[1] ) =~ y/\x01/\x01/ ] } - map { [ $_->[0], $_->[1] x $\ ] } - map { [ $_, substr $_[0], $_, $_[1] ] } - map { $_ * $_[1] } - 0 .. $\ - 1 - ]->[-1]; + map { $/ = !$_->[0] || $_->[1] < $/ ? $_->[1] : $/ } # Update min if first block OR value < min + map { [ $_->[0], ( $_[0] ^ $_->[1] ) =~ y/\1/\1/ ] } # Get number of flipped letters + map { [ $_->[0], $_->[1] 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} } -- cgit