-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.pm
92 lines (77 loc) · 1.1 KB
/
Utils.pm
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package Utils;
use strict;
use warnings;
sub C
{
my ($n, $r) = @_;
return fact($n)/(fact($r) * fact($n - $r));
}
sub fact
{
my $n = shift;
my $f = 1;
for (my $i = 1; $i <= $n; ++$i) {
$f *= $i;
}
return $f;
}
sub remove_list
{
my ($a, $remove_list) = @_;
my @ret;
my %h;
foreach my $r (@$remove_list) {
$h{$r} = 1;
}
for my $v (@$a) {
if (!defined($h{$v})) {
push(@ret, $v);
}
}
return @ret;
}
sub uniq
{
my @a = @_;
my @unique;
my %h = ();
for my $v (@a) {
if (!defined($h{$v})) {
push(@unique, $v);
$h{$v} = 1;
}
}
return @unique;
}
sub padding_result
{
my @result = @_;
while (scalar(@result) < 10) {
push(@result, 0);
}
return @result[0 .. 9];
}
sub includes
{
my ($array, $value) = @_;
foreach my $v (@$array) {
if ($v == $value) {
return 1;
}
}
return undef;
}
sub intersection_count
{
my ($a1, $a2) = @_;
my $ok = 0;
foreach my $v1 (@$a1) {
foreach my $v2 (@$a2) {
if ($v1 == $v2) {
++$ok;
}
}
}
return $ok;
}
1;