diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-09-27 02:27:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-27 02:27:52 +0100 |
| commit | 94243e56bbf33115e71a8ea7333afd1147900f1f (patch) | |
| tree | 05a48764032ea1850bf43303e3f9dd3046fb2af4 /challenge-131 | |
| parent | ceb1744a52a936939e5f0d6a65797e9ed976afa9 (diff) | |
| parent | 1862140455e37a1bca800a14bf79e2a0c6c20e78 (diff) | |
| download | perlweeklychallenge-club-94243e56bbf33115e71a8ea7333afd1147900f1f.tar.gz perlweeklychallenge-club-94243e56bbf33115e71a8ea7333afd1147900f1f.tar.bz2 perlweeklychallenge-club-94243e56bbf33115e71a8ea7333afd1147900f1f.zip | |
Merge pull request #4930 from adamcrussell/challenge-131
Perl solution for challenge 131
Diffstat (limited to 'challenge-131')
| -rw-r--r-- | challenge-131/adam-russell/perl/ch-1.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-131/adam-russell/perl/ch-1.pl b/challenge-131/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..284534287e --- /dev/null +++ b/challenge-131/adam-russell/perl/ch-1.pl @@ -0,0 +1,43 @@ +use strict; +use warnings; +## +# You are given a sorted list of unique positive integers. +# Write a script to return list of arrays where the arrays +# are consecutive integers. +## +use Data::Dump q/pp/; + +sub consecutive_arrays{ + my @arrays=([]); + my $a = 0; + my $x = shift; + { + if(!@{$arrays[$a]}){ + push @{$arrays[$a]}, $x; + } + else{ + my $y = $arrays[$a]->[@{$arrays[$a]} - 1]; + if($x == $y + 1){ + push @{$arrays[$a]}, $x; + } + else{ + push @arrays, [$x]; + $a+=1; + } + } + $x = shift; + redo if $x; + } + return @arrays; +} + +MAIN:{ + print pp consecutive_arrays(1, 2, 3, 6, 7, 8, 9); + print "\n"; + print pp consecutive_arrays(11, 12, 14, 17, 18, 19); + print "\n"; + print pp consecutive_arrays(2, 4, 6, 8); + print "\n"; + print pp consecutive_arrays(1, 2, 3, 4, 5); + print "\n"; +}
\ No newline at end of file |
