-
Notifications
You must be signed in to change notification settings - Fork 1
/
permutations_test.ts
175 lines (159 loc) · 3.88 KB
/
permutations_test.ts
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
import {
assertEquals,
assertStrictEquals,
assertThrows,
factorial,
range,
} from "./test_deps.ts";
import { permutations } from "./permutations.ts";
import { permutationsWithReplacement } from "./permutations_with_replacement.ts";
Deno.test("r = NaN", () => {
assertThrows(
() => [...permutations("abc", NaN)],
RangeError,
"r must be a non-negative integer",
);
});
Deno.test("r = Infinity", () => {
assertThrows(
() => [...permutations("abc", Infinity)],
RangeError,
"r must be a non-negative integer",
);
});
Deno.test("r = Math.PI", () => {
assertThrows(
() => [...permutations("abc", Math.PI)],
RangeError,
"r must be a non-negative integer",
);
});
Deno.test("r = -1", () => {
assertThrows(
() => [...permutations("abc", -1)],
RangeError,
"r must be a non-negative integer",
);
});
Deno.test("n = r = 0", () => {
const actual = [...permutations("", 0)];
const expected = [[]];
assertEquals(actual, expected);
});
Deno.test("r = 0", () => {
const actual = [...permutations("abc", 0)];
const expected = [[]];
assertEquals(actual, expected);
});
Deno.test("n = 0", () => {
const actual = [...permutations("", 1)];
assertEquals(actual, []);
});
Deno.test("r > n", () => {
const actual = [...permutations("abc", 4)];
const expected: Iterable<string[]> = [];
assertEquals(actual, expected);
});
Deno.test("n = r", () => {
const actual = [...permutations("abc", 3)];
const expected = [
["a", "b", "c"],
["a", "c", "b"],
["b", "a", "c"],
["b", "c", "a"],
["c", "a", "b"],
["c", "b", "a"],
];
assertEquals(actual, expected);
});
Deno.test("r = undefined (0)", () => {
const actual = [...permutations("")];
assertEquals(actual, [[]]);
});
Deno.test("r = undefined (n)", () => {
const actual = [...permutations("abc")];
const expected = [
["a", "b", "c"],
["a", "c", "b"],
["b", "a", "c"],
["b", "c", "a"],
["c", "a", "b"],
["c", "b", "a"],
];
assertEquals(actual, expected);
});
Deno.test("r < n", () => {
const actual = [...permutations([0, 1, 2, 3], 3)];
const expected = [
[0, 1, 2],
[0, 1, 3],
[0, 2, 1],
[0, 2, 3],
[0, 3, 1],
[0, 3, 2],
[1, 0, 2],
[1, 0, 3],
[1, 2, 0],
[1, 2, 3],
[1, 3, 0],
[1, 3, 2],
[2, 0, 1],
[2, 0, 3],
[2, 1, 0],
[2, 1, 3],
[2, 3, 0],
[2, 3, 1],
[3, 0, 1],
[3, 0, 2],
[3, 1, 0],
[3, 1, 2],
[3, 2, 0],
[3, 2, 1],
];
assertEquals(actual, expected);
});
for (let n = 0; n < 8; n++) {
const iterable = range(n);
for (let r = 0; r < 8; r++) {
Deno.test(`perm(${n}, ${r})`, () => {
const actual = [...permutations(iterable, r)];
const expectedLength = perm(n, r);
assertStrictEquals(actual.length, expectedLength);
});
}
}
for (let n = 0; n < 8; n++) {
const iterable = range(n);
for (let r = 0; r < 8; r++) {
Deno.test(`permutations1([${iterable}], ${r})`, () => {
const actual = [...permutations(iterable, r)];
const expected1 = [...permutations1(iterable, r)];
assertEquals(actual, expected1);
});
}
}
/** Return the number of ways to choose `r` items from `n` items without replacement and with order. */
function perm(n: number, r: number): number {
if (!Number.isInteger(n) || n < 0) {
throw RangeError("n must be a non-negative integer");
} else if (!Number.isInteger(r) || r < 0) {
throw RangeError("r must be a non-negative integer");
} else if (r > n) {
return 0;
} else {
return factorial(n) / factorial(n - r);
}
}
/** Equivalent to `permutations` for testing. */
function* permutations1<T>(
iterable: Iterable<T>,
r: number,
): Generator<T[]> {
const pool = [...iterable];
const n = pool.length;
for (const indices of permutationsWithReplacement(range(n), r)) {
if (new Set(indices).size === r) {
yield indices.map((i) => pool[i]);
}
}
}