-
Notifications
You must be signed in to change notification settings - Fork 0
/
permute.pl
199 lines (137 loc) · 5.32 KB
/
permute.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#====================================================================
#
# Filename: permute.pl
# Author: Ozan Akcin
# Created: 2017-01-22
# Purpose: Return unique permutations of an array
# Notes: We turn off 'recursion' warning as our permute
# function calls itself and 100 is no longer a big
# worry (but use your judgement) but we *do* exercise
# a sanity-check (see sub sanityCheck).
#
#====================================================================
#!/usr/bin/perl -w
use strict;
use warnings;
no warnings 'recursion'; #---turning off default 100-level recursion warning
my ( $arr, @arr, $lim1, $lim2, @permutations, $arrLen );
my ( $threshold, @mapidx, $cnt, $warnMsg, $dieMsg);
my ( @srcArray, $defUseAll, $useAll, $defRankByExt, $rankByExt, %permRank );
#---task-specific settings-------------------------------------------
$defUseAll = 0; #---use every term in each permutation?
$threshold = 5913; #--->die after this many iterations (basic sanity check)
$lim1 = 5; #--->return ranked permutations report only
$lim2 = 7; #--->don't run (without iterator)
$cnt = 0;
#--------------------------------------------------------------------
#==============================================================================
sub initArgs
{
die "Received empty preference terms array - try again." if ( ! $ARGV[0] );
@arr = split /\,/, $ARGV[0];
$useAll = defined($ARGV[1]) ? $ARGV[1] : $defUseAll;
$dieMsg = "That's too many!! Please narrow down your choice terms.\n";
}
#==============================================================================
#==============================================================================
sub sanityCheck
{
#-----------------------------------------------------------------
# As we're running a plain-vanilla recursion with no tail call
# optimization in our permute function for the moment, we keep a
# close-check on the recursion depth for now to avoid a blow-up.
# Future improvements could include:
# a). Use tail call optimization
# b). Turn our permute function into an iterator to avoid memory
# issues and introduce the option to simply list all permutations
# rather than actually check availability on these as well.
#-----------------------------------------------------------------
my ( $permNum, $incr );
$permNum = 1;
$incr = 1;
$permNum *= $incr++ while $incr <= scalar(@arr);
print "Preparing: ".$permNum." (all-terms) permutations..."."\n";
die $dieMsg if scalar(@arr) > $lim2;
}
#==============================================================================
#==============================================================================
sub swapidx
{
my ( $idx_1, $idx_2 ) = @_;
($mapidx[$idx_1], $mapidx[$idx_2]) = ( $mapidx[$idx_2], $mapidx[$idx_1] );
}
#==============================================================================
#==============================================================================
sub permute
{
#-----------------------------------------------------------------
# Beginning from the end of our input array, swap pairs of
# elements until we have iterated through all indices
#-----------------------------------------------------------------
$cnt++;
#---first commit form as-is:
push @permutations, join("",@srcArray[@mapidx]);
my ( $idx1, $idx2 ) = ($arrLen-1,$arrLen);
while ( $idx1 >= 0 and $mapidx[$idx1] > $mapidx[$idx1+1] ){
$idx1--;
return(0) if ( $idx1 < 0 );
}
while ( $mapidx[$idx1] > $mapidx[$idx2] ) {
$idx2--;
}
swapidx($idx2, $idx1++);
$idx2 = $arrLen;
while ( $idx2 > $idx1 ) {
swapidx($idx2--,$idx1++)
}
die("Exiting after a lot of recursions ($cnt)...") if $cnt > $threshold;
#---loop such that all cnt-1 to end pairings have reversed
permute();
}
#==============================================================================
#==============================================================================
sub runPermute
{
if ( ! $useAll ) {
#---allow parsimonious permutations starting with most favored terms
if ( scalar(@arr) > 1) {
for (my $idx=0; $idx <= $#arr; $idx++){
@srcArray = @arr[(0..$idx)];
if ( scalar(@srcArray) == 1 ) {
push @permutations, $srcArray[0];
next;
} else {
$arrLen = $#srcArray;
@mapidx = (0..$arrLen);
permute()
}
}
} else {
push @permutations, $arr[0];
}
} else {
@srcArray = @arr;
$arrLen = $#srcArray;
@mapidx = (0..$arrLen);
permute();
}
}
#==============================================================================
#==============================================================================
sub report
{
foreach my $rung ( @permutations ) {
print "$rung\n";
}
}
#==============================================================================
exit(main());
#==============================================================================
sub main
{
initArgs;
sanityCheck;
runPermute;
report;
}
#==============================================================================