From c0afb079eb7a7b59ee64caedef57208c63d25777 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 25 Jan 2021 16:22:15 +0000 Subject: tidied up --- challenge-097/james-smith/perl/ch-2.pl | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl index 42b107ffb7..81f89d7e4f 100644 --- a/challenge-097/james-smith/perl/ch-2.pl +++ b/challenge-097/james-smith/perl/ch-2.pl @@ -6,9 +6,11 @@ use warnings; use feature qw(say); use Test::More; +is( min_flips('101101',3), 0 ); is( min_flips('101100101',3), 1 ); is( min_flips('10110111', 4), 2 ); -is( min_flips('101100100',3), 1 ); +is( min_flips('100101100',3), 1 ); +is( min_flips('101100100100101',3), 2 ); is( min_flips('0000000100100011010001010110011110001001101010111100110111101111',4), 32 ); done_testing(); @@ -23,10 +25,12 @@ sub min_flips { ## One statement functions are a lovely perl concept - even if they can get a bit difficult ## to read.... ## + + local $/; return [ - map { local $/ = - local $\ = ( $_[0] ^ ( substr $_[0], $_, $_[1]) x (length($_[0])/$_[1]) ) =~ tr/\x01/\x01/, - !$_ || $_ < $/ ? $\ : $/, + map { + local $\ = ( $_[0] ^ (substr$_[0],$_,$_[1]) x (length($_[0])/$_[1]) ) =~ tr/\x01/\x01/, + $/ = (!$_ || ($\ < $/)) ? $\ : $/ } map { $_*$_[1] } 0 .. ( length($_[0])/$_[1] - 1 ) @@ -49,10 +53,13 @@ sub min_flips { ## Next if it is the first chunk OR the value of $\ is less than the current min ($/) ## We set $/ to $\ otherwise we leave it as $/ ## - ## The resultant array consists of the running minimum in the examples - ## for ex 1: it is [1,1,1] - ## for ex 2: it is [1,1] - ## for ex 3: it is [2,1,1] + ## The resulting array consists of the value for the node and the running minimum in the examples + ## + ## for ex 1: it is [0,0,0,0] + ## for ex 2: it is [1,1,2,1,1,1] + ## for ex 3: it is [1,1,1,1] + ## for ex 4: it is [2,2,2,2] + ## for ex 5: it is [2,2,1,1,1,1] ## ## We need the last value of this so we wrap the list into an array ref and take the last element ## -- cgit