Merry

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 with empty spaces replaced by flower counts or left empty if no adjacent flowers
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12};
12 // Edge Case: Handle empty input array
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 rows with different lengths
21 for (let i = 0; i < rows; i++) {
22 if (input[i].length !== cols) {
23 throw new Error('All rows must have the same length');
24 }
25 }
26
27 // Edge Case: Handle zero-width input
28 if (cols === 0) {
29 return input;
30 }
31
32 // Create result array
33 const result = [];
34
35 // Directions for 8 adjacent cells (horizontal, vertical, diagonal)
36 const directions = [
37 [-1, -1], [-1, 0], [-1, 1],
38 [0, -1], [0, 1],
39 [1, -1], [1, 0], [1, 1]
40 ];
41
42 // Process each cell
43 for (let i = 0; i < rows; i++) {
44 let newRow = '';
45 for (let j = 0; j < cols; j++) {
46 // If current cell is a flower, keep it as is
47 if (input[i][j] === '*') {
48 newRow += '*';
49 } else {
50 // Count adjacent flowers
51 let count = 0;
52
53 // Check all 8 directions
54 for (const [dx, dy] of directions) {
55 const ni = i + dx;
56 const nj = j + dy;
57
58 // Check if the adjacent cell is within bounds
59 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
60 // If adjacent cell is a flower, increment count
61 if (input[ni][nj] === '*') {
62 count++;
63 }
64 }
65 }
66
67 // If no adjacent flowers, leave empty, otherwise add count
68 if (count === 0) {
69 newRow += ' ';
70 } else {
71 newRow += count.toString();
72 }
73 }
74 }
75 result.push(newRow);
76 }
77
78 return result;
79 // Handled Edge Cases: Empty input array, rows with different lengths, zero-width input
80};
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.