relax

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 *
9 * @param {string[]} input - An array of strings representing the flower field, where
10 * '*' represents a flower and ' ' represents an empty space.
11 * @returns {string[]} - An array of strings representing the annotated flower field, where
12 * empty spaces are replaced with the count of adjacent flowers or
13 * remain empty if there are no adjacent flowers.
14 *
15 * Edge Cases Handled:
16 * - Empty input array (returns empty array)
17 * - Single row or column
18 * - All flowers or all empty spaces
19 * - Adjacent counting at board edges and corners
920 */
1021export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
22 // Edge Case: Empty input array
23 if (input.length === 0) {
24 return [];
25 }
26
27 const rows = input.length;
28 const cols = input[0].length;
29
30 // Edge Case: Empty rows
31 if (cols === 0) {
32 return input;
33 }
34
35 // Convert input strings to arrays for easier manipulation
36 const board = input.map(row => row.split(''));
37 const result = Array(rows).fill().map(() => Array(cols).fill(''));
38
39 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
40 const directions = [
41 [-1, -1], [-1, 0], [-1, 1],
42 [0, -1], [0, 1],
43 [1, -1], [1, 0], [1, 1]
44 ];
45
46 for (let i = 0; i < rows; i++) {
47 for (let j = 0; j < cols; j++) {
48 // If current cell is a flower, keep it as is
49 if (board[i][j] === '*') {
50 result[i][j] = '*';
51 } else {
52 // Count adjacent flowers
53 let count = 0;
54
55 for (const [dx, dy] of directions) {
56 const ni = i + dx;
57 const nj = j + dy;
58
59 // Check if the adjacent cell is within bounds
60 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
61 if (board[ni][nj] === '*') {
62 count++;
63 }
64 }
65 }
66
67 // Edge Case: No adjacent flowers - leave empty (represented as dot for display)
68 if (count === 0) {
69 result[i][j] = ' ';
70 } else {
71 result[i][j] = count.toString();
72 }
73 }
74 }
75 }
76
77 // Convert result arrays back to strings
78 return result.map(row => row.join(''));
1279};
80
81// Handled Edge Cases: Empty input array, Empty rows, All flowers or all empty spaces, Adjacent counting at board edges and corners
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.