let it go

Finished
44//
55
66/**
7 * @param {string[]} input
8 * @returns {string[]}
7 * Annotates a flower field grid by replacing empty spaces with the count of adjacent flowers.
8 * @param {string[]} input - A grid represented as an array of strings, where each string is a row.
9 * '*' represents a flower, ' ' represents an empty space.
10 * @returns {string[]} - The annotated grid with counts of adjacent flowers in empty spaces.
911 */
1012export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
13 // Edge Case: Handle null or undefined input
14 if (input === null || input === undefined) {
15 return [];
16 }
17
18 // Edge Case: Handle empty input array
19 if (input.length === 0) {
20 return [];
21 }
22
23 const rows = input.length;
24 const cols = input[0].length;
25 const result = [];
26
27 // Edge Case: Handle rows with inconsistent lengths
28 for (let i = 0; i < rows; i++) {
29 if (input[i].length !== cols) {
30 throw new Error('All rows must have the same length');
31 }
32 }
33
34 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
35 const directions = [
36 [-1, -1], [-1, 0], [-1, 1],
37 [0, -1], [0, 1],
38 [1, -1], [1, 0], [1, 1]
39 ];
40
41 for (let i = 0; i < rows; i++) {
42 let newRow = '';
43 for (let j = 0; j < cols; j++) {
44 if (input[i][j] === '*') {
45 newRow += '*';
46 } else {
47 let count = 0;
48 for (const [dx, dy] of directions) {
49 const ni = i + dx;
50 const nj = j + dy;
51 // Edge Case: Check bounds to avoid accessing invalid indices
52 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
53 if (input[ni][nj] === '*') {
54 count++;
55 }
56 }
57 }
58 // Edge Case: If no adjacent flowers, leave the cell empty
59 newRow += count === 0 ? ' ' : count.toString();
60 }
61 }
62 result.push(newRow);
63 }
64
65 // Handled Edge Cases: null/undefined input, empty input array, inconsistent row lengths, out-of-bounds access, no adjacent flowers
66 return result;
1267};
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.