lexicon

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 const cols = input[0].length;
20
21 // Edge Case: Handle rows with inconsistent 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 // Edge Case: Handle zero-width board
29 if (cols === 0) {
30 return input;
31 }
32
33 // Create a result grid as an array of arrays for easier manipulation
34 const result = Array.from({ length: rows }, () => Array(cols).fill(' '));
35
36 // Define the 8 possible directions for adjacent cells
37 const directions = [
38 [-1, -1], [-1, 0], [-1, 1],
39 [0, -1], [0, 1],
40 [1, -1], [1, 0], [1, 1]
41 ];
42
43 // Process each cell in the input
44 for (let i = 0; i < rows; i++) {
45 for (let j = 0; j < cols; j++) {
46 const cell = input[i][j];
47
48 // If the cell is a flower, keep it as is
49 if (cell === '*') {
50 result[i][j] = '*';
51 } else {
52 // Count adjacent flowers
53 let count = 0;
54
55 for (const [dx, dy] of directions) {
56 const ni = i + dx;
57 const nj = j + dy;
58
59 // Check if the adjacent cell is within bounds
60 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
61 if (input[ni][nj] === '*') {
62 count++;
63 }
64 }
65 }
66
67 // If no adjacent flowers, leave the cell empty
68 // Otherwise, set it to the count
69 result[i][j] = count === 0 ? ' ' : count.toString();
70 }
71 }
72 }
73
74 // Convert the result grid back to an array of strings
75 return result.map(row => row.join(''));
1276};
77
78// Handled Edge Cases: Empty input, inconsistent row lengths, zero-width board
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.