Skip to content

Commit 012fe9d

Browse files
committed
BoxBlur
1 parent 4c002c7 commit 012fe9d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

CodeSignal/js/thecore/BoxBlur.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function solution(image) {
2+
const blurred = [];
3+
4+
for (let row = 1; row < image.length - 1; row++) {
5+
blurred.push([]);
6+
for (let col = 1; col < image[row].length - 1; col++) {
7+
let sum = 0;
8+
for (let r = -1; r <= 1; r++) {
9+
for (let c = -1; c <= 1; c++) {
10+
sum += image[row + r][col + c];
11+
}
12+
}
13+
blurred[row - 1].push(Math.floor(sum / 9));
14+
}
15+
}
16+
17+
return blurred;
18+
}

CodeSignal/js/thecore/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
# BoxBlur
2+
___
3+
Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.
4+
5+
The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel `x` in the output image has a value equal to the average value of the pixel values from the `3 × 3` square that has its center at `x`, including `x` itself. All the pixels on the border of `x` are then removed.
6+
7+
Return the blurred image as an integer, with the fractions rounded down.
8+
9+
[My solution - Click me and check the whole solution + comments ](https://github.com/PiotrSierant/HTML-CSS-JS/blob/main/CodeSignal/js/thecore/BoxBlur.js)
10+
```javascript
11+
function solution(image) {
12+
const blurred = [];
13+
14+
for (let row = 1; row < image.length - 1; row++) {
15+
blurred.push([]);
16+
for (let col = 1; col < image[row].length - 1; col++) {
17+
let sum = 0;
18+
for (let r = -1; r <= 1; r++) {
19+
for (let c = -1; c <= 1; c++) {
20+
sum += image[row + r][col + c];
21+
}
22+
}
23+
blurred[row - 1].push(Math.floor(sum / 9));
24+
}
25+
}
26+
27+
return blurred;
28+
}
29+
30+
```
31+
132
# Minesweeper
233
___
334
In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

0 commit comments

Comments
 (0)