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