fishlium

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 - An array of strings representing the flower field grid.
9 * @returns {string[]} - An array of strings with empty spaces replaced by flower counts.
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12 // Edge Case: Handle empty input array
13 if (input.length === 0) {
14 return [];
15 }
16
17 const rows = input.length;
18 const cols = input[0].length;
19 const result = [];
20
21 // Edge Case: Handle rows with inconsistent lengths
22 for (let i = 0; i < rows; i++) {
23 if (input[i].length !== cols) {
24 throw new Error('All rows must have the same length');
25 }
26 }
27
28 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
29 const directions = [
30 [-1, -1], [-1, 0], [-1, 1],
31 [0, -1], [0, 1],
32 [1, -1], [1, 0], [1, 1]
33 ];
34
35 for (let i = 0; i < rows; i++) {
36 let newRow = '';
37 for (let j = 0; j < cols; j++) {
38 // If current cell is a flower, keep it as is
39 if (input[i][j] === '*') {
40 newRow += '*';
41 } else {
42 // Count adjacent flowers
43 let count = 0;
44 for (const [dx, dy] of directions) {
45 const ni = i + dx;
46 const nj = j + dy;
47
48 // Edge Case: Check boundaries to avoid index out of bounds
49 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
50 if (input[ni][nj] === '*') {
51 count++;
52 }
53 }
54 }
55
56 // Edge Case: If no adjacent flowers, leave cell empty
57 newRow += count === 0 ? ' ' : count.toString();
58 }
59 }
60 result.push(newRow);
61 }
62
63 // Handled Edge Cases: Empty input array, inconsistent row lengths, boundary checking, empty cells with no adjacent flowers
64 return result;
1265};
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.