Skip to content

Instantly share code, notes, and snippets.

@yatharth
Last active August 29, 2015 14:05
Show Gist options
  • Save yatharth/60eda92021a59bb27d51 to your computer and use it in GitHub Desktop.
Save yatharth/60eda92021a59bb27d51 to your computer and use it in GitHub Desktop.
No context here…
#!/usr/bin/env python3
# horizontal and vetical constants representing directives (class for figits seemed excessive)
BLANK, BAR__ = 'BLANK', 'BAR__'
LEFT_, RIGHT, BOTH_ = 'LEFT_', 'RIGHT', 'BOTH_'
# list of directives for digits 0-9 (horizionatal, vertical, horizontal, vertical, horizontal)
DIGITS = (
(BAR__, BOTH_, BLANK, BOTH_, BAR__),
(BLANK, RIGHT, BLANK, RIGHT, BLANK),
(BAR__, RIGHT, BAR__, LEFT_, BAR__),
(BAR__, RIGHT, BAR__, RIGHT, BAR__),
(BLANK, BOTH_, BAR__, RIGHT, BLANK),
(BAR__, LEFT_, BAR__, RIGHT, BAR__),
(BAR__, LEFT_, BAR__, BOTH_, BAR__),
(BAR__, RIGHT, BLANK, RIGHT, BLANK),
(BAR__, BOTH_, BAR__, BOTH_, BAR__),
(BAR__, BOTH_, BAR__, RIGHT, BAR__),
)
def get_digit(size, digit):
'''Returns raw represntation of digit sized `s+2` * `2s+3`.'''
# initialize variables
assert 1 <= size <= 10, AssertionError("Size not between 1 and 10: {}".format(size))
digit = DIGITS[digit]
output = []
# interpret directives
for directive in digit:
if directive in (BLANK, BAR__):
line = ' ' + ('-' if directive == 'BAR__' else ' ')*size + ' '
output.append(line)
elif directive in (LEFT_, RIGHT, BOTH_):
line = ('|' if directive != RIGHT else ' ') + ' '*size + ('|' if directive != LEFT_ else ' ')
output.extend([line] * (size))
return output
def print_number(line):
'''Concatenates digit representations and prints the (multi-)digit number.'''
# validate input
line = line.split()
assert len(line) == 2, AssertionError("Line contains {} {} input".format(abs(len(line) - 2), 'extra' if len(line) > 2 else 'less'))
assert all(x.isdigit() for x in line), AssertionError("Input is not numeric")
# initialize variables
size, numbers = line
output = [''] * (2*int(size) + 3)
if int(size) == 0:
return
# concatenate digit representations
for number in numbers:
output = [old + new for old, new in \
zip(output, get_digit(int(size), int(number)))]
# print the line
print('\n'.join(output))
def main():
'''Execute the main loop.'''
with open('input.txt', 'r') as lines:
for line in lines.readlines():
if line == '00\n':
break
else:
print_number(line)
else:
print("Warning: Input was not terminated by '00'")
if __name__ == '__main__':
main()
- -
| | |
- -
|| |
- -
-- --
| || |
| || |
-- -- --
| || |
| || |
-- --
--- --- ---
|| || |
|| || |
|| || |
--- ---
|| | |
|| | |
|| | |
--- ---
----------
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
----------
0 1234567890
1 123
2 456
3 789
10 0
00
4 1234567890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment