perfect

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 - A 2D representation of the flower field where '*' represents a flower and ' ' represents an empty space.
9 * @returns {string[]} - The annotated flower field with counts of adjacent flowers in empty spaces.
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12 // Edge Case: Empty input array
13 if (input.length === 0) {
14 return [];
15 }
16
17 const rows = input.length;
18 const cols = input[0].length;
19
20 // Edge Case: All rows have zero length
21 if (cols === 0) {
22 return input;
23 }
24
25 // Create a result grid as a 2D array
26 const result = Array.from({ length: rows }, () => Array(cols).fill(' '));
27
28 // Define the 8 possible directions for adjacent cells
29 const directions = [
30 [-1, -1], [-1, 0], [-1, 1],
31 [0, -1], [0, 1],
32 [1, -1], [1, 0], [1, 1]
33 ];
34
35 // Process each cell in the grid
36 for (let i = 0; i < rows; i++) {
37 for (let j = 0; j < cols; j++) {
38 // If the cell contains a flower, keep it as is
39 if (input[i][j] === '*') {
40 result[i][j] = '*';
41 } else {
42 // Count adjacent flowers
43 let count = 0;
44
45 // Check all 8 directions
46 for (const [dx, dy] of directions) {
47 const ni = i + dx;
48 const nj = j + dy;
49
50 // Edge Case: Check if the adjacent cell is within bounds
51 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
52 if (input[ni][nj] === '*') {
53 count++;
54 }
55 }
56 }
57
58 // If there are adjacent flowers, replace with count
59 // Edge Case: No adjacent flowers (count is 0) - leave as space
60 result[i][j] = count > 0 ? count.toString() : ' ';
61 }
62 }
63 }
64
65 // Convert the result grid back to an array of strings
66 return result.map(row => row.join(''));
1267};
68
69// Handled Edge Cases: Empty input array, all rows have zero length, 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.