line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Combinations;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
614
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings;
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Exporter;
|
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
897
|
|
7
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
8
|
|
|
|
|
|
|
our @EXPORT = qw(combinations_without_repetition
|
9
|
|
|
|
|
|
|
combinations_with_repetition
|
10
|
|
|
|
|
|
|
num_rem);
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '1.00';
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub combinations_without_repetition {
|
15
|
0
|
|
|
0
|
0
|
|
my ($ref_words, $k) = @_;
|
16
|
0
|
|
|
|
|
|
my $n = requirements(@_);
|
17
|
0
|
0
|
|
|
|
|
$n || return;
|
18
|
0
|
|
|
|
|
|
my $count = 0;
|
19
|
0
|
|
|
|
|
|
my $ret_all = ();
|
20
|
0
|
|
|
|
|
|
my ($i, @out_num);
|
21
|
0
|
|
|
|
|
|
for $i (0..$k-1) {
|
22
|
0
|
|
|
|
|
|
$out_num[$i] = $i;
|
23
|
|
|
|
|
|
|
}
|
24
|
0
|
|
|
|
|
|
++$count;
|
25
|
0
|
|
|
|
|
|
my $ret_ref = \&add_arr_comb;
|
26
|
0
|
|
|
|
|
|
$ret_all .= ${&$ret_ref(\@out_num, $count, $ref_words)};
|
|
0
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
while() {
|
28
|
0
|
|
|
|
|
|
$i = $k - 1;
|
29
|
0
|
|
0
|
|
|
|
while($i >= 0 && $out_num[$i] == $n - $k + $i) {
|
30
|
0
|
|
|
|
|
|
--$i;
|
31
|
|
|
|
|
|
|
}
|
32
|
0
|
0
|
|
|
|
|
return $ret_all if $i < 0;
|
33
|
0
|
|
|
|
|
|
++$out_num[$i];
|
34
|
0
|
|
|
|
|
|
for($i++; $i < $k; $i++) {
|
35
|
0
|
|
|
|
|
|
$out_num[$i] = $out_num[$i - 1] + 1;
|
36
|
|
|
|
|
|
|
}
|
37
|
0
|
|
|
|
|
|
++$count;
|
38
|
0
|
|
|
|
|
|
$ret_all .= ${&$ret_ref(\@out_num, $count, $ref_words)};
|
|
0
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
}
|
40
|
|
|
|
|
|
|
}
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub combinations_with_repetition {
|
43
|
0
|
|
|
0
|
0
|
|
my ($ref_words, $k) = @_;
|
44
|
0
|
|
|
|
|
|
my $n = requirements(@_);
|
45
|
0
|
0
|
|
|
|
|
$n || return;
|
46
|
0
|
|
|
|
|
|
my $count = 0;
|
47
|
0
|
|
|
|
|
|
my $ret_all = ();
|
48
|
0
|
|
|
|
|
|
my ($i, @out_num);
|
49
|
0
|
|
|
|
|
|
for $i (0..$k-1) {
|
50
|
0
|
|
|
|
|
|
$out_num[$i] = 0;
|
51
|
|
|
|
|
|
|
}
|
52
|
0
|
|
|
|
|
|
++$count;
|
53
|
0
|
|
|
|
|
|
my $ret_ref = \&add_arr_comb;
|
54
|
0
|
|
|
|
|
|
$ret_all .= ${&$ret_ref(\@out_num, $count, $ref_words)};
|
|
0
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
while() {
|
56
|
0
|
|
|
|
|
|
$i = $k - 1;
|
57
|
0
|
|
0
|
|
|
|
while($i >= 0 && $out_num[$i] == $n - 1) {
|
58
|
0
|
|
|
|
|
|
--$i;
|
59
|
|
|
|
|
|
|
}
|
60
|
0
|
0
|
|
|
|
|
return $ret_all if $i < 0;
|
61
|
0
|
|
|
|
|
|
++$out_num[$i];
|
62
|
0
|
|
|
|
|
|
for($i++; $i < $k; $i++) {
|
63
|
0
|
|
|
|
|
|
$out_num[$i] = $out_num[$i - 1];
|
64
|
|
|
|
|
|
|
}
|
65
|
0
|
|
|
|
|
|
++$count;
|
66
|
0
|
|
|
|
|
|
$ret_all .= ${&$ret_ref(\@out_num, $count, $ref_words)};
|
|
0
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
}
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub requirements {
|
71
|
0
|
|
|
0
|
0
|
|
my ($ref_words, $k) = @_;
|
72
|
0
|
0
|
0
|
|
|
|
if ($k <= 0 or $k > @$ref_words) {
|
73
|
0
|
|
|
|
|
|
print "Requirements:\n";
|
74
|
0
|
|
|
|
|
|
print "k - integer, k > 0\n";
|
75
|
0
|
|
|
|
|
|
print "k < or = size of array\n";
|
76
|
0
|
|
|
|
|
|
print "Quit\n";
|
77
|
0
|
|
|
|
|
|
return;
|
78
|
|
|
|
|
|
|
} else {
|
79
|
0
|
|
|
|
|
|
scalar @$ref_words;
|
80
|
|
|
|
|
|
|
}
|
81
|
|
|
|
|
|
|
}
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub add_arr_comb {
|
84
|
0
|
|
|
0
|
0
|
|
my ($ref_out_num, $count, $ref_words) = @_;
|
85
|
0
|
|
|
|
|
|
my $ret = ();
|
86
|
0
|
|
|
|
|
|
$ret .= "($count) ";
|
87
|
0
|
|
|
|
|
|
for my $i (0..$#$ref_out_num) {
|
88
|
0
|
|
|
|
|
|
$ret .= "$ref_words->[$ref_out_num->[$i]] ";
|
89
|
|
|
|
|
|
|
}
|
90
|
0
|
|
|
|
|
|
\($ret .= "\n");
|
91
|
|
|
|
|
|
|
}
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub num_rem {
|
94
|
0
|
|
|
0
|
0
|
|
my $out = ();
|
95
|
0
|
|
|
|
|
|
foreach (split(/\n/, shift)) {
|
96
|
0
|
|
|
|
|
|
s/^\(\d+\) //;
|
97
|
0
|
|
|
|
|
|
$out .= $_."\n";
|
98
|
|
|
|
|
|
|
}
|
99
|
0
|
|
|
|
|
|
$out;
|
100
|
|
|
|
|
|
|
}
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1;
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__
|