-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinatorics.java
More file actions
93 lines (87 loc) · 1.77 KB
/
Combinatorics.java
File metadata and controls
93 lines (87 loc) · 1.77 KB
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
package hackpack;
import java.util.ArrayList;
public class Combinatorics {
int[] items;
boolean[] inSubset;
/**
* Access all subsets in a set
* Total subsets = 2^n
* @param i current set item
*/
void subset(int i){
if(i == items.length){
// process completed subset
}
inSubset[i] = true;
subset(i+1);
inSubset[i] = false;
subset(i+1);
}
//int[] items
int[] perm;
boolean[] used;
/**
* Processes each permutation of a set
* Maximum of 10 items without restrictions
* Total permutations = avalible!/(avalible - needed)!
* @param i
*/
void permute(int i){
if(i == items.length){
// process the permutation
}
for(int j = 0; j < items.length;j++){
if(used[j] == false){
used[j] = true;
perm[i] = items[j];
permute(i + 1);
used[j] = false;
}
}
}
/**
* Gets the nth permutation of a lexicographically
* sorted set, given no repetitions in the set
* @param set The unique set to use
* @param n The specific permutation to find
* @return The nth permutation
*/
String nthPerm(ArrayList<String> set,int n){
StringBuilder s = new StringBuilder();
long factorial;
int pos;
while (set.size() > 0){
factorial = factorial(set.size() - 1);
pos = (int)(n/factorial);
s.append(set.get(pos));
set.remove(pos);
}
return s.toString();
}
long factorial(int n){
for(int i = 2;i < n;i++){
n *= i;
}
return n;
}
// int[] items
boolean[] inCombo;
int degree;
/**
* Process each combination in a set
* Total combinations = avalible!/((avalible-needed)!needed!)
* @param i
* @param in
*/
void combo(int i, int in){
if(in == degree){
// process the combination
}
if(i < items.length){
inCombo[i] = true;
combo(i+1,in+1);
inCombo[i] = false;
combo(i+1,in);
}
}
}