-
Notifications
You must be signed in to change notification settings - Fork 0
/
a1.c
338 lines (292 loc) · 8.4 KB
/
a1.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "test.h"
#include <string.h> // for testing generate_splits()
/*
* Generate k-selections of a[0..n-1] in lexicographic order and call process_selection to process them.
*
* The array b[] will have enough space to hold k elements.
* For a selection i1, ..., ik, you should set b[0] = a[i1], ..., b[k-1] = a[ik].
* Selections should be generated in lexicographic order.
* a[0..k-1] is the smallest selection and a[n-k..n-1] is the largest.
*/
void process_selection(int *b, int k, void *data) {
for (int i = 0; i < k; i++) {
printf("%d ", b[i]);
}
printf("\n");
}
void generate_selections_0(int a[], int n, int k, int b[], int pos, int idx, void *data, void (*process_selection)(int *b, int k, void *data)) {
if (pos == k) {
process_selection(b, k, data);
return;
}
for (int i = idx; i < n; i++) {
b[pos] = a[i];
generate_selections_0(a, n, k, b, pos + 1, i + 1, data, process_selection);
}
}
void generate_selections(int a[], int n, int k, int b[], void *data, void (*process_selection)(int *b, int k, void *data)) {
generate_selections_0(a, n, k, b, 0, 0, data, process_selection);
}
/*
* See Exercise 2 (a), page 94 in Jeff Erickson's textbook.
* The exercise only asks you to count the possible splits.
* In this assignment, you have to generate all possible splits into buf[]
* and call process_split() to process them.
* The dictionary parameter is an array of words, sorted in dictionary order.
* nwords is the number of words in this dictionary.
*/
#include <stdio.h>
#include <string.h>
int IsWord(const char *str, const char *dict[], int nwords) {
for (int i = 0; i < nwords; i++) {
if (strcmp(str, dict[i]) == 0) {
return 1;
}
}
return 0;
}
void generate_splits_recursive(const char *source, const char *dict[], int nwords, char buf[], int buf_index, void *data, void (*process_split)(char buf[], void *data)) {
int len = strlen(source);
for (int i = 1; i <= len; i++) {
strncpy(buf + buf_index, source, i);
buf[buf_index + i] = '\0';
if (IsWord(buf + buf_index, dict, nwords)) {
if (i == len) {
process_split(buf, data);
} else {
buf[buf_index + i] = ' ';
generate_splits_recursive(source + i, dict, nwords, buf, buf_index + i + 1, data, process_split);
}
}
}
}
void generate_splits(const char *source, const char *dict[], int nwords, char buf[], void *data, void (*process_split)(char buf[], void *data)) {
generate_splits_recursive(source, dict, nwords, buf, 0, data, process_split);
}
void process_split_example(char buf[], void *data) {
printf("%s\n", buf);
}
/*
* Transform a[] so that it becomes the previous permutation of the elements in it.
* If a[] is the first permutation, leave it unchanged.
*/
#include <stdio.h>
#include <stdbool.h>
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
bool previous_permutation(int a[], int n) {
int i = n - 2;
while (i >= 0 && a[i] <= a[i + 1]) {
i--;
}
if (i < 0) {
return false;
}
int j = n - 1;
while (a[j] >= a[i]) {
j--;
}
int temp = a[i];
a[i] = a[j];
a[j] = temp;
int left = i + 1;
int right = n - 1;
while (left < right) {
temp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
}
return true;
}
void print_array(int a[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
/* Write your tests here. Use the previous assignment for reference. */
typedef struct {
int index;
int err;
int first;
} state_t;
static void test_selections_2165(int b[], int k, void *data)
{
state_t *s = (state_t *)data;
if (s->first) {
s->err = 0;
s->first = 0;
}
switch (s->index) {
case 0:
if ((b[0] != 2) || (b[1] != 1)) {
s->err = 1;
}
break;
case 1:
if ((b[0] != 2) || (b[1] != 6)) {
s->err = 1;
}
break;
case 2:
if ((b[0] != 2) || (b[1] != 5)) {
s->err = 1;
}
break;
case 3:
if ((b[0] != 1) || (b[1] != 6)) {
s->err = 1;
}
break;
case 4:
if ((b[0] != 1) || (b[1] != 5)) {
s->err = 1;
}
break;
case 5:
if ((b[0] != 6) || (b[1] != 5)) {
s->err = 1;
}
break;
default:
s->err = 1;
}
++(s->index);
}
void count_selections(int b[], int k, void *data)
{
int *d = (int*)data;
++*d;
}
typedef struct {
int b[100];
} selection_t;
void last_selection(int b[], int k, void *data)
{
selection_t *s = (selection_t*)data;
for (int i = 0; i < k; ++i) {
s->b[i] = b[i];
}
}
BEGIN_TEST(generate_selections) {
int a[] = { 2, 1, 6, 5 };
int aa[] = { 1, 5, 3, 0, 1, 12, 4, 3, 6, 6 };
int bb[24];
for (int i = 0; i < 24; ++i) {
bb[i] = i;
}
int b[12];
int c = 0;
state_t s2165 = { .index = 0, .err = 1, .first = 1 };
generate_selections(a, 4, 2, b, &s2165, test_selections_2165);
ASSERT(!s2165.err, "Failed on 2 1 6 5.");
generate_selections(aa, 10, 5, b, &c, count_selections);
ASSERT_EQ(c, 252, "Failed on 10C5.");
selection_t s;
generate_selections(aa, 10, 5, b, &s, last_selection);
ASSERT_ARRAY_VALUES_EQ(s.b, 5, "Failed on last of 10C5.", 12, 4, 3, 6, 6);
c = 0;
generate_selections(bb, 24, 12, b, &c, count_selections);
ASSERT_EQ(c, 2704156, "Failed on 24C12");
generate_selections(bb, 24, 12, b, &s, last_selection);
ASSERT_ARRAY_VALUES_EQ(s.b, 12, "Failed on last of 24C12", 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);
} END_TEST
void test_splits_art(char buf[], void *data)
{
state_t *s = (state_t*)data;
if (s->first) {
s->err = 0;
s->first = 0;
}
switch (s->index) {
case 0:
if (strcmp(buf, "art is toil")) {
s->err = 1;
}
break;
case 1:
if (strcmp(buf, "artist oil")) {
s->err = 1;
}
break;
default:
s->err = 1;
}
++(s->index);
}
void count_splits(char buf[], void *data)
{
int *c = (int *)data;
++(*c);
}
BEGIN_TEST(generate_splits) {
const char *dict[] = {
"art",
"artist",
"is",
"oil",
"toil"
};
const char *dict_a[] = {
"a",
"aa",
"aaa",
"aaaa",
"aaaaa",
"aaaaaa",
"aaaaaaa",
"aaaaaaaa",
"aaaaaaaaa",
"aaaaaaaaaa"
};
state_t s = { .index = 0, .err = 1, .first = 1 };
char buf[34000];
char long_source[16000 + 1];
for (int i = 0; i < 16000; ++i) {
long_source[i] = 'a';
}
long_source[16000] = 0;
int c;
generate_splits("artistoil", dict, 5, buf, &s, test_splits_art);
ASSERT(!s.err, "Failed on \'artistoil\'.");
c = 0;
generate_splits("aaaaaaaaaa", dict_a, 1, buf, &c, count_splits);
ASSERT_EQ(c, 1, "Failed on \'aaaaaaaaaa\' with one split.");
c = 0;
generate_splits("aaaaaaaaaa", dict_a, 10, buf, &c, count_splits);
ASSERT_EQ(c, 512, "Failed on \'aaaaaaaaaa\' with binary splits.");
c = 0;
generate_splits(long_source, dict_a, 1, buf, &c, count_splits);
ASSERT_EQ(c, 1, "Failed on long source.");
c = 0;
generate_splits("aaaaaaaaaa", dict_a, 2, buf, &c, count_splits);
ASSERT_EQ(c, 89, "Failed on Fibonacci split.");
} END_TEST
BEGIN_TEST(previous_permutation) {
int a[] = { 1, 5, 6, 2, 3, 4 };
previous_permutation(a, 6);
ASSERT_ARRAY_VALUES_EQ(a, 6, "Failed on 1 5 6 2 3 4.", 1, 5, 4, 6, 3, 2);
int aa[] = { 1, 2, 3, 5, 4, 6 };
previous_permutation(aa, 3); // 3 is correct.
ASSERT_ARRAY_VALUES_EQ(aa, 3, "Failed on 1 2 3.", 1, 2, 3);
previous_permutation(aa, 1);
ASSERT_ARRAY_VALUES_EQ(aa, 6, "Failed on aa, 1.", 1, 2, 3, 5, 4, 6);
int bb[] = { 1, 1, 1, 1 };
previous_permutation(bb, 4);
ASSERT_ARRAY_VALUES_EQ(bb, 4, "Failed on 4 1s.", 1, 1, 1, 1);
previous_permutation(aa+3, 3);
ASSERT_ARRAY_VALUES_EQ(aa, 6, "Failed on last part of aa.", 1, 2, 3, 4, 6, 5);
} END_TEST
int main()
{
run_tests((test_t[]) {
TEST(generate_selections),
TEST(generate_splits),
TEST(previous_permutation),
0
});
return 0;
}