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 - 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');
12};
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
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 // Convert input strings to arrays for easier manipulation
29 const board = input.map(row => row.split(''));
30 // Create a copy of the board for reading to avoid counting already processed cells
31 const originalBoard = input.map(row => row.split(''));
32
33 // Define the 8 possible directions (horizontal, vertical, diagonal)
34 const directions = [
35 [-1, -1], [-1, 0], [-1, 1],
36 [0, -1], [0, 1],
37 [1, -1], [1, 0], [1, 1]
38 ];
39
40 // Process each cell in the board
41 for (let i = 0; i < rows; i++) {
42 for (let j = 0; j < cols; j++) {
43 // Edge Case: Only process empty cells
44 if (board[i][j] === ' ') {
45 let count = 0;
46
47 // Check all 8 adjacent cells
48 for (const [dx, dy] of directions) {
49 const ni = i + dx;
50 const nj = j + dy;
51
52 // Edge Case: Check if adjacent cell is within bounds
53 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
54 // Edge Case: Count adjacent flowers from the original board
55 if (originalBoard[ni][nj] === '*') {
56 count++;
57 }
58 }
59 }
60
61 // Edge Case: Replace empty cell with count if there are adjacent flowers
62 if (count > 0) {
63 board[i][j] = count.toString();
64 }
65 }
66 }
67 }
68
69 // Convert arrays back to strings
70 return board.map(row => row.join(''));
71
72 // Handled Edge Cases: Empty input, rows with different lengths, out-of-bounds access, non-empty cells
73};
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.