Finished
88 * @returns {string}
99 */
1010export const convert = (text) => {
11 throw new Error('Remove this line and implement the function');
11 // Edge Case: Empty input
12 if (!text) return '';
13
14 const lines = text.split('\n');
15
16 // Edge Case: Invalid number of lines (must be multiple of 4)
17 if (lines.length % 4 !== 0) {
18 throw new Error('Invalid input grid');
19 }
20
21 const result = [];
22
23 // Process 4 lines at a time (one digit row)
24 for (let i = 0; i < lines.length; i += 4) {
25 const line1 = lines[i];
26 const line2 = lines[i + 1];
27 const line3 = lines[i + 2];
28 const line4 = lines[i + 3];
29
30 // Edge Case: Fourth line is not blank (allow trailing spaces)
31 if (line4.trim() !== '') {
32 throw new Error('Invalid input grid');
33 }
34
35 // Edge Case: Lines have different lengths (except fourth line which can be blank with spaces)
36 let lineWidth = line1.length;
37
38 // Allow some flexibility in line lengths for multi-line inputs
39 // but ensure they are reasonable for OCR processing
40 if (line2.length !== lineWidth || line3.length !== lineWidth) {
41 // Check if the difference is small and could be due to formatting
42 const maxLen = Math.max(lineWidth, line2.length, line3.length);
43 const minLen = Math.min(lineWidth, line2.length, line3.length);
44
45 // If the difference is more than 3 characters, it's likely invalid
46 if (maxLen - minLen > 3) {
47 throw new Error('Invalid input grid');
48 }
49
50 // Use the maximum length for processing
51 lineWidth = maxLen;
52 }
53
54 // Edge Case: Line length is not a multiple of 3
55 if (lineWidth % 3 !== 0) {
56 throw new Error('Invalid input grid');
57 }
58
59 let rowResult = '';
60
61 // Process each 3-character wide digit
62 for (let j = 0; j < lineWidth; j += 3) {
63 const digitPattern = [
64 line1.substring(j, j + 3),
65 line2.substring(j, j + 3),
66 line3.substring(j, j + 3)
67 ];
68
69 rowResult += recognizeDigit(digitPattern);
70 }
71
72 result.push(rowResult);
73 }
74
75 return result.join(',');
76};
77
78const recognizeDigit = (pattern) => {
79 const patterns = {
80 ' _ | ||_|': '0',
81 ' | |': '1',
82 ' _ _||_ ': '2',
83 ' _ _| _|': '3',
84 ' |_| |': '4',
85 ' _ |_ _|': '5',
86 ' _ |_ |_|': '6',
87 ' _ | |': '7',
88 ' _ |_||_|': '8',
89 ' _ |_| _|': '9'
90 };
91
92 const key = pattern.join('');
93
94 // Edge Case: Unrecognized pattern
95 return patterns[key] || '?';
1296};
97
98// Handled Edge Cases: Empty input, Invalid number of lines, Fourth line is not blank, Lines have different lengths, Line length is not a multiple of 3, Unrecognized pattern
Test NameStatus
recognizes zero
Pass
recognizes one
Pass
recognizes two
Pass
recognizes three
Pass
recognizes four
Pass
recognizes five
Pass
recognizes six
Pass
recognizes seven
Pass
recognizes eight
Pass
recognizes nine
Pass
recognizes ten
Pass
identifies garble
Pass
converts 110101100
Pass
identifies garble mixed in
Pass
converts 1234567890
Pass
converts 123 456 789
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.