diff options
| author | James Smith <js5@sanger.ac.uk> | 2023-01-22 18:54:06 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-22 18:54:06 +0000 |
| commit | c5fb373217d001ef624971e2b600297b4dcaaad8 (patch) | |
| tree | 0c6148dabf473364e8ea2959caac4b07fc9e3d32 | |
| parent | 4cd1c92bf54b17c2af1044266e1779bb071e8828 (diff) | |
| download | perlweeklychallenge-club-c5fb373217d001ef624971e2b600297b4dcaaad8.tar.gz perlweeklychallenge-club-c5fb373217d001ef624971e2b600297b4dcaaad8.tar.bz2 perlweeklychallenge-club-c5fb373217d001ef624971e2b600297b4dcaaad8.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-200/james-smith/perl/ch-1.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-200/james-smith/perl/ch-1.pl b/challenge-200/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..5025939f0a --- /dev/null +++ b/challenge-200/james-smith/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); + +my @TESTS = ( + [ [1..4], '(1,2,3), (1,2,3,4), (2,3,4)' ], + [ [1,3,4,5], '(3,4,5)' ], + [ [1,2,3,5,6,7,9,0,-4,-8], '(0,-4,-8), (1,2,3), (5,6,7)' ], + [ [1,1,1,1,2,3,4,6,8,10,15,20,25,50,100,125,150,175,200], '(1,1,1), (1,1,1), (1,1,1,1), (1,2,3), (1,2,3,4), (10,15,20), (10,15,20,25), (100,125,150), (100,125,150,175), (100,125,150,175,200), (125,150,175), (125,150,175,200), (15,20,25), (150,175,200), (2,3,4), (4,6,8), (4,6,8,10), (6,8,10)' ], + [ [2], "" ], + [ [1,2,4,8,16,32], "" ], +); +is( a_slices( @{$_->[0]}), $_->[1] ) foreach @TESTS; +done_testing(); + +## Display an array of arrays compactly... +sub d_slices { return join ', ', map { '('.join(',', @{$_}).')' } @{$_[0]}; } + + +sub a_slices { + ## Less than 1 value return... + return unless $#_; + ## Set start of sequence to 0, d - the difference between entry 1 and entry 0 + my($st,$d,@pairs)=(0,$_[1]-$_[0]); + ## Loop through all end points + for(my$en=1;$en<@_;$en++) { + ## If the gap is different - update gap (and start) and continute through loop + if($_[$en]-$_[$en-1] != $d) { + ($st,$d)=($en-1,$_[$en]-$_[$en-1]) + ## If it is the same add it and all alternative entries + } else { + ## We only store the start/end of the runs not the whole sequence + push( @pairs, map { [$_,$en] } $st..$en-2 ); + } + } + ## Now we find all the start ends and return the series of each of these subsequences. + return [ map { [ @_[ $_->[0] .. $_->[1] ] ] } @pairs ]; +} + |
