-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical Assessment_practice 18_19_q1
305 lines (245 loc) · 9.67 KB
/
Practical Assessment_practice 18_19_q1
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
// Practical Assessment_practice 18_19
// QUESTION 1
// 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 <<<");
}
}
//===============================================================
//===============================================================
// TASK 1A
//===============================================================
function make_big_int_from_number(num) {
function make_list(num){
return num === 0
? null
: pair(num%10, make_list(math_floor(num/10)));
}
return num === 0
? list(0)
: make_list(num);
}
// TASK 1A TESTS
assert("1A_1", () => make_big_int_from_number(0),
list(0), []);
assert("1A_2", () => make_big_int_from_number(5),
list(5), []);
assert("1A_3", () => make_big_int_from_number(10),
list(0,1), []);
assert("1A_4", () => make_big_int_from_number(1234),
list(4,3,2,1), []);
assert("1A_5", () => make_big_int_from_number(9876543210),
list(0,1,2,3,4,5,6,7,8,9), []);
//===============================================================
// TASK 1B
//===============================================================
function big_int_to_string(bint) {
const lst = reverse(bint);
function helper(lst){
return is_null(lst)
? ""
: stringify(head(lst)) + helper(tail(lst));
}
return helper(lst);
}
// TASK 1B TESTS
assert("1B_1", () => big_int_to_string(list(0)),
"0", []);
assert("1B_2", () => big_int_to_string(list(5)),
"5", []);
assert("1B_3", () => big_int_to_string(list(0,1)),
"10", []);
assert("1B_4", () => big_int_to_string(list(4,3,2,1)),
"1234", []);
assert("1B_5", () => big_int_to_string(list(0,1,2,3,4,5,6,7,8,9)),
"9876543210", []);
assert("1B_6", () => big_int_to_string(
list(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9)),
"9876543210987654321", []);
//===============================================================
// TASK 1C
//===============================================================
function big_int_add(bintX, bintY) {
// You may modify the given partial implementation,
// or remove it and write your own.
function add(x, y, carry) {
if (is_null(x) && is_null(y)) {
return (carry === 0) ? null : pair(carry, null);
} else {
if (is_null(x)){
if (carry === 0){
return y;
}
else {
const sum = head(y) + carry;
return pair(sum%10, add(x, tail(y), math_floor(sum/10)));
}
}
else if (is_null(y)){
if (carry === 0){
return x;
}
else {
const sum = head(x) + carry;
return pair(sum%10, add(tail(x), y, math_floor(sum/10)));
}
}
else{
const sum = head(x) + head(y) + carry;
return pair(sum%10, add(tail(x), tail(y), math_floor(sum/10)));
}
}
}
return add(bintX, bintY, 0);
}
// TASK 1C TESTS
assert("1C_1", () => big_int_add(list(0), list(3,2,1)),
list(3,2,1), ["make_big_int_from_number"]);
assert("1C_2", () => big_int_add(list(5,6,7), list(0)),
list(5,6,7), ["make_big_int_from_number"]);
assert("1C_3", () => big_int_add(list(4,3,2,1), list(5,4,3,2)),
list(9,7,5,3), ["make_big_int_from_number"]);
assert("1C_4", () => big_int_add(list(7,8,9), list(5,6)),
list(2,5,0,1), ["make_big_int_from_number"]);
assert("1C_5", () => big_int_add(list(5,6), list(7,8,9)),
list(2,5,0,1), ["make_big_int_from_number"]);
assert("1C_6", () => big_int_add(
list(9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9), list(5)),
list(4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1),
["make_big_int_from_number"]);
//===============================================================
// TASK 1D
//===============================================================
function big_int_mult_by_digit(bint, digit) {
function helper(bint, digit, carry){
if (is_null(bint)){
return (carry === 0) ? null : pair(carry, null);
}
else {
const product = (head(bint) * digit) + carry;
return pair(product%10, helper(tail(bint), digit, math_floor(product/10)));
}
}
return digit === 0
? list(0)
: helper(bint, digit, 0);
}
// TASK 1D TESTS
assert("1D_1", () => big_int_mult_by_digit(list(0), 5),
list(0), ["make_big_int_from_number", "big_int_add"]);
assert("1D_2", () => big_int_mult_by_digit(list(7,4,3), 0),
list(0), ["make_big_int_from_number", "big_int_add"]);
assert("1D_3", () => big_int_mult_by_digit(list(7,4,3), 5),
list(5,3,7,1), ["make_big_int_from_number", "big_int_add"]);
assert("1D_4", () => big_int_mult_by_digit(
list(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9), 3),
list(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7,2),
["make_big_int_from_number", "big_int_add"]);
//===============================================================
// TASK 1E
//===============================================================
function big_int_mult_by_10_pow_n(bint, n) {
function helper(bint, n){
return n === 0
? bint
: pair(0, helper(bint, n-1));
}
return head(bint) === 0 && length(bint) === 1
? list(0)
: helper(bint, n);
}
// TASK 1E TESTS
assert("1E_1", () => big_int_mult_by_10_pow_n(list(0), 5),
list(0),
["make_big_int_from_number", "big_int_add", "big_int_mult_by_digit"]);
assert("1E_2", () => big_int_mult_by_10_pow_n(list(7,4,3), 0),
list(7,4,3),
["make_big_int_from_number", "big_int_add", "big_int_mult_by_digit"]);
assert("1E_3", () => big_int_mult_by_10_pow_n(list(7,4,3), 3),
list(0,0,0,7,4,3),
["make_big_int_from_number", "big_int_add", "big_int_mult_by_digit"]);
assert("1E_4", () => big_int_mult_by_10_pow_n(list(5,8,3,1), 20),
list(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,8,3,1),
["make_big_int_from_number", "big_int_add", "big_int_mult_by_digit"]);
//===============================================================
// TASK 1F
//===============================================================
function big_int_mult(bintX, bintY) {
function helper(primary, secondary, pow){
return is_null(secondary)
? null
: big_int_add(helper(primary, tail(secondary), pow + 1), big_int_mult_by_10_pow_n(big_int_mult_by_digit(primary, head(secondary)), pow));
}
return (head(bintX) === 0 && length(bintX) === 1) || (head(bintY) === 0 && length(bintY) === 1)
? list(0)
: helper(bintX, bintY, 0);
}
// TASK 1F TESTS
assert("1F_1", () => big_int_mult(list(0), list(0)),
list(0),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
assert("1F_2", () => big_int_mult(list(0), list(3,2,1)),
list(0),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
assert("1F_3", () => big_int_mult(list(3,2,1), list(0)),
list(0),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
assert("1F_4", () => big_int_mult(list(3,2,1), list(1)),
list(3,2,1),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
assert("1F_5", () => big_int_mult(list(9), list(6)),
list(4,5),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
assert("1F_6", () => big_int_mult(list(7,8,9), list(5,6)),
list(5,5,1,4,6),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
assert("1F_7", () => big_int_mult(
list(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), list(7,8,9)),
list(7,8,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,8,9),
["make_big_int_from_number", "big_int_add",
"big_int_mult_by_digit", "big_int_mult_by_10_pow_n"]);
//===============================================================