let it go

Finished
44//
55
66/**
7 * @param {string[]} input
8 * @returns {string[]}
7 * Annotates a flower field by replacing empty squares with the count of adjacent flowers.
8 * @param {string[]} input - A 2D array represented as a list of strings, where each string is a row.
9 * '*' represents a flower, ' ' represents an empty square.
10 * @returns {string[]} - The annotated flower field with counts replacing empty squares.
911 */
1012export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
13 // Edge Case: Handle empty input
14 if (!input || input.length === 0) {
15 return [];
16 }
17
18 const rows = input.length;
19 const cols = input[0].length;
20
21 // Edge Case: Handle rows of different lengths by treating them as if they have trailing spaces
22 const normalizedInput = input.map(row => row.padEnd(cols, ' '));
23
24 // Create result array
25 const result = [];
26
27 // 8 directions: horizontal, vertical, diagonal
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 for (let i = 0; i < rows; i++) {
35 let newRow = '';
36 for (let j = 0; j < cols; j++) {
37 // If current cell is a flower, keep it as is
38 if (normalizedInput[i][j] === '*') {
39 newRow += '*';
40 } else {
41 // Count adjacent flowers
42 let count = 0;
43 for (const [dx, dy] of directions) {
44 const ni = i + dx;
45 const nj = j + dy;
46
47 // Check if neighbor is within bounds
48 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
49 if (normalizedInput[ni][nj] === '*') {
50 count++;
51 }
52 }
53 }
54
55 // Edge Case: If no adjacent flowers, leave the cell empty
56 if (count === 0) {
57 newRow += ' ';
58 } else {
59 newRow += count.toString();
60 }
61 }
62 }
63 result.push(newRow);
64 }
65
66 return result;
1267};
68
69// Handled Edge Cases: Empty input, rows of different lengths, cells with 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.