who-are-you

Finished
88 * @returns {string[]}
99 */
1010export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
11 // Edge Case: Handle empty input array
12 if (!input || input.length === 0) {
13 return [];
14 }
15
16 const rows = input.length;
17
18 // Note: Rows can have different lengths, so we handle each row individually
19
20 // Create a result grid as a 2D array
21 const result = Array(rows);
22 for (let i = 0; i < rows; i++) {
23 result[i] = input[i].split('');
24 }
25
26 // Define the 8 possible directions (horizontal, vertical, diagonal)
27 const directions = [
28 [-1, -1], [-1, 0], [-1, 1],
29 [0, -1], [0, 1],
30 [1, -1], [1, 0], [1, 1]
31 ];
32
33 // Iterate through each cell
34 for (let i = 0; i < rows; i++) {
35 const cols = input[i].length;
36 for (let j = 0; j < cols; j++) {
37 // If the cell is empty, count adjacent flowers
38 if (input[i][j] === ' ') {
39 let count = 0;
40
41 // Check all 8 directions
42 for (const [dx, dy] of directions) {
43 const ni = i + dx;
44 const nj = j + dy;
45
46 // Edge Case: Check bounds to avoid accessing invalid indices
47 if (ni >= 0 && ni < rows && nj >= 0 && nj < input[ni].length) {
48 if (input[ni][nj] === '*') {
49 count++;
50 }
51 }
52 }
53
54 // Edge Case: Only replace empty cell with count if count > 0
55 if (count > 0) {
56 result[i][j] = count.toString();
57 }
58 }
59 }
60 }
61
62 // Convert each row back to a string
63 return result.map(row => row.join(''));
1264};
65
66// Handled Edge Cases: Empty input array, rows with different lengths, accessing invalid indices, empty cells with zero 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.