Created
May 12, 2014 10:46
-
-
Save tom-galvin/821253ee803810b5f08b to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge #162e Solution (Novel Compression, pt. 1: Unpacking the Data)
This file contains hidden or 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
def decompress(chunks, dict) | |
delimiter, next_delimiter = '', ' ' | |
output = '' | |
chunks.each do |ch| | |
case ch | |
when /[0-9]\^/ | |
output += delimiter + dict[ch.to_i].capitalize | |
when /[0-9]!/ | |
output += delimiter + dict[ch.to_i].upcase | |
when /[0-9]/ | |
output += delimiter + dict[ch.to_i] | |
when /[rR]/ | |
output += delimiter + "\n" | |
next_delimiter = '' | |
when /[eE]/ | |
output += delimiter # needed for any punctuation at the end of a line | |
break # exit the loop | |
when /\-/ | |
next_delimiter = '-' | |
when /[\.,\?!;:]/ | |
next_delimiter = ch + next_delimiter | |
else | |
puts 'Bad chunk: ' + ch | |
end | |
delimiter = next_delimiter | |
next_delimiter = ' ' | |
end | |
return output | |
end | |
dict_size = gets.chomp.to_i | |
dict = Array.new(dict_size) { gets.chomp.downcase } | |
chunks = [] | |
loop do | |
input_chunks = gets.chomp.split ' ' | |
chunks += input_chunks | |
break if input_chunks.last == 'E' | |
end | |
puts decompress(chunks, dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment