sn62

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 - Array of strings representing the flower field where '*' is a flower and ' ' is empty
9 * @returns {string[]} - Array of strings with empty spaces replaced by flower counts or left empty if no adjacent flowers
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12 // Edge Case: Handle empty input array
13 if (!input || input.length === 0) {
14 return [];
15 }
16
17 // Edge Case: Handle input with empty strings
18 const rows = input.length;
19 const cols = input[0] ? input[0].length : 0;
20
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] = input[i].split('');
29 }
30
31 // Define the 8 possible directions (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 in the grid
39 for (let row = 0; row < rows; row++) {
40 for (let col = 0; col < cols; col++) {
41 // Edge Case: Only process empty cells
42 if (input[row][col] === ' ') {
43 let count = 0;
44
45 // Check all 8 adjacent cells
46 for (const [dx, dy] of directions) {
47 const newRow = row + dx;
48 const newCol = col + dy;
49
50 // Edge Case: Check bounds to avoid accessing invalid indices
51 if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
52 if (input[newRow][newCol] === '*') {
53 count++;
54 }
55 }
56 }
57
58 // Edge Case: Only replace with count if there are adjacent flowers
59 if (count > 0) {
60 result[row][col] = count.toString();
61 }
62 }
63 }
64 }
65
66 // Convert each row back to a string
67 return result.map(row => row.join(''));
1268};
69
70// Handled Edge Cases: Empty input array, input with empty strings, out-of-bounds access, non-empty cells, empty cells with 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.