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 const cols = input[0].length;
20 const result = [];
21
22 // Edge Case: Handle rows of different lengths by treating them as having trailing spaces
23 // This implementation assumes all rows have the same length as the first row.
24 // If variable lengths are expected, we'd need to pad shorter rows.
25
26 for (let i = 0; i < rows; i++) {
27 let newRow = '';
28 for (let j = 0; j < cols; j++) {
29 if (input[i][j] === '*') {
30 newRow += '*';
31 } else {
32 let count = 0;
33 // Check all 8 adjacent cells
34 for (let di = -1; di <= 1; di++) {
35 for (let dj = -1; dj <= 1; dj++) {
36 // Skip the cell itself
37 if (di === 0 && dj === 0) continue;
38
39 const ni = i + di;
40 const nj = j + dj;
41
42 // Edge Case: Check bounds to avoid accessing invalid indices
43 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
44 if (input[ni][nj] === '*') {
45 count++;
46 }
47 }
48 }
49 }
50
51 // Edge Case: If no adjacent flowers, leave the cell empty
52 if (count === 0) {
53 newRow += ' ';
54 } else {
55 newRow += count.toString();
56 }
57 }
58 }
59 result.push(newRow);
60 }
61
62 // Handled Edge Cases: Empty input, out-of-bounds access, cells with no adjacent flowers
63 return result;
1264};
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.