-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.pl
52 lines (47 loc) · 1.29 KB
/
ch-2.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use experimental qw{ say postderef signatures state };
use Carp;
use List::Util qw{ max uniq };
my @input = (
[ 1, 3, 3, 2 ],
[ 1, 2, 1, 3 ],
[ 1, 3, 2, 1, 2 ],
[ 1, 1, 2, 3, 2 ],
[ 2, 1, 2, 1, 1 ],
);
for my $input (@input) {
my @x = $input->@*;
my $x = join ', ', @x;
my @o = array_degree(@x);
my $o = join ', ', @o;
say <<"END";
Input: \@array = ($x)
Output: ($o)
END
}
sub array_degree ( @array ) {
my @output;
my %hash;
for my $i ( uniq sort @array ) {
my $j = scalar grep { $_ == $i } @array;
$hash{$i} = $j;
}
my $max = max values %hash;
my @n = grep { $max == $hash{$_} } uniq sort @array;
for my $n (@n) {
for my $i ( 0 .. -1 + scalar @array ) {
for my $j ( $i + 1 .. -1 + scalar @array ) {
my @sub = @array[ $i .. $j ];
my $c = scalar grep { $_ == $n } @sub;
next if $sub[0] ne $n; # starts with the digit
next if $sub[-1] ne $n; # ends with the digits
next if $c != $max; # contains all the occurances
push @output, [@sub];
}
}
}
@output = sort { scalar $a->@* <=> $b->@* } @output;
return $output[0]->@*;
}