aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-12-19 09:11:47 +0000
committerGitHub <noreply@github.com>2022-12-19 09:11:47 +0000
commitb6300ccb79c3911b759033f5d751661916ac5520 (patch)
treef761abb10ea19e0c861756184fe02fe2b7838238
parent5622cc99422fbf8389c0b02c727f40863307fe25 (diff)
downloadperlweeklychallenge-club-b6300ccb79c3911b759033f5d751661916ac5520.tar.gz
perlweeklychallenge-club-b6300ccb79c3911b759033f5d751661916ac5520.tar.bz2
perlweeklychallenge-club-b6300ccb79c3911b759033f5d751661916ac5520.zip
Create ch-2.pl
-rw-r--r--challenge-196/james-smith/perl/ch-2.pl26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-196/james-smith/perl/ch-2.pl b/challenge-196/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..fe42f2b8b2
--- /dev/null
+++ b/challenge-196/james-smith/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/local/bin/perl
+
+use strict;
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+
+my @TESTS = (
+ [ [1,3,4,5,7], '( [3,5] )' ],
+ [ [1,2,3,6,7,9], '( [1,3], [6,7] )' ],
+ [ [0,1,2,4,5,6,8,9], '( [0,2], [4,6], [8,9] )' ],
+ [ [1,3,5], '( )' ],
+);
+
+is( dmp( range( @{$_->[0]} ) ), $_->[1] ) for @TESTS;
+done_testing();
+
+sub range {
+ my $s = my $e = shift, my @r;
+ ($_[0]==$e+1) ? ( $e=shift ) : ( $s==$e || push(@r,[$s,$e]) , $e=$s=shift ) while @_;
+ push @r, [$s,$e] unless $s==$e;
+ @r
+}
+
+sub dmp { sprintf '( %s )', join ', ', map { sprintf '[%s]', join ',', @{$_} } @_ }