| 1 | | def convert(input_grid: str) -> str: |
| 2 | | pass |
| 1 | def convert(input_grid): |
| 2 | # Edge Case: Empty input |
| 3 | if not input_grid: |
| 4 | return "" |
| 5 | |
| 6 | # Edge Case: Input is a string, convert to list of lines |
| 7 | if isinstance(input_grid, str): |
| 8 | lines = input_grid.splitlines() |
| 9 | else: |
| 10 | lines = input_grid |
| 11 | |
| 12 | # Edge Case: Number of lines is not a multiple of 4 |
| 13 | if len(lines) % 4 != 0: |
| 14 | raise ValueError("Number of input lines is not a multiple of four") |
| 15 | |
| 16 | # Edge Case: Empty grid after splitting |
| 17 | if not lines or (len(lines) == 1 and lines[0] == ""): |
| 18 | return "" |
| 19 | |
| 20 | result_lines = [] |
| 21 | |
| 22 | # Process each 4-line group |
| 23 | for group_start in range(0, len(lines), 4): |
| 24 | group = lines[group_start:group_start+4] |
| 25 | |
| 26 | # Edge Case: Inconsistent row lengths |
| 27 | row_length = len(group[0]) |
| 28 | for row in group: |
| 29 | if len(row) != row_length: |
| 30 | raise ValueError("All rows must have the same length") |
| 31 | |
| 32 | # Edge Case: Row length is not a multiple of 3 |
| 33 | if row_length % 3 != 0: |
| 34 | raise ValueError("Number of input columns is not a multiple of three") |
| 35 | |
| 36 | line_result = "" |
| 37 | |
| 38 | # Process each 3-character segment in the group |
| 39 | for segment_start in range(0, row_length, 3): |
| 40 | # Extract the 3x4 character pattern |
| 41 | pattern = [] |
| 42 | for i in range(4): |
| 43 | if segment_start + 3 <= len(group[i]): |
| 44 | pattern.append(group[i][segment_start:segment_start+3]) |
| 45 | else: |
| 46 | # Edge Case: Not enough characters in a row |
| 47 | pattern.append(" ") # Pad with spaces |
| 48 | |
| 49 | # Convert pattern to digit |
| 50 | digit = recognize_digit(pattern) |
| 51 | line_result += digit |
| 52 | |
| 53 | result_lines.append(line_result) |
| 54 | |
| 55 | return ",".join(result_lines) |
| 3 | 56 | |
| 57 | def recognize_digit(pattern): |
| 58 | # Define patterns for digits 0-9 |
| 59 | patterns = { |
| 60 | (" _ ", "| |", "|_|", " "): "0", |
| 61 | (" ", " |", " |", " "): "1", |
| 62 | (" _ ", " _|", "|_ ", " "): "2", |
| 63 | (" _ ", " _|", " _|", " "): "3", |
| 64 | (" ", "|_|", " |", " "): "4", |
| 65 | (" _ ", "|_ ", " _|", " "): "5", |
| 66 | (" _ ", "|_ ", "|_|", " "): "6", |
| 67 | (" _ ", " |", " |", " "): "7", |
| 68 | (" _ ", "|_|", "|_|", " "): "8", |
| 69 | (" _ ", "|_|", " _|", " "): "9" |
| 70 | } |
| 71 | |
| 72 | # Edge Case: Pattern not recognized |
| 73 | return patterns.get(tuple(pattern), "?") |
| 74 | |
| 75 | # Handled Edge Cases: Empty input, input as string, number of lines not multiple of 4, empty grid, inconsistent row lengths, row length not multiple of 3, not enough characters in a row, pattern not recognized |