-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical Assessment_practice 18_19_q2
270 lines (223 loc) · 8.09 KB
/
Practical Assessment_practice 18_19_q2
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
// Practical Assessment_practice 18_19
// QUESTION 2
// Instructions for students who are using this for practice:
//
// (1) Copy and paste this entire file into the editor of Source Academy
// Playground at https://sourceacademy.nus.edu.sg/playground
// (2) Write your solution for each task in the Source Academy Playground.
// (3) Run the program to test your solution on the given testcases.
//===============================================================
// These functions are provided for running the testcases
// in the Source Academy Playground.
// They are NOT part of the actual testing facility provided
// in the actual Practical Assessment.
//===============================================================
// Tests whether arrays A and B are structurally equal.
function equal_array(A, B) {
if (!is_array(A) || !is_array(B)) {
return false;
} else if (array_length(A) !== array_length(B)) {
return false;
} else {
let is_equal = true;
const len = array_length(A);
for (let i = 0; is_equal && i < len; i = i + 1) {
if (is_array(A[i]) || is_array(B[i])) {
is_equal = equal_array(A[i], B[i]);
} else {
is_equal = equal(A[i], B[i]);
}
}
return is_equal;
}
}
// NOTE: This is NOT the actual assert function used
// in the actual Practical Assessment.
function assert(test_name, test_func, truth, dependence) {
const result = test_func();
const is_equal = (is_array(truth)? equal_array(result, truth)
: equal(result, truth));
if (is_equal) {
display(test_name + ": PASSED");
} else {
display(test_name + ": FAILED <<<");
}
}
//===============================================================
//===============================================================
// DO NOT REMOVE OR MODIFY THE FOLLOWING FUNCTIONS.
// You may call them in your functions.
//===============================================================
function swap(A, i, j) {
const temp = A[i];
A[i] = A[j];
A[j] = temp;
}
//---------------------------------------------------------------
function copy_array(A) {
const len = array_length(A);
const B = [];
for (let i = 0; i < len; i = i + 1) {
B[i] = A[i];
}
return B;
}
//---------------------------------------------------------------
function reverse_array(A) {
const len = array_length(A);
const half_len = math_floor(len / 2);
for (let i = 0; i < half_len; i = i + 1) {
swap(A, i, len - 1 - i);
}
}
//---------------------------------------------------------------
function array_to_list(A) {
const len = array_length(A);
let L = null;
for (let i = len - 1; i >= 0; i = i - 1) {
L = pair(A[i], L);
}
return L;
}
//---------------------------------------------------------------
function list_to_array(L) {
const A = [];
let i = 0;
for (let p = L; !is_null(p); p = tail(p)) {
A[i] = head(p);
i = i + 1;
}
return A;
}
//---------------------------------------------------------------
// Sorts the array of numbers in ascending order.
function sort_ascending(A) {
const len = array_length(A);
for (let i = 1; i < len; i = i + 1) {
const x = A[i];
let j = i - 1;
while (j >= 0 && A[j] > x) {
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = x;
}
}
//---------------------------------------------------------------
function digits_to_string(digits) {
const len = array_length(digits);
let str = "";
for (let i = 0; i < len; i = i + 1) {
str = str + stringify(digits[i]);
}
return str;
}
// const D = [8, 3, 9, 2, 8, 1];
// digits_to_string(D); // returns "839281"
//===============================================================
// DO NOT REMOVE OR MODIFY THE ABOVE FUNCTIONS.
//===============================================================
//===============================================================
// TASK 2A
//===============================================================
function build_largest_int(digits) {
sort_ascending(digits);
reverse_array(digits);
return digits_to_string(digits);
}
// TASK 2A TESTS
assert("2A_1", () => build_largest_int([1]),
"1", []);
assert("2A_2", () => build_largest_int([1,2,3,4,5]),
"54321", []);
assert("2A_3", () => build_largest_int([9,8,7]),
"987", []);
assert("2A_4", () => build_largest_int([4,1,9,1,4,9,1]),
"9944111", []);
assert("2A_5", () => build_largest_int([5,5,5,5,5,5,7,5,5,5]),
"7555555555", []);
assert("2A_6", () => build_largest_int([5,5,5,5,5,5,5,5,5,5]),
"5555555555", []);
//===============================================================
// TASK 2B
//===============================================================
function build_2nd_largest_int(digits) {
sort_ascending(digits);
const length = array_length(digits);
let i = 0;
while (i + 1 < length && digits[i] === digits[i+1]){
i = i + 1;
}
if (length === 1 || i === length - 1){
return digits_to_string(digits);
}
else {
swap(digits, i, i + 1);
reverse_array(digits);
return digits_to_string(digits);
}
}
// TASK 2B TESTS
assert("2B_1", () => build_2nd_largest_int([1]),
"1", ["build_largest_int"]);
assert("2B_2", () => build_2nd_largest_int([1,2,3,4,5]),
"54312", ["build_largest_int"]);
assert("2B_3", () => build_2nd_largest_int([9,8,7]),
"978", ["build_largest_int"]);
assert("2B_4", () => build_2nd_largest_int([4,1,9,1,4,9,1]),
"9941411", ["build_largest_int"]);
assert("2B_5", () => build_2nd_largest_int([5,5,5,5,5,5,7,5,5,5]),
"5755555555", ["build_largest_int"]);
assert("2B_6", () => build_2nd_largest_int([5,5,5,5,5,5,5,5,5,5]),
"5555555555", ["build_largest_int"]);
//===============================================================
// TASK 2C
//===============================================================
function build_nth_largest_int(digits, n) {
function permutations(s){
return is_null(s)
? list(null)
: accumulate(append, null,
map(x => map(p => pair(x, p),
permutations(remove(x,s))),
s));
}
const lst = array_to_list(digits);
const perm = permutations(lst);
const mapp = map(list_to_array, perm);
const map2 = map(digits_to_string, mapp);
const arr = list_to_array(map2);
sort_ascending(arr);
reverse_array(arr);
const length = array_length(arr);
if (n - 1 >= length){
return arr[length - 1];
}
else {
return arr[n-1];
}
}
// TASK 2C TESTS
assert("2C_1", () => build_nth_largest_int([1,2,4,3], 1),
"4321", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_2", () => build_nth_largest_int([3,1,4,2], 2),
"4312", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_3", () => build_nth_largest_int([3,1,4,2], 10),
"3214", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_4", () => build_nth_largest_int([1,3,4,2], 18),
"2134", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_5", () => build_nth_largest_int([3,1,4,2], 24),
"1234", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_6", () => build_nth_largest_int([4,3,2,1], 28),
"1234", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_7", () => build_nth_largest_int([5,3,7], 1),
"753", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_8", () => build_nth_largest_int([3,5,7], 4),
"537", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_9", () => build_nth_largest_int([5,3,7], 6),
"357", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_10", () => build_nth_largest_int([5,3,7], 10),
"357", ["build_largest_int", "build_2nd_largest_int"]);
assert("2C_11", () => build_nth_largest_int([5], 10),
"5", ["build_largest_int", "build_2nd_largest_int"]);
//===============================================================