From b6300ccb79c3911b759033f5d751661916ac5520 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 19 Dec 2022 09:11:47 +0000 Subject: Create ch-2.pl --- challenge-196/james-smith/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-196/james-smith/perl/ch-2.pl 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 ',', @{$_} } @_ } -- cgit