relax

Finished
44//
55
66/**
7 * @param {string[]} input
8 * @returns {string[]}
7 * Annotates a flower field by replacing empty spaces with the count of adjacent flowers.
8 * @param {string[]} input - An array of strings representing the flower field
9 * @returns {string[]} - An array of strings with annotations added
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12};
12 // Edge Case: Handle empty input
13 if (!input || input.length === 0) {
14 return [];
15 }
16
17 const rows = input.length;
18 const cols = input[0].length;
19 const result = [];
20
21 // Edge Case: Handle empty rows
22 if (cols === 0) {
23 return input;
24 }
25
26 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
27 const directions = [
28 [-1, -1], [-1, 0], [-1, 1],
29 [0, -1], [0, 1],
30 [1, -1], [1, 0], [1, 1]
31 ];
32
33 for (let i = 0; i < rows; i++) {
34 let newRow = '';
35 for (let j = 0; j < cols; j++) {
36 // If current cell is a flower, keep it as is
37 if (input[i][j] === '*') {
38 newRow += '*';
39 } else {
40 // Count adjacent flowers
41 let count = 0;
42 for (const [dx, dy] of directions) {
43 const ni = i + dx;
44 const nj = j + dy;
45
46 // Edge Case: Check bounds to avoid accessing invalid indices
47 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
48 if (input[ni][nj] === '*') {
49 count++;
50 }
51 }
52 }
53
54 // Edge Case: If no adjacent flowers, leave the cell empty
55 newRow += count === 0 ? ' ' : count.toString();
56 }
57 }
58 result.push(newRow);
59 }
60
61 // Handled Edge Cases: empty input, empty rows, out-of-bounds access, no adjacent flowers
62 return result;
63};
Test NameStatus
handles no rows
Pass
handles no columns
Pass
handles no flowers
Pass
handles garden full of flowers
Pass
handles flower surrounded by spaces
Pass
handles space surrounded by flowers
Pass
handles horizontal line
Pass
handles horizontal line, flowers at edges
Pass
handles vertical line
Pass
handles vertical line, flowers at edges
Pass
handles cross
Pass
handles large garden
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.