| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export const convert = () => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 6 | export const convert = (input) => { |
| 7 | // Edge Case: Handle empty input |
| 8 | if (!input || input.length === 0) { |
| 9 | return ''; |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Handle string input by splitting into lines |
| 13 | if (typeof input === 'string') { |
| 14 | input = input.split('\n'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Handle input that is not an array |
| 18 | if (!Array.isArray(input)) { |
| 19 | throw new Error('Invalid input format'); |
| 20 | } |
| 21 | |
| 22 | // Edge Case: Handle input with incorrect number of rows (must be multiple of 4) |
| 23 | if (input.length % 4 !== 0) { |
| 24 | throw new Error('Incorrect number of rows'); |
| 25 | } |
| 26 | |
| 27 | const result = []; |
| 28 | const numRows = input.length; |
| 29 | const numLines = numRows / 4; |
| 30 | |
| 31 | for (let line = 0; line < numLines; line++) { |
| 32 | const startRow = line * 4; |
| 33 | const lineRows = input.slice(startRow, startRow + 4); |
| 34 | |
| 35 | // Edge Case: Handle inconsistent row lengths within a line |
| 36 | const lineWidth = lineRows[0].length; |
| 37 | for (let i = 1; i < 4; i++) { |
| 38 | if (lineRows[i].length !== lineWidth) { |
| 39 | throw new Error('Rows have inconsistent lengths'); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Handle rows that are not multiple of 3 characters wide |
| 44 | if (lineWidth % 3 !== 0) { |
| 45 | throw new Error('Invalid row width'); |
| 46 | } |
| 47 | |
| 48 | const numDigits = lineWidth / 3; |
| 49 | let lineResult = ''; |
| 50 | |
| 51 | for (let digit = 0; digit < numDigits; digit++) { |
| 52 | const startCol = digit * 3; |
| 53 | const digitPattern = [ |
| 54 | lineRows[0].substring(startCol, startCol + 3), |
| 55 | lineRows[1].substring(startCol, startCol + 3), |
| 56 | lineRows[2].substring(startCol, startCol + 3), |
| 57 | lineRows[3].substring(startCol, startCol + 3) |
| 58 | ]; |
| 59 | |
| 60 | lineResult += recognizeDigit(digitPattern); |
| 61 | } |
| 62 | |
| 63 | result.push(lineResult); |
| 64 | } |
| 65 | |
| 66 | return result.join(','); |
| 67 | }; |
| 68 | |
| 69 | const recognizeDigit = (pattern) => { |
| 70 | // Define patterns for digits 0-9 |
| 71 | const patterns = { |
| 72 | ' _ | ||_| ': '0', |
| 73 | ' | | ': '1', |
| 74 | ' _ _||_ ': '2', |
| 75 | ' _ _| _| ': '3', |
| 76 | ' |_| | ': '4', |
| 77 | ' _ |_ _| ': '5', |
| 78 | ' _ |_ |_| ': '6', |
| 79 | ' _ | | ': '7', |
| 80 | ' _ |_||_| ': '8', |
| 81 | ' _ |_| _| ': '9' |
| 82 | }; |
| 83 | |
| 84 | const key = pattern.join(''); |
| 85 | |
| 86 | // Edge Case: Handle unrecognized digit patterns |
| 87 | return patterns[key] || '?'; |
| 8 | 88 | }; |
| 89 | |
| 90 | // Handled Edge Cases: empty input, input not being an array, incorrect number of rows, inconsistent row lengths, invalid row width, unrecognized digit patterns |