aboutsummaryrefslogtreecommitdiff
path: root/challenge-263/ash/perl/ch-1.pl
blob: 9b71bb2fa09e93a7dae86ef75d8591e1a205cb91 (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
# Solution to Task 1 of The Weekly Challenge 263
# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK1

# $ perl ch-1.pl
# (1, 2)
# ()
# (4)

use v5.20;
use experimental 'signatures';

my $tests = [
    [[1, 5, 3, 2, 4, 2], 2],
    [[1, 2, 4, 3, 5],    6],
    [[5, 3, 2, 4, 2, 1], 4]
];

for my $test (@$tests) {
    my $result = solve($test->[0], $test->[1]);
    say '(' . join(', ', @$result) . ')';
}

sub solve($data, $value) {
    my @sorted = sort(@$data);
    
    my @indices;
    for (my $c = 0; $c < scalar @sorted; $c++) {
        my $current = $sorted[$c];
        push @indices, $c if $current == $value;
        last if $current > $value;
    }

    return \@indices;
}