aboutsummaryrefslogtreecommitdiff
path: root/challenge-263/dave-jacoby/perl/ch-1.pl
blob: 304bb4774bb120c88c17a2562fa0654943be1c0d (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
#!/usr/bin/env perl

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

my @examples = (

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

for my $example (@examples) {
    my @output = target_index($example);
    my $output = join ', ', @output;
    my $ints   = join ', ', $example->{ints}->@*;
    my $k      = $example->{k};
    say <<"END";
    Input:  \@ints = ($ints), \$k = $k
    Output: ($output)
END
}

sub target_index ($obj) {
    my @sorted = sort { $a <=> $b } $obj->{ints}->@*;
    my $k      = $obj->{k};
    my @output = grep { $k == $sorted[$_] } 0 .. $#sorted;
    return @output;
}