Description

Today's challenge will be to create a program to decipher a seven segment display, commonly seen on many older electronic devices.

Input Description

For this challenge, you will receive 3 lines of input, with each line being 27 characters long (representing 9 total numbers), with the digits spread across the 3 lines. Your job is to return the represented digits. You don't need to account for odd spacing or missing segments.

Output Description

Your program should print the numbers contained in the display.

Challenge Inputs

    _  _     _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|
  ||_  _|  | _||_|  ||_| _|

    _  _  _  _  _  _  _  _ 
|_| _| _||_|| ||_ |_| _||_ 
  | _| _||_||_| _||_||_  _|

 _  _  _  _  _  _  _  _  _ 
|_  _||_ |_| _|  ||_ | ||_|
 _||_ |_||_| _|  ||_||_||_|

 _  _        _  _  _  _  _ 
|_||_ |_|  || ||_ |_ |_| _|
 _| _|  |  ||_| _| _| _||_

Challenge Outputs

123456789
433805825
526837608
954105592

Ideas!

If you have an idea for a challenge please share it on /r/dailyprogrammer_ideas and there's a good chance we'll use it.


In [60]:
import os
from itertools import zip_longest, chain

In [61]:
INPUT_1 = \
"""
    _  _     _  _  _  _  _
  | _| _||_||_ |_   ||_||_|
  ||_  _|  | _||_|  ||_| _|
"""

INPUT_2 = \
"""
 _  _  _  _  _  _  _  _  _ 
| | _| _||_ |_  _||_ | ||_|
|_||_ |_ |_| _||_ |_||_||_|
"""

INPUT_3 = \
"""
 _  _  _     _     _  _  _ 
 _||_   ||_|| |  ||_|  || |
 _| _|  |  ||_|  | _|  ||_|
"""

In [62]:
NUMBERS = {
    ' _ | ||_|' : '0',
    '     |  |' : '1',
    ' _  _||_ ' : '2',
    ' _  _| _|' : '3',
    '   |_|  |' : '4',
    ' _ |_  _|' : '5',
    ' _ |_ |_|' : '6',
    ' _   |  |' : '7',
    ' _ |_||_|' : '8',
    ' _ |_| _|' : '9'
}

ASCII = {v:k for k,v in NUMBERS.items()}

In [63]:
LINE_LENGTH = 27
DIGIT_LINES = 3
DIGIT_WIDTH = 3

In [64]:
def grouper(iterable, n, fillvalue=None):
    """
    Collect data into fixed-length chunks or blocks
    https://docs.python.org/3/library/itertools.html#itertools-recipes
    """
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

In [65]:
def de_ascii(ascii_text):
    ascii_lines = ascii_text.split("\n")
    ascii_lines = [str.ljust(line, LINE_LENGTH, ' ') for line in ascii_lines if line != '']
    
    line_groupers = [grouper(line, DIGIT_LINES) for line in ascii_lines]
    
    output_number = ''
    
    for digit_tuple in zip(*line_groupers):
        digit_string = ''.join(chain.from_iterable(digit_tuple))
        output_number = output_number + NUMBERS[digit_string]
        
    return output_number

In [66]:
def ascii(number):
    output_strings = ['    ' for _ in range(DIGIT_LINES)]
    for line in range(DIGIT_LINES):
        for digit in number:
            output_strings[line] += ASCII[digit][0+(DIGIT_WIDTH * line):3 + (DIGIT_WIDTH * line)]
    return '\n'.join(output_strings)

In [67]:
de_ascii(INPUT_1)


Out[67]:
'123456789'

In [68]:
de_ascii(INPUT_2)


Out[68]:
'022652608'

In [70]:
de_ascii(INPUT_3)


Out[70]:
'357401970'