Konoha

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 - An array of strings representing the flower field, where '*' is a flower and ' ' is an empty space
9 * @returns {string[]} - An array of strings representing the annotated flower field
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12 // Edge Case: Handle empty input
13 if (!input || input.length === 0) {
14 return [];
15 }
16
17 const rows = input.length;
18 const cols = input[0].length;
19
20 // Edge Case: Handle input with empty strings
21 if (cols === 0) {
22 return input;
23 }
24
25 // Convert input strings to arrays for easier manipulation
26 const field = input.map(row => row.split(''));
27 const result = Array(rows).fill().map(() => Array(cols).fill(''));
28
29 // Define the 8 possible directions (horizontal, vertical, diagonal)
30 const directions = [
31 [-1, -1], [-1, 0], [-1, 1],
32 [0, -1], [0, 1],
33 [1, -1], [1, 0], [1, 1]
34 ];
35
36 // Process each cell in the field
37 for (let i = 0; i < rows; i++) {
38 for (let j = 0; j < cols; j++) {
39 // If the cell contains a flower, keep it as is
40 if (field[i][j] === '*') {
41 result[i][j] = '*';
42 } else {
43 // Count adjacent flowers
44 let count = 0;
45
46 // Check all 8 directions
47 for (const [dx, dy] of directions) {
48 const ni = i + dx;
49 const nj = j + dy;
50
51 // Edge Case: Check if the adjacent cell is within bounds
52 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
53 if (field[ni][nj] === '*') {
54 count++;
55 }
56 }
57 }
58
59 // Edge Case: If no adjacent flowers, leave the cell empty
60 // Otherwise, place the count
61 result[i][j] = count === 0 ? ' ' : count.toString();
62 }
63 }
64 }
65
66 // Convert arrays back to strings
67 return result.map(row => row.join(''));
1268};
69
70// Handled Edge Cases: Empty input, input with empty strings, out of bounds adjacency checks, 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.