tobe

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 // Create a result grid as a 2D array
21 const result = Array(rows).fill().map(() => Array(cols).fill(''));
22
23 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
24 const directions = [
25 [-1, -1], [-1, 0], [-1, 1],
26 [0, -1], [0, 1],
27 [1, -1], [1, 0], [1, 1]
28 ];
29
30 // Process each cell
31 for (let i = 0; i < rows; i++) {
32 for (let j = 0; j < cols; j++) {
33 // If current cell is a flower, keep it as is
34 if (input[i][j] === '*') {
35 result[i][j] = '*';
36 } else {
37 // Count adjacent flowers
38 let count = 0;
39
40 // Check all 8 directions
41 for (const [dx, dy] of directions) {
42 const ni = i + dx;
43 const nj = j + dy;
44
45 // Edge Case: Check if adjacent cell is within bounds
46 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
47 if (input[ni][nj] === '*') {
48 count++;
49 }
50 }
51 }
52
53 // Edge Case: If no adjacent flowers, leave cell empty
54 // Otherwise, set cell to the count
55 result[i][j] = count === 0 ? ' ' : count.toString();
56 }
57 }
58 }
59
60 // Convert result grid back to array of strings
61 return result.map(row => row.join(''));
1262};
63
64// Handled Edge Cases: Empty input array, input with empty strings, 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.