Skip to content

Instantly share code, notes, and snippets.

@tom-galvin
Created May 12, 2014 10:46
Show Gist options
  • Save tom-galvin/821253ee803810b5f08b to your computer and use it in GitHub Desktop.
Save tom-galvin/821253ee803810b5f08b to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge #162e Solution (Novel Compression, pt. 1: Unpacking the Data)
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