aboutsummaryrefslogtreecommitdiff
path: root/challenge-131/mohammad-anwar/perl/ch-1.pl
blob: 8f32b97e6d1e453a5a6d7e64fa9b5b7cd2eaa069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl

=head1

Week 131:

    https://theweeklychallenge.org/blog/perl-weekly-challenge-131

Task #1: Consecutive Arrays

    You are given a sorted list of unique positive integers.

    Write a script to return list of arrays where the arrays are
    consecutive integers.

=cut

use strict;
use warnings;

use Test::More;
use Test::Deep;

is_deeply( [ consecutive_arrays(1, 2, 3, 6, 7, 8, 9) ],
           [ [1, 2, 3], [6, 7, 8, 9] ],
           'Example 1');

is_deeply( [ consecutive_arrays(11, 12, 14, 17, 18, 19) ],
           [ [11, 12], [14], [17, 18, 19] ],
           'Example 2');

is_deeply( [ consecutive_arrays(2, 4, 6, 8) ],
           [ [2], [4], [6], [8] ],
           'Example 3');

is_deeply( [ consecutive_arrays(1, 2, 3, 4, 5) ],
           [ [1, 2, 3, 4, 5] ],
           'Example 4');

done_testing;

sub consecutive_arrays {
    my @numbers = @_;

    my @arrays    = ();
    my $sub_array = [];
    my $i;
    foreach my $number (@numbers) {
        $i = $number unless defined $i;
        if ($i + 1 == $number) {
            push @$sub_array, $number;
            $i = $number;
        }
        else {
            push @arrays, $sub_array if scalar @$sub_array;;
            $sub_array = [$number];
            $i = $number;
        }
    }

    push @arrays, $sub_array if scalar @$sub_array;;

    return @arrays;
}