future

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
20 // Edge Case: Handle zero-width board
21 if (rows === 0 || input[0].length === 0) {
22 return input;
23 }
24
25 // Create a 2D array for the result
26 const result = Array(rows);
27
28 // Define the 8 possible directions (horizontal, vertical, diagonal)
29 const directions = [
30 [-1, -1], [-1, 0], [-1, 1],
31 [0, -1], [0, 1],
32 [1, -1], [1, 0], [1, 1]
33 ];
34
35 // Process each cell in the grid
36 for (let i = 0; i < rows; i++) {
37 const rowLength = input[i].length;
38 result[i] = [];
39 for (let j = 0; j < rowLength; j++) {
40 // If the cell is a flower, keep it as is
41 if (input[i][j] === '*') {
42 result[i][j] = '*';
43 } else {
44 // Count adjacent flowers
45 let count = 0;
46
47 // Check all 8 directions
48 for (const [dx, dy] of directions) {
49 const ni = i + dx;
50 const nj = j + dy;
51
52 // Edge Case: Check if the neighbor is within bounds
53 if (ni >= 0 && ni < rows && nj >= 0 && nj < (input[ni] ? input[ni].length : 0)) {
54 if (input[ni][nj] === '*') {
55 count++;
56 }
57 }
58 }
59
60 // Edge Case: If no adjacent flowers, leave the cell as space
61 // Otherwise, place the count
62 result[i][j] = count === 0 ? ' ' : count.toString();
63 }
64 }
65 // Join the row array into a string
66 result[i] = result[i].join('');
67 }
68
69 return result;
1270};
71
72// Handled Edge Cases: Empty input, rows with inconsistent lengths, zero-width board, neighbors out of bounds, 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.