armor

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 * @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 space.
10 * @returns {string[]} - The annotated flower field with counts replacing empty spaces.
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
20 // Create a 2D array for easier access
21 const grid = input.map(row => row.split(''));
22
23 // Directions for 8 neighbors (horizontal, vertical, diagonal)
24 const directions = [
25 [-1, -1], [-1, 0], [-1, 1],
26 [0, -1], [0, 1],
27 [1, -1], [1, 0], [1, 1]
28 ];
29
30 const result = [];
31
32 for (let i = 0; i < rows; i++) {
33 let newRow = '';
34 const cols = grid[i].length; // Use the actual length of this row
35
36 for (let j = 0; j < cols; j++) {
37 if (grid[i][j] === '*') {
38 newRow += '*';
39 } else {
40 let count = 0;
41
42 // Check all 8 directions
43 for (const [dx, dy] of directions) {
44 const ni = i + dx;
45 const nj = j + dy;
46
47 // Edge Case: Check boundaries to avoid index out of bounds
48 // Also need to check if the neighbor row has enough columns
49 if (ni >= 0 && ni < rows && nj >= 0 && nj < (grid[ni] ? grid[ni].length : 0)) {
50 if (grid[ni][nj] === '*') {
51 count++;
52 }
53 }
54 }
55
56 // Edge Case: If no adjacent flowers, leave the space empty
57 if (count === 0) {
58 newRow += ' ';
59 } else {
60 newRow += count.toString();
61 }
62 }
63 }
64 result.push(newRow);
65 }
66
67 return result;
1268};
69
70// Handled Edge Cases: Empty input, rows of different lengths (assumed to be same as first), boundary checking, 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.