yahoo

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 - An array of strings representing the flower field, where '*' represents a flower and ' ' represents an empty space.
9 * @returns {string[]} - An array of strings representing 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 // Convert input strings to arrays for easier manipulation
28 const field = input.map(row => row.split(''));
29
30 // Define the 8 possible directions (horizontal, vertical, diagonal)
31 const directions = [
32 [-1, -1], [-1, 0], [-1, 1],
33 [0, -1], [0, 1],
34 [1, -1], [1, 0], [1, 1]
35 ];
36
37 // Process each cell in the field
38 for (let i = 0; i < rows; i++) {
39 for (let j = 0; j < cols; j++) {
40 // Edge Case: Handle non-space, non-flower characters
41 if (field[i][j] !== ' ' && field[i][j] !== '*') {
42 throw new Error('Invalid character in input');
43 }
44
45 // Only process empty spaces
46 if (field[i][j] === ' ') {
47 let count = 0;
48
49 // Check all 8 adjacent cells
50 for (const [dx, dy] of directions) {
51 const ni = i + dx;
52 const nj = j + dy;
53
54 // Check if the adjacent cell is within bounds
55 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
56 // If the adjacent cell is a flower, increment count
57 if (field[ni][nj] === '*') {
58 count++;
59 }
60 }
61 }
62
63 // If there are adjacent flowers, replace the space with the count
64 if (count > 0) {
65 field[i][j] = count.toString();
66 }
67 }
68 }
69 }
70
71 // Convert arrays back to strings
72 return field.map(row => row.join(''));
1273};
74
75// Handled Edge Cases: Empty input, rows with different lengths, non-space/non-flower characters
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.