aboutsummaryrefslogtreecommitdiff
path: root/challenge-261/dave-jacoby/perl/ch-2.pl
blob: 82b437a08c430622d0f429060a870421f1bb1256 (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
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ say postderef signatures state };

use List::Util qw{ any };

my @examples = (

    { ints => [ 5, 3, 6, 1, 12 ], start => 3 },
    { ints => [ 1, 2, 4, 3 ],     start => 1 },
    { ints => [ 5, 6, 7 ],        start => 2 },
);

for my $example (@examples) {
    my $start  = $example->{start};
    my @ints   = $example->{ints}->@*;
    my $output = multiply_by_two( $start, @ints );
    my $ints   = join ',', @ints;

    say <<"END";
    Input: \@word = ($ints) and \$start = $start
    Output: $output
END
}

sub multiply_by_two ( $start, @ints ) {
    while ( any { $start == $_ } @ints ) {
        $start *= 2;
    }
    return $start;
}