fortitude

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 - Array of strings representing the flower field where '*' is a flower and ' ' is empty
9 * @returns {string[]} - 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 // Edge Case: Handle input with empty strings
18 const rows = input.length;
19 const cols = input[0].length;
20
21 // Edge Case: Handle rows with different 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 // Create a result array with the same dimensions as input
29 const result = Array(rows);
30
31 // Define the 8 possible directions (horizontal, vertical, diagonal)
32 const directions = [
33 [-1, -1], [-1, 0], [-1, 1],
34 [0, -1], [0, 1],
35 [1, -1], [1, 0], [1, 1]
36 ];
37
38 // Process each cell in the grid
39 for (let i = 0; i < rows; i++) {
40 let newRow = '';
41 for (let j = 0; j < cols; j++) {
42 // If the cell is a flower, keep it as is
43 if (input[i][j] === '*') {
44 newRow += '*';
45 } else {
46 // Count adjacent flowers
47 let count = 0;
48
49 // Check all 8 directions
50 for (const [dx, dy] of directions) {
51 const ni = i + dx;
52 const nj = j + dy;
53
54 // Edge Case: Check if the adjacent cell is within bounds
55 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
56 if (input[ni][nj] === '*') {
57 count++;
58 }
59 }
60 }
61
62 // Edge Case: If no adjacent flowers, leave the cell empty
63 if (count === 0) {
64 newRow += ' ';
65 } else {
66 // Otherwise, replace with the count
67 newRow += count.toString();
68 }
69 }
70 }
71 result[i] = newRow;
72 }
73
74 return result;
75
76 // Handled Edge Cases: Empty input array, input with empty strings, rows with different lengths, adjacent cell bounds checking, no adjacent flowers
77};
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.