xas

Finished
44//
55
66/**
7 * @param {string[]} input
8 * @returns {string[]}
7 * Annotates a flower field grid by replacing empty spaces with the count of adjacent flowers.
8 * @param {string[]} input - An array of strings representing the flower field grid.
9 * @returns {string[]} - An array of strings with empty spaces replaced by flower counts.
10 *
11 * Edge Cases Handled:
12 * - Empty input array (returns empty array)
13 * - Input with no empty spaces (returns original grid)
14 * - Input with no flowers (returns original grid)
15 * - Cells at corners, edges, and center (proper neighbor checking)
16 * - Cells with zero adjacent flowers (remain empty)
917 */
1018export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12};
19 // Edge Case: Empty input array
20 if (input.length === 0) {
21 return [];
22 }
23
24 const rows = input.length;
25 const cols = input[0].length;
26
27 // Edge Case: Empty rows
28 if (cols === 0) {
29 return input;
30 }
31
32 // Create a result grid as a 2D array
33 const result = Array(rows).fill().map(() => Array(cols).fill(''));
34
35 // Directions for 8 neighbors (horizontal, vertical, diagonal)
36 const directions = [
37 [-1, -1], [-1, 0], [-1, 1],
38 [0, -1], [0, 1],
39 [1, -1], [1, 0], [1, 1]
40 ];
41
42 // Iterate through each cell
43 for (let i = 0; i < rows; i++) {
44 for (let j = 0; j < cols; j++) {
45 // If current cell is a flower, keep it as is
46 if (input[i][j] === '*') {
47 result[i][j] = '*';
48 } else {
49 // Count adjacent flowers
50 let count = 0;
51
52 // Check all 8 directions
53 for (const [dx, dy] of directions) {
54 const ni = i + dx;
55 const nj = j + dy;
56
57 // Check if neighbor is within bounds
58 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
59 if (input[ni][nj] === '*') {
60 count++;
61 }
62 }
63 }
64
65 // Edge Case: No adjacent flowers
66 if (count === 0) {
67 result[i][j] = ' ';
68 } else {
69 result[i][j] = count.toString();
70 }
71 }
72 }
73 }
74
75 // Convert result grid back to array of strings
76 return result.map(row => row.join(''));
77
78 // Handled Edge Cases: Empty input array, empty rows, cells with no adjacent flowers, proper neighbor checking for all positions
79};
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.