diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-25 19:02:32 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-25 19:02:32 +0000 |
| commit | 64689b2bd7b6b4d559198c33e168353666e27db8 (patch) | |
| tree | f61b56b75ebf22d1a51be4fb6bd0562d078db0be | |
| parent | aada61e5eb256d12fcae121374efd10a5b59391a (diff) | |
| parent | c0afb079eb7a7b59ee64caedef57208c63d25777 (diff) | |
| download | perlweeklychallenge-club-64689b2bd7b6b4d559198c33e168353666e27db8.tar.gz perlweeklychallenge-club-64689b2bd7b6b4d559198c33e168353666e27db8.tar.bz2 perlweeklychallenge-club-64689b2bd7b6b4d559198c33e168353666e27db8.zip | |
Merge pull request #3371 from drbaggy/master
challenge 97
| -rw-r--r-- | challenge-097/james-smith/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-097/james-smith/perl/ch-2.pl | 68 |
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-097/james-smith/perl/ch-1.pl b/challenge-097/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..d6dfd58a56 --- /dev/null +++ b/challenge-097/james-smith/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( caesar('ABCDEFGHIJKLMNOPQRSTUVWXYZ',3), 'XYZABCDEFGHIJKLMNOPQRSTUVW' ); +is( caesar('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG',3), 'QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD' ); + +done_testing(); + +sub caesar { + return join q(), map { m{[A-Z]} ? chr 65+(ord($_)-65-$_[1])%26 : $_ } split m{}, $_[0]; +} + diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..81f89d7e4f --- /dev/null +++ b/challenge-097/james-smith/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/local/bin/perl + +use strict; + +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('100101100',3), 1 ); +is( min_flips('101100100100101',3), 2 ); +is( min_flips('0000000100100011010001010110011110001001101010111100110111101111',4), 32 ); + +done_testing(); + +sub min_flips { + ## Golf mode on... + ## + ## This was to use this to serve as an example of perl idioms that other programmers + ## may find it difficult to understand - and so I tried to put as many of them in relatively + ## short function.... + ## + ## One statement functions are a lovely perl concept - even if they can get a bit difficult + ## to read.... + ## + + local $/; + return [ + map { + local $\ = ( $_[0] ^ (substr$_[0],$_,$_[1]) x (length($_[0])/$_[1]) ) =~ tr/\x01/\x01/, + $/ = (!$_ || ($\ < $/)) ? $\ : $/ + } + map { $_*$_[1] } + 0 .. ( length($_[0])/$_[1] - 1 ) + ]->[-1]; + ## We could use variales here - but playing with localised special variables is fun! + ## $/ <- minimum value + ## $\ <- value for given chunk.... + ## + ## The inner map + range combination returns a list of ids of the form 0, $n, 2*$n until the + ## end of the array, we use this to chunk the array up for comparison + ## + ## Inside the end map - we first string "xor" the string with the repeated chunk of the array. + ## so e.g. for the first example this becomes: + ## '101100101' ^ '101'.'101'.'101' + ## '101100101' ^ '100'.'100'.'100' + ## '101100101' ^ '101'.'101'.'101' + ## if the symbols match you get a value \x00, when they don't match you get value \x01 + ## so we then count the \x01's using tr... and store in $\ + ## + ## 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 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 + ## + ## [ list... ]->[-1]; +} + |
