corpus

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 representing the annotated flower field.
910 */
1011export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12};
12 // Edge Case: Handle empty input
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 inconsistent 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 // Create a 2D array for the output
28 const output = Array(rows);
29 for (let i = 0; i < rows; i++) {
30 output[i] = input[i].split('');
31 }
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
41 for (let i = 0; i < rows; i++) {
42 for (let j = 0; j < cols; j++) {
43 // If the cell is empty, count adjacent flowers
44 if (input[i][j] === ' ') {
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 bounds to avoid accessing invalid indices
53 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
54 if (input[ni][nj] === '*') {
55 count++;
56 }
57 }
58 }
59
60 // Edge Case: Only replace empty cell with count if count > 0
61 if (count > 0) {
62 output[i][j] = count.toString();
63 }
64 }
65 }
66 }
67
68 // Convert each row back to a string
69 for (let i = 0; i < rows; i++) {
70 output[i] = output[i].join('');
71 }
72
73 return output;
74 // Handled Edge Cases: Empty input, rows with inconsistent lengths, accessing invalid indices, replacing empty cells only when count > 0
75};
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.