Skip to content

Commit

Permalink
improved the code
Browse files Browse the repository at this point in the history
  • Loading branch information
its-ChaTTy committed Apr 22, 2024
1 parent 3551848 commit f68531c
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions practice/ocr-numbers/ocr_numbers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
def process_section(section, digits):
"""
Process a section of the input grid and convert it to a string of digits.
Args:
section (list): A list of strings representing a section of the input grid.
digits (dict): A dictionary mapping grid patterns to digits.
Returns:
str: A string of digits representing the section.
Examples:
>>> process_section([" _ ", "| |", "|_|", " "], digits)
'0'
"""
# Create chunks by joining every three characters in each row of the section
chunks = [''.join(row[i:i+3] for row in section) for i in range(0, len(section[0]), 3)]

# Convert each chunk to a digit (or '?' if the chunk is not recognized)
output = [digits.get(chunk, '?') for chunk in chunks]

# Join the digits into a string and return it
return ''.join(output)


def convert(input_grid):
"""
Convert an OCR number grid to a string of digits.
Args:
input_grid (list): A list of strings representing the OCR number grid.
Returns:
str: A string of digits representing the OCR number grid.
Raises:
ValueError: If the number of input lines is not a multiple of four or
if the number of input columns is not a multiple of three.
Examples:
>>> convert([" _ ", "| |", "|_|", " ", " ", " |", " |", " "])
'0,1'
"""
# Define the mapping from grid patterns to digits
digits = {
" _ | ||_| ": "0",
" | | ": "1",
Expand All @@ -17,10 +55,16 @@ def convert(input_grid):
" _ |_| _| ": "9",
}

# Check if the number of input lines is a multiple of four
if len(input_grid) % 4 != 0:
raise ValueError("Number of input lines is not a multiple of four")

# Check if the number of input columns is a multiple of three
if any(len(row) % 3 != 0 for row in input_grid):
raise ValueError("Number of input columns is not a multiple of three")

# Split the input grid into sections
sections = [input_grid[i:i+4] for i in range(0, len(input_grid), 4)]

# Process each section and join the results with commas
return ','.join(process_section(section, digits) for section in sections)

0 comments on commit f68531c

Please sign in to comment.