Created
May 27, 2014 06:10
-
-
Save kitroed/2b8e296a8e80f7533bbb to your computer and use it in GitHub Desktop.
r/dailyprogrammer (Easy): Novel Compression, pt. 1: Unpacking the Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# daily programmer #162 easy "Novel Compression pt. 1: Unpacking the Data" | |
# author: Kit Roed | |
# example dictionary: | |
# | |
# is | |
# my | |
# hello | |
# name | |
# stan | |
# And we are given the following chunks: | |
# 2! ! R 1^ 3 0 4^ . E | |
# Then the output text is: | |
# HELLO! | |
# My name is Stan. | |
# take dict input in from the console (add until empty string passed) | |
dictionary = [] | |
input_word_count = int(input('dictionary length: ')) | |
for word_number in range(0, input_word_count): | |
dictionary.append(input('Enter dictionary word %d of %d: ' % (word_number + 1 , input_word_count))) | |
# now ask for "chunks" | |
chunks = '' | |
while True: | |
chunk_input = input('Enter data chunk (blank to stop): ') | |
if len(chunk_input) > 0: | |
chunks += chunk_input + ' ' | |
else: | |
break | |
# process chunks | |
# split works on whitespace by default (handy!) | |
digits = '' | |
output = '' | |
leading_space = False | |
for chunk in chunks.split(): | |
# before looping through each chunk, output any digits left from the last pass | |
# (used for a normal numeric item) | |
if len(digits) > 0: | |
if leading_space: | |
output += ' ' | |
output += dictionary[int(digits)] | |
leading_space = True | |
prev_was_digit = False | |
digits = '' | |
for c in chunk: | |
if c.isdigit(): | |
digits += c | |
prev_was_digit = True | |
elif c == '^' and prev_was_digit: | |
# word from dict with first letter caps | |
if leading_space: | |
output += ' ' | |
output += dictionary[int(digits)].title() | |
digits = '' | |
leading_space = True | |
elif c == '!' and prev_was_digit: | |
# word from dict in all caps | |
if leading_space: | |
output += ' ' | |
output += dictionary[int(digits)].upper() | |
digits = '' | |
leading_space = True | |
elif c == '-': | |
# word with hyphen instead of space | |
leading_space = False | |
output += c | |
elif c in ('.', ',', ',', '?', '!', ';', ':') and not prev_was_digit: | |
output += c | |
elif c == 'R': | |
output += '\n' | |
leading_space = False | |
elif c == 'E': | |
break | |
print(output) | |
# Output: | |
# I do not like them in a house. | |
# I do not like them with a mouse. | |
# I do not like them here or there. | |
# I do not like them anywhere. | |
# I do not like green eggs and ham. | |
# I do not like them, Sam-I-am. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment