kale

Finished
88 * @returns {string[]}
99 */
1010export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
11 // Edge Case: Empty grid (no rows)
12 if (!input || input.length === 0) {
13 return [];
14 }
15
16 // Edge Case: Grid with empty strings
17 const rows = input.length;
18 const cols = input[0] ? input[0].length : 0;
19
20 // Edge Case: Single row or single column grid with no columns
21 if (cols === 0) {
22 return input;
23 }
24
25 // Create a result grid as a 2D array
26 const result = Array(rows);
27 for (let i = 0; i < rows; i++) {
28 result[i] = Array(cols);
29 }
30
31 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
32 const directions = [
33 [-1, -1], [-1, 0], [-1, 1],
34 [0, -1], [0, 1],
35 [1, -1], [1, 0], [1, 1]
36 ];
37
38 // Process each cell
39 for (let i = 0; i < rows; i++) {
40 for (let j = 0; j < cols; j++) {
41 // Edge Case: Cell is a flower
42 if (input[i][j] === '*') {
43 result[i][j] = '*';
44 }
45 // Edge Case: Cell is empty
46 else {
47 let count = 0;
48 // Check all 8 adjacent cells
49 for (const [dx, dy] of directions) {
50 const ni = i + dx;
51 const nj = j + dy;
52
53 // Edge Case: Adjacent cell is within bounds
54 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
55 if (input[ni][nj] === '*') {
56 count++;
57 }
58 }
59 }
60
61 // Edge Case: No adjacent flowers
62 if (count === 0) {
63 result[i][j] = ' ';
64 } else {
65 result[i][j] = count.toString();
66 }
67 }
68 }
69 }
70
71 // Convert result array back to array of strings
72 return result.map(row => row.join(''));
73
74 // Handled Edge Cases: Empty grid, grid with empty strings, single row/column grids, cells with flowers, empty cells with no adjacent flowers, empty cells with adjacent flowers
1275};
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.