Merry

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.length === 0) {
14 return [];
15 }
16
17 // Edge Case: Handle input with empty strings
18 const rows = input.length;
19 const cols = input[0].length;
20
21 // Edge Case: Handle rows with different lengths
22 // In this implementation, we assume all rows have the same length as the first row
23
24 // Create a result array with the same dimensions as input
25 const result = Array(rows).fill().map(() => Array(cols).fill(''));
26
27 // Define the 8 possible directions for adjacent cells
28 const directions = [
29 [-1, -1], [-1, 0], [-1, 1],
30 [0, -1], [0, 1],
31 [1, -1], [1, 0], [1, 1]
32 ];
33
34 // Process each cell in the grid
35 for (let i = 0; i < rows; i++) {
36 for (let j = 0; j < cols; j++) {
37 // If the cell contains a flower, keep it as is
38 if (input[i][j] === '*') {
39 result[i][j] = '*';
40 } else {
41 // Count adjacent flowers
42 let flowerCount = 0;
43
44 // Check all 8 adjacent positions
45 for (const [dx, dy] of directions) {
46 const newRow = i + dx;
47 const newCol = j + dy;
48
49 // Edge Case: Check if the adjacent position is within bounds
50 if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
51 if (input[newRow][newCol] === '*') {
52 flowerCount++;
53 }
54 }
55 }
56
57 // If no adjacent flowers, leave the cell empty
58 // Otherwise, set it to the count of adjacent flowers
59 result[i][j] = flowerCount === 0 ? ' ' : flowerCount.toString();
60 }
61 }
62 }
63
64 // Convert each row array back to a string
65 return result.map(row => row.join(''));
1266};
67
68// Handled Edge Cases: Empty input array, input with empty strings, rows with different lengths (assumed consistent), adjacent positions out of bounds
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.