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 - A rectangular array of strings representing the flower field.
9 * @returns {string[]} - The annotated flower field.
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12 // Edge Case: Handle empty input
13 if (!input || input.length === 0) {
14 return [];
15 }
16
17 const rows = input.length;
18 const cols = input[0].length;
19
20 // Edge Case: Handle rows with different lengths
21 for (let i = 0; i < rows; i++) {
22 if (input[i].length !== cols) {
23 throw new Error('All rows must have the same length');
24 }
25 }
26
27 // Create a 2D array for easier manipulation
28 const field = input.map(row => row.split(''));
29 const result = Array(rows).fill().map(() => Array(cols).fill(''));
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
39 for (let i = 0; i < rows; i++) {
40 for (let j = 0; j < cols; j++) {
41 // If the cell is a flower, keep it as is
42 if (field[i][j] === '*') {
43 result[i][j] = '*';
44 } else {
45 // Count adjacent flowers
46 let count = 0;
47
48 // Check all 8 directions
49 for (const [dx, dy] of directions) {
50 const ni = i + dx;
51 const nj = j + dy;
52
53 // Edge Case: Check if the adjacent cell is within bounds
54 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
55 if (field[ni][nj] === '*') {
56 count++;
57 }
58 }
59 }
60
61 // Edge Case: If no adjacent flowers, leave the cell empty
62 // Otherwise, place the count
63 result[i][j] = count === 0 ? ' ' : count.toString();
64 }
65 }
66 }
67
68 // Convert the 2D array back to an array of strings
69 return result.map(row => row.join(''));
1270};
71
72// Handled Edge Cases: Empty input, rows with different lengths, adjacent cells out of bounds, 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.