661. Image Smoother Flashcards

1
Q

can you set up variables for img.length, img[0].length?

A

let m = img.length, n = img[0].length;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

can you set up variable res for empty two dimensional array?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

can you set up variable dir for adjacent cells of current cell in 8 directions?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

can you set up a function smooth that takes coordinates (i, j)?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

can you set up variable sum with initial value current img cell, variable count with initial value 1 in smooth function?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

can you loop through directions in dir in smooth function?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {

    }
   
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

can you check in the dir loop, if adjacent cell is undefined, if not, update sum with the adjacent cell value, increment count

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
if (img[i + r] !== undefined && img[i + r][j + c] !== undefined) {
sum += img[i + r][j + c];
count++;
}
}

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

can you return average of the sum in the smooth function?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
if (img[i + r] !== undefined && img[i + r][j + c] !== undefined) {
sum += img[i + r][j + c];
count++;
}
}
return sum / count | 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

can you loop through each cell as input in smooth function, assign smooth function return value to empty two dimensional array?

A

let m = img.length, n = img[0].length;
let res = new Array(m).fill(0).map(() => new Array(n).fill(0));
let dir = [[0, 1], [1, 0], [0, -1], [-1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]];
const smooth = (i, j) => {
let sum = img[i][j], count = 1;
for (let [r, c] of dir) {
if (img[i + r] !== undefined && img[i + r][j + c] !== undefined) {
sum += img[i + r][j + c];
count++;
}
}
return sum / count | 0;
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
res[i][j] = smooth(i, j);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

can you return the two dimensional array?

A

return res;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly