-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical Assessment_practice 18_19_q3
235 lines (198 loc) · 7.26 KB
/
Practical Assessment_practice 18_19_q3
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
// Practical Assessment_practice 18_19
// TOTAL: 45/50
// QUESTION 3
// 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 3A(I)
//===============================================================
function count_lower_neighbors(emap, r, c) {
const rows = array_length(emap);
const columns = array_length(emap[0]);
if (emap[r][c] === undefined || r === 0 || c === 0 || r === rows-1 || c=== columns-1){
return 0;
}
else {
let count = 0;
const current = emap[r][c];
let flag = false;
for(let i = r-1; i <= r +1; i = i + 1){
for(let j = c-1; j <= c+1; j = j + 1){
if (emap[i][j] === undefined){
return 0;
}
else if (emap[i][j] < current){
count = count + 1;
}
}
}
return count;
}
}
// TASK 3A(I) TESTS
const emapA1 =
[[3, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 2, 3, 1],
[1, 0, 3, 2, 1, 1, 0],
[1, 1, 1, 1, 3, 1, 1],
[1, 2, 1, 1, 3, 1, 3],
[1, 1, 1, 1, 4, 1, 1]];
assert("3A(I)_1", () => count_lower_neighbors([[5]], 0, 0), 0, []);
assert("3A(I)_2", () => count_lower_neighbors(emapA1, 0, 0), 0, []);
assert("3A(I)_3", () => count_lower_neighbors(emapA1, 5, 4), 0, []);
assert("3A(I)_4", () => count_lower_neighbors(emapA1, 4, 6), 0, []);
assert("3A(I)_5", () => count_lower_neighbors(emapA1, 1, 1), 1, []);
assert("3A(I)_6", () => count_lower_neighbors(emapA1, 2, 2), 8, []);
assert("3A(I)_7", () => count_lower_neighbors(emapA1, 2, 3), 5, []);
assert("3A(I)_8", () => count_lower_neighbors(emapA1, 4, 4), 6, []);
//===============================================================
// TASK 3A(II)
//===============================================================
function count_peaks(emap) {
let count = 0;
const rows = array_length(emap);
const columns = array_length(emap[0]);
for(let i = 0; i < rows; i = i + 1){
for(let j = 0; j < columns; j = j + 1){
if (count_lower_neighbors(emap, i, j) === 8){
count = count + 1;
}
}
}
return count;
}
// TASK 3A(II) TESTS
const emapA2a =
[[3, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 2, 3, 1],
[1, 0, 3, 2, 1, 1, 0],
[1, 1, 1, 1, 3, 1, 1],
[1, 2, 1, 1, 3, 1, 3],
[1, 1, 1, 1, 4, 1, 1]]; // 3 peaks
const emapA2b =
[[3, 1, 4, 1, 5, 1, 6, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 7, 1, 8, 1, 9, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[2, 1, 3, 1, 4, 1, 5, 2],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 9, 1, 8, 1, 7, 1, 6],
[1, 1, 1, 1, 1, 1, 1, 1],
[8, 1, 9, 1, 8, 1, 9, 1]]; // 9 peaks
assert("3A(II)_1", () => count_peaks([[5]]),
0, ["count_lower_neighbors"]);
assert("3A(II)_2", () => count_peaks([[2,3,4],[3,5,3],[4,3,2]]),
1, ["count_lower_neighbors"]);
assert("3A(II)_3", () => count_peaks(emapA2a),
3, ["count_lower_neighbors"]);
assert("3A(II)_4", () => count_peaks(emapA2b),
9, ["count_lower_neighbors"]);
//===============================================================
// TASK 3B
//===============================================================
function count_islands(emap) {
// WRITE HERE.
// ---BEGIN TASK---
const R = array_length(emap); // emap size is R x C.
const C = array_length(emap[0]); // emap size is R x C.
const label = []; // 2D array for labelling islands.
let island_count = 0;
// The function island "floods" an entire island with
// the label island_id, starting from the position (row, col).
function label_island(row, col, island_id) {
if ( row >= 0 && row < R && col >= 0 && col < C ) {
if ( emap[row][col] !== 0 && label[row][col] === 0 ) {
label[row][col] = island_id;
label_island(row, col - 1, island_id);
label_island(row, col + 1, island_id);
label_island(row - 1, col, island_id);
label_island(row + 1, col, island_id);
} else {}
} else {}
}
// The labels are initialized to 0.
// The islands are going to be labelled from 1 onwards.
for (let row = 0; row < R; row = row + 1) {
label[row] = [];
for (let col = 0; col < C; col = col + 1) {
label[row][col] = 0;
}
}
for (let row = 0; row < R; row = row + 1) {
for (let col = 0; col < C; col = col + 1) {
if (emap[row][col] !== 0 && label[row][col] === 0) {
island_count = island_count + 1;
label_island(row, col, island_count);
} else {}
}
}
return island_count;
// ---END TASK---
}
// TASK 3B TESTS
const emapB1 =
[[2, 1, 0, 2, 1, 1, 3],
[0, 1, 0, 1, 0, 0, 2],
[0, 0, 0, 2, 3, 1, 1],
[1, 0, 2, 0, 0, 0, 0],
[0, 0, 1, 2, 0, 0, 0],
[1, 0, 3, 0, 1, 1, 2]]; // 6 islands
const emapB2 =
[[1, 2, 0, 0, 1, 0, 0, 1],
[1, 2, 2, 3, 1, 0, 2, 1],
[0, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 3, 3, 0],
[1, 1, 2, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 1, 2, 3],
[1, 3, 2, 1, 1, 0, 1, 1]]; // 5 islands
assert("3B_1", () => count_islands([[0]]), 0, []);
assert("3B_2", () => count_islands([[1]]), 1, []);
assert("3B_3", () => count_islands([[0,0], [0,0]]), 0, []);
assert("3B_4", () => count_islands([[2,1], [1,3]]), 1, []);
assert("3B_5", () => count_islands([[0,1], [0,0]]), 1, []);
assert("3B_6", () => count_islands([[2,0], [0,1]]), 2, []);
assert("3B_7", () => count_islands(emapB1), 6, []);
assert("3B_8", () => count_islands(emapB2), 5, []);
//===============================================================